2.69.0 • Published 4 months ago

@s-ui/react-molecule-autosuggest v2.69.0

Weekly downloads
987
License
MIT
Repository
-
Last release
4 months ago

MoleculeAutosuggest

MoleculeAutosuggest is an input that will display a list of suggestions while values are entered It allows Single and Multiple Selection

Installation

$ npm install @s-ui/react-molecule-autosuggest --save

Usage

import MoleculeAutosuggest, { MoleculeAutosuggestDropdownListSizes } from '@s-ui/react-molecule-autosuggest'
import MoleculeAutosuggestOption from '@s-ui/react-molecule-dropdown-option'

const IconCloseTag = () => <span>x</span>  
const IconClear = () => <span>x</span>  

const suggestions = ['John','Johnny']

Single Selection

Basic usage

 <MoleculeAutosuggest
  value={'Jo'}
  placeholder="Select an option..."
  iconClear={<IconClose />}
  state="success"
>
  {suggestions.map((suggestion, i) => (
    <MoleculeAutosuggestOption key={i} value={suggestion}>
      {option}
    </MoleculeAutosuggestOption>
  ))}
</MoleculeAutosuggest>

Large List

<MoleculeAutosuggest
  value={'Jo'}
  placeholder="Select an option..."
  iconClear={<IconClose />}
  size={MoleculeAutosuggestDropdownListSizes.LARGE}
>
  {suggestions.map((suggestion, i) => (
    <MoleculeAutosuggestOption key={i} value={suggestion}>
      {option}
    </MoleculeAutosuggestOption>
  ))}
</MoleculeAutosuggest>

Multiple Selection

Basic usage

<MoleculeAutosuggest
  value={'Jo'}
  placeholder="Select some options..."
  iconCloseTag={<IconCloseTag />}
  iconClear={<IconClear />}
  multiselection
>
  {suggestions.map((suggestion, i) => (
    <MoleculeAutosuggestOption key={i} value={suggestion}>
      {option}
    </MoleculeAutosuggestOption>
  ))}
</MoleculeAutosuggest>

Managing State

Custom component from MoleculeAutosuggest that handle State

MoleculeAutosuggest 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 (MoleculeAutosuggestOption) to MoleculeAutosuggest

Example:

import React, {Component} from 'react'

import MoleculeAutosuggest from '@s-ui/react-molecule-autosuggest'
import MoleculeAutosuggestOption from '@s-ui/react-molecule-dropdown-option'

import {getAsyncCountriesFromQuery} from '../services'

export default class AutosuggestSingleWithAsyncOptions extends Component {
  state = {value: '', options: []}

  onChange = async (e, {value}) => {
    const options = await getAsyncCountriesFromQuery({query: value})
    this.setState({value, options})
  }

  render() {
    const {options, value} = this.state
    const {onChange, props} = this

    return (
      <MoleculeAutosuggest {...props} value={value} onChange={onChange}>
        {options.map((option, i) => (
          <MoleculeAutosuggestOption key={i} value={option}>
            {option}
          </MoleculeAutosuggestOption>
        ))}
      </MoleculeAutosuggest>
    )
  }
}

so then, the AutosuggestSingleWithAsyncOptions can used in this way...

  <AutosuggestSingleWithAsyncOptions iconClear={<IconClear />} />

Using an hoc like withDynamicOptions

It can be useful creating a hoc (like the one withDynamicOptions available in the demo) that combined with other hoc from @s-ui/hoc can be used to simplify the use of this component with dinamyc suggestions based on the value

withDynamicOptions

import React, {Component} from 'react'

export default (BaseComponent, BaseChildComponent) => getDynamicOptions => {
  const displayName = BaseComponent.displayName

  return class withDynamicOptions extends Component {
    static displayName = `withDynamicOptions(${displayName})`

    state = { options: []}

    async componentDidUpdate({value: prevQuery}) {
      const {value: query} = this.props
      if (query !== prevQuery) {
        const options = await getDynamicOptions({query})
        this.setState({options}) // eslint-disable-line
      }
    }

    render() {
      const {props} = this
      const {options} = this.state
      return (
        <BaseComponent {...props}>
          {options.map((option, i) => (
            <BaseChildComponent key={i} value={option}>
              {option}
            </BaseChildComponent>
          ))}
        </BaseComponent>
      )
    }
  }
}

use of withDynamicOptions for creating a stateful version of MoleculeAutosuggest

import MoleculeAutosuggest from '@s-ui/react-molecule-autosuggest'
import MoleculeAutosuggestOption from '@s-ui/react-molecule-dropdown-option'

import withDynamicOptions from './hoc/withDynamicOptions'
import {withStateValue} from '@s-ui/hoc'

// some function that gets a `{query}` and returns asynchronoudly a list of values
import {getAsyncCountriesFromQuery} from './services'

const MoleculeAutosuggestWithDynamicOptions = withDynamicOptions(
  MoleculeAutosuggest,
  MoleculeAutosuggestOption
)(getAsyncCountriesFromQuery)


const MoleculeAutosuggestWithState = withStateValue(
  MoleculeAutosuggestWithDynamicOptions
)

so then, the MoleculeAutosuggestWithState can used in this way...

<MoleculeAutosuggestWithState
  placeholder="Type a Country name..."
  onChange={(_, {value}) => console.log(value)}
  iconClear={<IconClear />}
/>

Find full description and more examples in the demo page.

2.69.0

4 months ago

2.68.0

9 months ago

2.66.0

2 years ago

2.66.0-beta.0

2 years ago

2.67.0

1 year ago

2.65.0-beta.0

2 years ago

2.64.0

2 years ago

2.62.0

2 years ago

2.65.0

2 years ago

2.63.0

2 years ago

2.59.0

2 years ago

2.60.0

2 years ago

2.58.0

2 years ago

2.61.0

2 years ago

2.55.0

2 years ago

2.57.0

2 years ago

2.56.0

2 years ago

2.53.0

2 years ago

2.54.0

2 years ago

2.52.0

3 years ago

2.51.0

3 years ago

2.49.0

3 years ago

2.50.0

3 years ago

2.48.0

3 years ago

2.47.0

3 years ago

2.46.0

3 years ago

2.45.0

3 years ago

2.44.0

3 years ago

2.43.0

3 years ago

2.42.0

3 years ago

2.41.0

3 years ago

2.40.0

3 years ago

2.39.0

3 years ago

2.38.0

3 years ago

2.34.0

3 years ago

2.36.0

3 years ago

2.37.0

3 years ago

2.32.0

3 years ago

2.31.0

3 years ago

2.33.0

3 years ago

2.29.0

3 years ago

2.30.0

3 years ago

2.25.0

3 years ago

2.24.0

3 years ago

2.23.0

3 years ago

2.22.0

3 years ago

2.21.0

3 years ago

2.20.0

4 years ago

2.19.0

4 years ago

2.18.0

4 years ago

2.17.0

4 years ago

2.16.0

4 years ago

2.15.0

4 years ago

2.14.0

4 years ago

2.13.0

4 years ago

2.12.0

4 years ago

2.11.0

4 years ago

2.10.0

4 years ago

2.9.0

4 years ago

2.8.0

4 years ago

2.7.0

4 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.16.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.13.0

4 years ago

1.12.0

4 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago