import styles from './InlineForm.module.scss' import { Icon } from '../../_shared/Icon' import { createSignal, onMount } from 'solid-js' import { clsx } from 'clsx' import { Popover } from '../../_shared/Popover' import { useLocalize } from '../../../context/localize' type Props = { onClose: () => void onClear?: () => void onSubmit: (value: string) => void validate?: (value: string) => string | Promise initialValue?: string showInput?: boolean placeholder: string } export const InlineForm = (props: Props) => { const { t } = useLocalize() const [formValue, setFormValue] = createSignal(props.initialValue || '') const [formValueError, setFormValueError] = createSignal() const inputRef: { current: HTMLInputElement } = { current: null } const handleFormInput = (e) => { const value = e.currentTarget.value setFormValueError() setFormValue(value) } const handleSaveButtonClick = async () => { if (props.validate) { const errorMessage = await props.validate(formValue()) if (errorMessage) { setFormValueError(errorMessage) return } } props.onSubmit(formValue()) props.onClose() } const handleKeyDown = async (e) => { setFormValueError('') if (e.key === 'Enter') { e.preventDefault() await handleSaveButtonClick() } if (e.key === 'Escape' && props.onClear) { props.onClear() } } const handleClear = () => { props.initialValue ? props.onClear() : props.onClose() } onMount(() => { inputRef.current.focus() }) return (
(inputRef.current = el)} type="text" value={props.initialValue ?? ''} placeholder={props.placeholder} onKeyDown={handleKeyDown} onInput={handleFormInput} /> {(triggerRef: (el) => void) => ( )} {(triggerRef: (el) => void) => ( )}
{formValueError()}
) }