refactor: routine key editing

This commit is contained in:
cha0s 2021-01-31 12:45:25 -06:00
parent a13a955931
commit 058267f4ce

View File

@ -29,6 +29,7 @@ const Behaved = ({
editingRef.current?.focus(); editingRef.current?.focus();
}); });
const [isEditing, setIsEditing] = useState(false); const [isEditing, setIsEditing] = useState(false);
const [previousKey, setPreviousKey] = useState(false);
const previousEditing = usePrevious(isEditing); const previousEditing = usePrevious(isEditing);
useEffect(() => { useEffect(() => {
setTimeout(() => { setTimeout(() => {
@ -41,33 +42,39 @@ const Behaved = ({
const tabPanels = []; const tabPanels = [];
Object.entries(json.params.routines).forEach(([key, expressions]) => { Object.entries(json.params.routines).forEach(([key, expressions]) => {
const routinePath = join(path, 'params/routines', key); const routinePath = join(path, 'params/routines', key);
const confirmEdit = () => {
if (isEditing && previousKey !== isEditing) {
patch({
op: 'move',
from: join(path, 'params/routines', previousKey),
path: join(path, 'params/routines', isEditing),
});
}
setIsEditing(false);
setPreviousKey(false);
};
tabs.push( tabs.push(
<Tab <Tab
key={key} key={key}
onDoubleClick={() => { onDoubleClick={() => {
setPreviousKey(key);
setIsEditing(key); setIsEditing(key);
}} }}
> >
{ {
key === isEditing key === previousKey
? ( ? (
<input <input
type="text" type="text"
value={key} value={isEditing}
onBlur={() => { onBlur={confirmEdit}
setIsEditing(false);
}}
onChange={({target: {value}}) => { onChange={({target: {value}}) => {
patch({ setPreviousKey(key);
op: 'move',
from: routinePath,
path: join(path, 'params/routines', value),
});
setIsEditing(value); setIsEditing(value);
}} }}
onKeyPress={({key}) => { onKeyPress={({key}) => {
if ('Enter' === key) { if ('Enter' === key) {
setIsEditing(false); confirmEdit();
} }
}} }}
ref={editingRef} ref={editingRef}
@ -124,6 +131,7 @@ const Behaved = ({
}, },
}); });
setIsEditing(name); setIsEditing(name);
setPreviousKey(name);
}} }}
type="button" type="button"
> >