import styles from './InlineForm.module.scss' import { Icon } from '../../_shared/Icon' import { createSignal, Show } from 'solid-js' import { useLocalize } from '../../../context/localize' import { clsx } from 'clsx' type Props = { onClose: () => void onClear?: () => void onSubmit: (value: string) => void variant: 'inBubble' | 'inFloating' validate?: (value: string) => string | Promise initialValue?: string } export const InlineForm = (props: Props) => { const { t } = useLocalize() const [formValue, setFormValue] = createSignal(props.initialValue || '') const [formValueError, setFormValueError] = createSignal('') const handleFormInput = (value) => { setFormValue(value) } const handleSaveButtonClick = async () => { const errorMessage = await props.validate(formValue()) if (errorMessage) { setFormValueError(errorMessage) return } props.onSubmit(formValue()) props.onClose() } const handleKeyPress = async (event) => { setFormValueError('') const key = event.key if (key === 'Enter') { await handleSaveButtonClick() } if (key === 'Esc') { props.onClear } } return (
handleKeyPress(e)} onInput={(e) => handleFormInput(e.currentTarget.value)} /> handleKeyPress(e)} onInput={(e) => handleFormInput(e.currentTarget.value)} />
{formValueError()}
) }