import { RtBaseProps, useRtBaseProps } from '@dpa/react-hoc'; import { ForwardedRef, forwardRef, ReactNode } from 'react'; import { RtFormFieldCombobox } from '@dpa/react-form-field-combobox'; import { RtFormFieldSelectProps } from '../rt-form-field-select.types'; import classNames from 'classnames/bind'; import styles from '../styles/rt-form-field-select.module.css'; import { useDefaultProps } from '../hooks/use-default-props'; import { useSelect } from '../hooks/use-select'; import { RtFormFieldSizeUI } from '@dpa/react-form-field-text'; import { RtFrame } from '@dpa/react-frame-intro'; import { RtFormFieldTextForSelect } from './rt-form-field-text-for-select'; import { SelectedOptionsContext } from '../context/selected-options-context'; import { Virtuoso } from 'react-virtuoso'; const cn = classNames.bind(styles); export const RtFormFieldSelect: ( props: RtFormFieldSelectProps & RtBaseProps & { ref?: ForwardedRef } ) => ReactNode = forwardRef(function RtFormFieldSelect(_props, ref) { const props = useDefaultProps(_props); const { use, modifier, ui, size, labelInValue, multiple, options, value, listMaxHeight, onChange, maxTagCount, disabled, sortFn, filterFn, filterMode, onFocus, onBlur, listMaxItemsHeight, shouldRenderSelectedOptions, dropdownProps, dropdownRender: _dropdownRender, optionRender: _optionRender, notFoundContent: _notFoundContent, onDeselect, onDropdownScroll, onDropdownScrollToBottomEnd, selectedOptionsRenderFn, ...formFieldTextProps } = props; const baseProps = useRtBaseProps(props, '0.2.2'); const { apiRef, inputRef, handleClear, handleSelect, selectedLabel, selectedValueId, selectedValuesList, selectedOptions, onDebounceFilter, filteredOptions, searchValue, notFoundContent, optionRender, dropdownRender, dropdownScrollHandler, virtualMaxItemsLength, } = useSelect(props); return ( onDebounceFilter(''), 300); }, animation: props.dropdownProps?.animation, closeOnOutsidePress: props.dropdownProps?.closeOnOutsidePress, zIndex: props.dropdownProps?.zIndex, }} {...({ ui, size } as RtFormFieldSizeUI)} formFieldComponent={RtFormFieldTextForSelect} onFocus={props.onFocus} onBlur={props.onBlur} > {dropdownRender( <> { return (
handleSelect(option, e)} onMouseDown={(e) => { e.preventDefault(); e.stopPropagation(); }} style={{ paddingBottom: index !== filteredOptions.length - 1 ? '0.5em' : 0, }} > {optionRender(option, { index, isActive: multiple ? selectedValuesList.includes(option.value) : selectedValueId === option.value, })}
); }} /> {filteredOptions.length === 0 && notFoundContent} )}
); });