import React from "react"; import type { ReactHTMLProps } from "@lib/typings"; import { Form, Input, InputRef } from "antd"; import { twMerge } from "tailwind-merge"; import type { SizeType } from "antd/lib/config-provider/SizeContext"; import type { Rule } from "rc-field-form/lib/interface"; export interface EditableTextProps extends Omit, 'onChange' | 'children'> { content?: string rootClassName?: string inputClassName?: string inputWidth?: string | number | undefined inputSize?: SizeType rules?: Rule[] onChange?: (val: string) => void disabled?: boolean } export default function EditableText(props: EditableTextProps) { const { rootClassName, disabled, inputClassName, rules, inputSize, inputWidth, onChange, content, ...rest } = props const [ editMode, setEditMode ] = React.useState(false) const inputRef = React.useRef(null) const [ val, setVal ] = React.useState(content) React.useEffect(() => { const { input } = inputRef.current || {} if (input) { input.value = val || '' } }, [ val ]) const [ form ] = Form.useForm() return (
{val} setEditMode(true)} />
{ setEditMode(false) const newVal = inputRef.current?.input?.value || '' onChange && onChange(newVal) setVal(newVal) }} > .ant-row>.ant-col>div>.ant-form-item-explain]:hidden'} > { if (evt.key === 'Enter') { form.submit() } }} />
) }