import React from 'react'; import type {FlexProps} from '@gravity-ui/uikit'; import {Flex, Label, Text, TextInput} from '@gravity-ui/uikit'; import i18n from '../i18n'; import {block} from '../shared'; const PixelsInLetter = 10; interface SubjectInputProps { newSubjects: string[]; setNewSubjects: React.Dispatch>; } export function SubjectInput({newSubjects, setNewSubjects}: SubjectInputProps) { const [renderInline, setRenderInline] = React.useState(true); const [inputValue, setInputValue] = React.useState(''); const subjectInputRef = React.useRef(null); const labelsRef = React.useRef(null); const checkWidth = (additionalWidth = 0) => { if (!labelsRef.current || !subjectInputRef.current) { return; } if ( (labelsRef.current.offsetWidth + additionalWidth) * 2 >= subjectInputRef.current.offsetWidth ) { setRenderInline(false); } else { setRenderInline(true); } }; const addNewAclSubject = () => { if (inputValue) { setNewSubjects((prev) => [...prev, inputValue]); setInputValue(''); checkWidth(inputValue.length * PixelsInLetter); } }; const handleEnterClick = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { addNewAclSubject(); } }; const handleRemoveSubjectGetter = (subject: string) => { return () => { setNewSubjects((prev) => prev.filter((el) => el !== subject)); checkWidth(-1 * subject.length * PixelsInLetter); }; }; const renderLabels = (wrap: FlexProps['wrap'] = 'nowrap') => { return ( {newSubjects.map((subject) => ( ))} ); }; return ( {!renderInline && renderLabels('wrap')} ); }