import React, { FC, useCallback, useContext, useEffect, useRef, useState } from 'react' import { MenuOption } from '../../../../../../common/types' import { convertPrimitiveToString, wait, waitForElement } from '../../../../../../common/utils' import { OmniFilterContext } from '../../../../../OmniSearch' import { AutoSizeInput } from '../../../../auto-size-input/AutoSizeInput' import { FilterItemContext } from '../../../../filter-item/FilterItem' import { FilterPopover } from '../../../../filter-popover/FilterPopover' import { InputSegment } from '../../../../input-segment/InputSegment' import { LoadingSpinner } from '../../../../loading-spinner/LoadingSpinner' import { MenuLoading } from '../../../../menu-loading/MenuLoading' import { Menu } from '../../../../menu/Menu' import { useOptions, useResolvedLabel } from '../../../option-hooks/option-hooks' import { typeGuardPrimitiveValue } from '../../../value-segment/ValueSegment' import { useFilterItemSegmentStyles, ValueWithOptionsSegmentProps } from '../../ValueSegments' export const SinglewithOptions: FC = ({ index, optionsResolver, }) => { // hooks const styles = useFilterItemSegmentStyles() // refs const containerRef = useRef(null) const inputRef = useRef(null) // context const { focusMode, focusedSegmentIndex, focusedFilterIndex, } = useContext(OmniFilterContext) const { filter: { field, operator, value }, filterIndex, onChangeValue, focusNextSegment, } = useContext(FilterItemContext) // memoized values and constants const resolveOptionLabel = field?.option.resolveOptionLabel; const primitiveValue = typeGuardPrimitiveValue(operator, value) || '' const dontRefreshOnSearch = field?.option.dontRefreshOnSearch // state const [displayValue, setDisplayValue] = useState(convertPrimitiveToString(primitiveValue)) const [searchQuery, setSearchQuery] = useState('') const [renderChildren, setRenderChildren] = useState(false) // menu option hooks const { menuOptions, optionsLoading } = useOptions(searchQuery, optionsResolver, dontRefreshOnSearch) const { resolvedLabel, labelLoading } = useResolvedLabel(menuOptions, convertPrimitiveToString(primitiveValue), resolveOptionLabel) // more memoized values and constants const isSelected = (focusMode === 'segment' || focusMode === 'cursor') && (focusedFilterIndex === filterIndex) && (focusedSegmentIndex === index) // imperative logic const selectAll = useCallback(() => { if (!inputRef.current) return inputRef.current.setSelectionRange(0, inputRef.current.value.length) }, []) // side effects useEffect(() => { if (isSelected && !document.activeElement?.isSameNode(inputRef.current)) { inputRef.current?.focus() if (focusMode === 'segment') { selectAll() } } }, [focusMode, isSelected, selectAll]) useEffect(() => { if (isSelected) return setDisplayValue(resolvedLabel || '') setSearchQuery('') // eslint-disable-next-line react-hooks/exhaustive-deps }, [resolvedLabel]) useEffect(() => { const waitForContainer = async () => { while (!containerRef.current) { await wait(50) } setRenderChildren(true) } waitForContainer() }, []) // event handlers const handleInputChange = useCallback((event: React.ChangeEvent) => { setDisplayValue(event.target.value) setSearchQuery(event.target.value) }, []) const handleSelectOption = useCallback((option: MenuOption) => { onChangeValue(option.filterValue === undefined ? option.label : option.filterValue) setDisplayValue(option.label) setSearchQuery('') focusNextSegment() }, [focusNextSegment, onChangeValue]) const handleFocusInput = useCallback(() => { requestAnimationFrame(async () => { await waitForElement(inputRef.current) inputRef.current?.focus() }) }, []) return ( {renderChildren && ( <> {labelLoading ? (
) : ( ) : ( )} > )} )}
) }