import { Atom, useAtom } from "jotai"; import { useState } from "react"; import styled from "styled-components"; export interface CanBeConvertedToString { toString(): string; } export interface Props { valueAtom: Atom; formatter?: (arg: T) => string; inputType?: string; validator: (arg: string) => T | null; } const ClickableContainer = styled.span` display: inline-block; padding: 4px 10px; margin: 4px; border: 1px solid #eee; border-radius: 5px; &:hover { background-color: #eee; } `; const EditingBox = styled.input` display: inline-block; padding: 4px 10px; margin: 4px; border: 1px solid #eee; border-radius: 5px; `; export default function EditBox({ valueAtom, formatter, inputType, validator, }: Props) { const [value, setValue] = useAtom(valueAtom); const [valueInput, setValueInput] = useState(""); const [editing, setEditing] = useState(false); const startEditing = (_: any) => { setValueInput(value.toString()); setEditing(true); }; const finalize = (e: Event) => { e.preventDefault(); const validateResult = validator(valueInput); if (validateResult !== null) { setValue(validateResult); setEditing(false); } }; if (editing) { return (
setValueInput(e.target.value)} /> ); } else { return ( {formatter ? formatter(value) : value.toString()} ); } }