import styles from './InlineForm.module.scss' import { Icon } from '../../_shared/Icon' import { createSignal } from 'solid-js' import { clsx } from 'clsx' type Props = { onClose: () => void onClear?: () => void onSubmit: (value: string) => void validate?: (value: string) => string | Promise | Promise | Promise initialValue?: string showInput?: boolean placeholder: string errorMessage: string autoFocus?: boolean } export const InlineForm = (props: Props) => { const [formValue, setFormValue] = createSignal(props.initialValue || '') const [formValueError, setFormValueError] = createSignal() const handleFormInput = (value) => { setFormValueError() setFormValue(value) } const handleSaveButtonClick = async () => { if (props.validate) { const checkValid = await props.validate(formValue()) if (checkValid) { props.onSubmit(formValue()) props.onClose() } else { setFormValueError(props.errorMessage) } } else { 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)} />
{formValueError()}
) }