import { FunctionalComponent, h, Component, ComponentChild, Fragment } from "preact"; import * as style from "./style.module.css"; import { useState, useEffect } from "preact/hooks"; export interface EditableTextProps { value: string class?: string editIconClass?: string onChange?: (value: string) => void render?: (value: string) => ComponentChild } const EditableText: FunctionalComponent = ({ onChange, value, render, ...props }) => { const [ internalValue, setInternalValue ] = useState(value); const [ editMode, setEditMode ] = useState(false); useEffect(() => { if (onChange) onChange(internalValue); }, [internalValue]); const onEditIconClick = () => { setEditMode(true); }; const onValidateButtonClick = () => { setEditMode(false); } const onValueChange = (evt: Event) => { const currentTarget = evt.currentTarget as HTMLInputElement; setInternalValue(currentTarget.value); }; return (
{ editMode ? : { render ? render(internalValue) : {internalValue} } 🖋️ }
); }; export default EditableText;