import React, { useRef } from 'react' import { OutlinedInput } from '@toptal/picasso-outlined-input' import { documentable, forwardRef, noop, useCombinedRefs, } from '@toptal/picasso-utils' import { twJoin, twMerge } from '@toptal/picasso-tailwind-merge' import { SelectCaret } from '../SelectCaret' import type { ValueType, SelectProps } from '../SelectBase' import { getOptionText, useAdornments, useSelectState, useSelectProps, renderOption as defaultRenderOption, } from '../SelectBase' import NativeSelectOptions from '../NativeSelectOptions' import NativeSelectPlaceholder from '../NativeSelectPlaceholder' import { NativeSelectInput } from './NativeSelectInput' const DEFAULT_EMPTY_ARRAY_VALUE: ValueType[] = [] const classesByWidth: Record< Exclude, string > = { full: 'w-full', shrink: 'w-auto', auto: '', } export const NativeSelect = documentable( forwardRef( ( props: SelectProps, ref: React.Ref | null ) => { const { className, style, width = 'full', loading = false, id, icon, iconPosition = 'start', name, renderOption = defaultRenderOption, placeholder, disabled = false, status = 'default', multiple, value = multiple ? DEFAULT_EMPTY_ARRAY_VALUE : '', size = 'medium', enableReset, onChange = noop, options, getDisplayValue = getOptionText, /* eslint-disable @typescript-eslint/no-unused-vars */ menuWidth, popperContainer, enableAutofill, autoComplete, searchPlaceholder, searchThreshold, limit, native, testIds, highlight, enableResetSearch, /* eslint-enable @typescript-eslint/no-unused-vars */ ...rest } = props const selectRef = useCombinedRefs( ref, useRef(null) ) const inputWrapperRef = useRef(null) const selectState = useSelectState({ getDisplayValue, options, disabled, multiple, value, limit, }) const { selection, emptySelectValue } = selectState const { getItemProps, getInputProps } = useSelectProps({ selectRef, selectProps: props, selectState, }) const [selectStartAdornment, selectEndAdornment] = useAdornments({ position: iconPosition, icon, loading, disabled, }) const startAdornment = selectStartAdornment && (
{selectStartAdornment}
) const endAdornment = selectEndAdornment && (
{selectEndAdornment}
) const children = ( <> {placeholder} ) const nativeSelectComponent = ( , className: twJoin( 'w-full p-2 focus:bg-inheritColor', !selection.isSelected() && 'text-gray-600', React.isValidElement(startAdornment) && 'pl-[2.5625rem]', React.isValidElement(endAdornment) && 'pr-[3.5625rem]' ), }} /> ) return (
{nativeSelectComponent}
) } ) ) NativeSelect.displayName = 'NativeSelect' export default NativeSelect