import { ChangeEvent, useRef, useState } from 'react'; import styled from '@emotion/styled'; import { textInputStyle } from '../../layout/styles/themes'; import { EditableCell } from './EditableCell'; type OwnProps = { placeholder?: string; content: string; changeHandler: (updated: string) => void; editModeHorizontalAlign?: 'left' | 'right'; }; type StyledEditModeProps = { isEditMode: boolean; }; // TODO: refactor const StyledInplaceInput = styled.input` width: 100%; ${textInputStyle} `; const StyledNoEditText = styled.div` width: 100%; `; export function EditableText({ content, placeholder, changeHandler, editModeHorizontalAlign, }: OwnProps) { const inputRef = useRef(null); const [inputValue, setInputValue] = useState(content); const [isEditMode, setIsEditMode] = useState(false); return ( setIsEditMode(false)} onInsideClick={() => setIsEditMode(true)} editModeHorizontalAlign={editModeHorizontalAlign} editModeContent={ ) => { setInputValue(event.target.value); changeHandler(event.target.value); }} /> } nonEditModeContent={{inputValue}} > ); }