Using custom select with react-hook-form #2825
Replies: 6 comments 12 replies
-
@bluebill1049 you might find this interesting. |
Beta Was this translation helpful? Give feedback.
-
It might be better with CodeSandbox. Though I must say that the custom component could be handled with the |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 I will look into that. Thanks ❤️ . |
Beta Was this translation helpful? Give feedback.
-
This solution looks great but I have found some flaws that make it unusable in my case. If you use the ID/Class for getting the Right now I'm struggling because I can't find a way to refactor this:
Into something that update the select value. |
Beta Was this translation helpful? Give feedback.
-
In my case, all I needed was to know that this could be done: https://www.carlrippon.com/using-a-forwarded-ref-internally/ |
Beta Was this translation helpful? Give feedback.
-
`const { /** */ /** / useOutsideClick(selectRef, () => { useEffect(() => {
}, [selected]) <select className={styles.htmlSelect} id={id} {...register(name)} > {options.map((item) => ( {item.desc} ))} <div className={styles.customSelectWrapper} onClick={() => { setOpen((prev) => !prev) }} ref={selectRef} > <div className={cn(styles.customSelect, isOpen && styles.open)}> {options.find((item) => item.value === selected)?.desc || label} {options.map((item) => ( <div className={styles.optionContainer} key={item.value} onClick={() => { setSelected(item.value) }} > <span className={cn(styles.customOption, selected === item.value && styles.selected)} data-value={item.value} > {item.desc} ))} )` what i do wrong? The data is always an empty string |
Beta Was this translation helpful? Give feedback.
-
For whoever is interested in doing this. Here is how I did it, and it worked like a charm.
Stop ✋ All you need is here ⬇️
Codesandbox: https://codesandbox.io/s/custom-select-component-with-react-hook-form-jj91d
Code overview
Some files and theme variables are not included. Head to the Codesandbox to see full version
Select component
TSX
###Style
###Usage
CSS credit goes to https://andrejgajdos.com/custom-select-dropdown/ ❤️
Beta Was this translation helpful? Give feedback.
All reactions