Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formik and react-autocomplete

Tags:

reactjs

formik

I'm looking for a way to connect two libraries: react-autocomplete and formik. However, I can not find a way.

Please, give me some tips.

like image 227
konkret12345 Avatar asked Mar 24 '26 16:03

konkret12345


1 Answers

You can make use of setFieldValue provided by Formik.

<Autocomplete
      getItemValue={(item) => item.label}
      items={[{ label: 'apple' }, { label: 'banana' }, { label: 'pear' }]}
      renderItem={(item, isHighlighted) => <div style={{ background: isHighlighted ? 'lightgray' : 'white' }}>{item.label}</div>}
      value={values.value}
      onChange={(e) => setFieldValue('value', e.target.value)}
      onSelect={(val) => setFieldValue('value', val)}
/>

Sample code: https://codesandbox.io/s/n0y93104qp

like image 191
Dani Vijay Avatar answered Mar 27 '26 09:03

Dani Vijay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!