'use client'; import * as React from 'react'; import { classNames } from '@vkontakte/vkjs'; import { type Placement } from '@vkontakte/vkui-floating-ui/core'; import { useBooleanState } from '../../hooks/useBooleanState'; import { useExternRef } from '../../hooks/useExternRef'; import { callMultiple } from '../../lib/callMultiple'; import { CustomScrollView } from '../CustomScrollView/CustomScrollView'; import { CustomSelectOption } from '../CustomSelectOption/CustomSelectOption'; import { DropdownIcon } from '../DropdownIcon/DropdownIcon'; import { Input, type InputProps } from '../Input/Input'; import { Popper } from '../Popper/Popper'; import styles from './CalendarTime.module.css'; type Label = { /** * Значение. */ value: string; /** * Лейбл. */ label: string; /** * Disabled состояние. */ disabled?: boolean; }; type OptionProps = { /** * Выбранное значение. */ selectedValue: string | number | readonly string[] | undefined; /** * Опция. */ option: Label; /** * Нажатие на элемент. */ onClickOption?: ((value: string) => void) | undefined; }; function getScrollParent(node: Node | null): HTMLElement | null { if (!(node instanceof HTMLElement)) { return null; } if (node.scrollHeight > node.clientHeight) { return node; } else { return getScrollParent(node.parentNode); } } function Option({ option, onClickOption, selectedValue }: OptionProps) { const ref = React.useRef(null); const selected = selectedValue === option.value; React.useEffect(() => { if (!selected) { return; } const element = ref.current; const scrollElement = getScrollParent(ref.current); if (!element || !scrollElement) { return; } scrollElement.scrollTo({ top: element.offsetTop - scrollElement.offsetHeight / 2 + element.offsetHeight / 2, }); }, [selected]); return ( { onClickOption?.(option.value); }} selected={selected} disabled={option.disabled} textNoWrap > {option.label} ); } type ComboBoxProps = InputProps & { // eslint-disable-next-line jsdoc/require-jsdoc readonly labels?: readonly Label[] | undefined; /** * Нажатие на элемент. */ onClickOption?: ((value: string) => void) | undefined; }; export function ComboBox({ getRootRef, ref: refProp, onBlur, onFocus, after, labels = [], defaultValue, slotProps, onClickOption, ...restProps }: ComboBoxProps) { const ref = useExternRef(getRootRef, refProp); const inputRef = useExternRef(slotProps?.input?.getRootRef); const [focus, setFocus, setBlur] = useBooleanState(false); const [popperPlacement, setPopperPlacement] = React.useState('bottom'); const labelsElements = focus ? labels.map((option) => { return (