import {createElement, useLayoutEffect, useRef, useState} from 'react'; import './entry.css'; function TextField({ item, onEnter, onSelect, onRemove, ref }) { const onKeyDown = e => { if (e.keyCode === 38) { // up onSelect(-1); e.preventDefault(); return; } else if (e.keyCode === 40) { // down onSelect(+1); e.preventDefault(); return; } else if (e.keyCode === 8 && e.target.innerText.length === 0) { // backspace onRemove(); e.preventDefault(); return; } if (e.keyCode !== 13) { return; } else if (e.shiftKey) { return; } e.preventDefault(); onEnter(e.ctrlKey ? 'todo' : 'plain'); }; useLayoutEffect(() => { if (ref.current == null) { return; } ref.current.innerText = item.text; }, [item.text]); return
onSelect(0)}>
; } function Plain({ item, onEnter, onSelect, onRemove, ref }) { return ; } function Todo({ item, onEnter, onSelect, onRemove, ref }) { return <> ; } export default function Entry({ item, onEnter, onSelect, onRemove, isFocused }) { const ref = useRef(null); useLayoutEffect(() => { if (isFocused && ref.current != null && document.activeElement !== ref.current) { ref.current.focus(); document.execCommand('selectAll', false, null); document.getSelection().collapseToEnd(); } }, [isFocused]); const child = createElement(item.type === 'plain' ? Plain : Todo, { ref, item, onEnter, onSelect, onRemove: () => onRemove(true), }); return
{child}
}