import { useTheme } from 'next-themes';
import { find } from 'lodash';
const ProductSelect = ({
loading,
options,
onSelect,
value,
handleChange,
...props
}) => {
const findDefaultValueById = find(options, { value });
if (value && !findDefaultValueById) {
return 'Loading...';
}
const { theme } = useTheme();
const isDarkMode = theme === 'light';
const backgroundColor = !isDarkMode ? '#18181b' : 'white';
const borderColor = !isDarkMode ? '#3f3f46' : '#d4d4d8';
const textColor = !isDarkMode ? 'white' : '#18181b';
const placeholderColor = !isDarkMode ? '#fff' : '#18181b';
const selectedValueColor = !isDarkMode ? 'white' : '#18181b';
const handleSelect = (selectedValue) => {
if (selectedValue && onSelect) {
onSelect(selectedValue.id || null);
} else if (!selectedValue) {
onSelect(null);
}
};
return (
<div className="z-50 relative">
<AsyncSelect
className={`bg-${backgroundColor} ${
isDarkMode ? 'dark:bg-[#18181b]' : ''
} w-full outline-none min-h-unit-12 rounded-large transition-colors motion-reduce:transition-none`}
styles={{
control: (baseStyles) => ({
...baseStyles,
borderColor,
backgroundColor: `bg-${backgroundColor}`,
color: `!text-${textColor}`,
fontSize: 'font-medium',
boxShadow: 'none',
paddingTop: 12,
paddingBottom: 12,
borderRadius: 14,
borderWidth: 2,
}),
menu: (provided) => ({
...provided,
backgroundColor,
borderRadius: 12,
}),
option: (styles, { isFocused }) => ({
...styles,
backgroundColor: isFocused ? borderColor : backgroundColor,
color: isFocused ? textColor : textColor,
borderRadius: 12,
fontSize: 14,
}),
singleValue: (styles) => ({
...styles,
borderRadius: 12,
fontSize: 14,
color: selectedValueColor,
}),
menuList: (styles) => ({
...styles,
borderRadius: 12,
paddingLeft: 12,
paddingRight: 12,
}),
placeholder: (styles) => ({
...styles,
color: placeholderColor,
}),
}}
isClearable
cacheOptions
defaultOptions
options={options}
isLoading={loading}
noOptionsMessage={() => (loading ? 'Loading...' : 'No options found')}
loadingMessage={() => 'Searching...'}
onChange={handleSelect}
defaultValue={findDefaultValueById}
{...props}
/>
</div>
);
};
export default ProductSelect;
<ProductSelect
loading={productsLoading}
label="Product and code"
ariaLabel="Product"
name="profile.product"
placeholder="Product"
value={values.profile.product}
onSelect={(product) => setFieldValue('profile.product', product)}
options={allProducts}
initialValues={values?.profile.product}
handleChange={handleChange}
error={
errors && errors?.profile?.product && touched?.profile?.product
? errors?.profile?.product
: undefined
}
/>