From 84f7d717758843142cc5490990110c919599192a Mon Sep 17 00:00:00 2001 From: cha0s Date: Sat, 30 Jan 2021 21:39:44 -0600 Subject: [PATCH] feat: add, remove, rename routines --- .../entity/src/trait-components/behaved.jsx | 120 +++++++++++++++++- .../entity/src/trait-components/behaved.scss | 6 + 2 files changed, 119 insertions(+), 7 deletions(-) diff --git a/packages/entity/src/trait-components/behaved.jsx b/packages/entity/src/trait-components/behaved.jsx index fc8ddfe..998870e 100644 --- a/packages/entity/src/trait-components/behaved.jsx +++ b/packages/entity/src/trait-components/behaved.jsx @@ -1,6 +1,13 @@ import {join} from 'path'; -import {PropTypes, React} from '@latus/react'; +import { + PropTypes, + React, + useEffect, + useRef, + useState, +} from '@latus/react'; +import {useJsonPatcher} from '@persea/json'; import { Tab, Tabs, @@ -15,12 +22,69 @@ const Behaved = ({ json, path, }) => { + const patch = useJsonPatcher(); + const editingRef = useRef(); + useEffect(() => { + editingRef.current?.focus(); + }); + const [isEditing, setIsEditing] = useState(false); const tabs = []; const tabPanels = []; Object.entries(json.params.routines).forEach(([key, expressions]) => { + const routinePath = join(path, 'params/routines', key); tabs.push( - - {key} + { + if (false === isEditing) { + setTimeout(() => { + editingRef.current?.select(); + }, 10); + } + setIsEditing(key); + }} + > + { + key === isEditing + ? ( + { + setIsEditing(false); + }} + onChange={({target: {value}}) => { + patch({ + op: 'move', + from: routinePath, + path: join(path, 'params/routines', value), + }); + setIsEditing(value); + }} + onKeyPress={({key}) => { + if ('Enter' === key) { + setIsEditing(false); + } + }} + ref={editingRef} + /> + ) + : key + } +   + + , ); tabPanels.push( @@ -28,7 +92,7 @@ const Behaved = ({ , ); @@ -38,9 +102,51 @@ const Behaved = ({
Routines
- - {tabs} - +
+ + + {tabs} + +
{tabPanels}
diff --git a/packages/entity/src/trait-components/behaved.scss b/packages/entity/src/trait-components/behaved.scss index a2e283d..1dd1a66 100644 --- a/packages/entity/src/trait-components/behaved.scss +++ b/packages/entity/src/trait-components/behaved.scss @@ -1,3 +1,9 @@ +.behaved__routines-tab-bar { + display: flex; + margin-bottom: 1em; + overflow: auto; +} + .behaved__routines-label[class] { flex-wrap: nowrap; > .react-tabs {