import React, { FC, useCallback, useContext, useEffect, useRef, useState } from 'react' import { OmniFilterOperator } from '../../../../common/types' import { getOperatorDefinition, 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 { OperatorsMenu } from '../../menus/operators-menu/OperatorsMenu' import { InputSegment } from '../../input-segment/InputSegment' import { SegmentProps } from '../value-segments/segment-props' import {} from '../value-segments/segment-props' import { useFilterItemSegmentStyles } from '../value-segments/ValueSegments' export const OperatorSegment: FC = ({ index, }) => { // hooks const styles = useFilterItemSegmentStyles() // refs const containerRef = useRef(null) const inputRef = useRef(null) // context const { focusMode, focusedSegmentIndex, focusedFilterIndex, } = useContext(OmniFilterContext) const { filter: { field, operator }, filterIndex, onChangeOperator, focusNextSegment, } = useContext(FilterItemContext) // state const [displayValue, setDisplayValue] = useState(getOperatorDefinition(field, operator)?.label || '') const [searchQuery, setSearchQuery] = useState('') const [renderChildren, setRenderChildren] = useState(false) // 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(() => { setDisplayValue(getOperatorDefinition(field, operator)?.label || '') setSearchQuery('') // eslint-disable-next-line react-hooks/exhaustive-deps }, [operator]) 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 handleSelectOperator = useCallback((operator: OmniFilterOperator) => { onChangeOperator(operator) setDisplayValue(getOperatorDefinition(field, operator)?.label || '') focusNextSegment() }, [field, focusNextSegment, onChangeOperator]) const handleFocusInput = useCallback(() => { requestAnimationFrame(async () => { await waitForElement(inputRef.current) inputRef.current?.focus() }) }, []) const handleInputBlur = useCallback(() => { if (displayValue !== getOperatorDefinition(field, operator)?.label) { onChangeOperator(null) } }, [displayValue, field, onChangeOperator, operator]) return ( {renderChildren && ( )} > )} ) }