Skip to content

Instantly share code, notes, and snippets.

@kristersd
Created November 1, 2022 11:16
Show Gist options
  • Save kristersd/6a1f64022e68843350e1969c0fe2923d to your computer and use it in GitHub Desktop.
Save kristersd/6a1f64022e68843350e1969c0fe2923d to your computer and use it in GitHub Desktop.
const InputSelect = ({options, invalidate, onChange, className, muteEmpty, children, ...props}) => {
useEffect(() => {
if (!invalidate) {
return;
}
if (props.value && !options.some(({value}) => value === props.value)) {
onChange({
target: {
type: "select",
value: options[0]?.value ?? "",
},
});
}
}, [invalidate, options, onChange, props.value]);
return (
<Input {...props}
type="select"
className={classNames({
"custom-select": true,
"custom-select-sm": props.bsSize === "sm",
"custom-select-lg": props.bsSize === "lg",
[className]: !!className,
"text-muted": muteEmpty && !props.value,
})}
onChange={onChange}>
{children}
</Input>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment