`MoleculeSelect` is a customized `select` created from a combination of `AtomInput`, `MoleculeInputTags`, `MoleculeDropdownList` and `MoleculeDropdownOption`
npm install @s-ui/react-molecule-selectMoleculeSelect is a customized select created from a combination of AtomInput, MoleculeInputTags, MoleculeDropdownList and MoleculeDropdownOption
It allows Single and Multiple Selection





``sh`
$ npm install @s-ui/react-molecule-select --save
`js
import MoleculeSelect, {
moleculeSelectDropdownListSizes
} from '@s-ui/react-molecule-select'
import MoleculeSelectOption from '@s-ui/react-molecule-dropdown-option'
const IconCloseTag = () => x
const IconArrowDown = () => â–¼
const options = ['John', 'Paul', 'George', 'Ringo']
`
#### Basic usage
`js`
onChange={(_, {value}) => console.log(value)}
iconArrowDown={
>
{options.map((option, i) => (
{option}
))}
#### With default value
`js`
onChange={(_, {value}) => console.log(value)}
iconArrowDown={
value="John"
>
{options.map((option, i) => (
{option}
))}
#### Large List
`js`
onChange={(_, {value}) => console.log(value)}
iconArrowDown={
size={moleculeSelectDropdownListSizes.LARGE}
>
{options.map((option, i) => (
{option}
))}
#### With a search input
`js`
onChange={(_, {value}) => console.log(value)}
iconArrowDown={
hasSearch
onSearch={({value}) => console.log(value)}
searchPlaceholder="Search a country..."
noResults={
No results found...
}
size={moleculeSelectDropdownListSizes.LARGE}
>
{options.map((option, i) => (
{option}
))}
#### With state highlight
Can be success, error or alert.
`js`
onChange={(_, {value}) => console.log(value)}
iconArrowDown={
state="success"
>
{options.map((option, i) => (
{option}
))}
#### Basic usage
`js`
iconArrowDown={
multiselection
value={['John', 'Paul']}
>
{options.map((option, i) => (
{option}
))}
#### With selected items limit
`js`
iconArrowDown={
multiselection
value={['John', 'Paul']}
max={2}
>
{options.map((option, i) => (
{option}
))}
MoleculeSelect is a stateless component, so to manage dynamic options we need to create a wrapper component that manages this and pass proper props and proper children (MoleculeSelectOption) to MoleculeSelect
Example:
`js
import React, {Component} from 'react'
import MoleculeSelect from '@s-ui/react-molecule-select'
import MoleculeSelectOption from '@s-ui/react-molecule-dropdown-option'
const options = ['John', 'Paul', 'George', 'Ringo']
export default class SelectSingleWithAsyncOptions extends Component {
state = {value: ''}
onChange = async (e, {value}) => {
this.setState({value})
}
render() {
const {value} = this.state
const {onChange, props} = this
return (
{options.map((option, i) => (
{option}
))}
)
}
}
`
so then, the SelectSingleWithAsyncOptions can used in this way...
`js`
There's a hoc called withStateValue available at @s-ui/hoc
that can be used to simplify the use of this component with internal state
`js
import MoleculeSelect from '@s-ui/react-molecule-select'
import MoleculeSelectOption from '@s-ui/react-molecule-dropdown-option'
import withDynamicOptions from './hoc/withDynamicOptions'
import {withStateValue} from '@s-ui/hoc'
const MoleculeSelectWithState = withStateValue(MoleculeSelect)
const options = ['John', 'Paul', 'George', 'Ringo']
`
so then, the MoleculeSelectWithState can be used in this way...
`js`
onChange={(_, {value}) => console.log(value)}
iconClear={
>
{options.map((option, i) => (
{option}
))}
If you need an option that cannot be customized from MoleculeDropdownOption you can create your own option compatible w/ MoleculeSelect y MoleculeSelect by using the handlersFactory method available in MoleculeDropdownOption that you can use to create proper handlers needed to work properly along w/ MoleculeSelect y MoleculeSelect
`js
import React from 'react'
import {handlersFactory} from '@s-ui/react-molecule-dropdown-option'
const BASE_CLASS = 'AlternativeOption'
const AlternativeOption = ({children, onSelect, innerRef, value}) => {
const {handleClick, handleKeyDown, handleFocus} = handlersFactory({
value,
onSelect
})
return (
AlternativeOption.displayName = 'AlternativeOption'
AlternativeOption.defaultProps = {
onSelect: () => {},
innerRef: React.createRef()
}
export default AlternativeOption
`
so then you can do something like...
`js``
onChange={(_, {value}) => console.log(value)}
iconArrowDown={
>
{options.map((option, i) => (
{option}
))}
> Find full description and more examples in the demo page.