Fix switching between KCL arguments (#5512)

* Fix switching between KCL arguments

Needed to imperatively send a transaction to the editor, since it
doesn't get re-rendered when switching from one KCL-type arg to another.

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Pierre Jacquier <pierre@zoo.dev>
This commit is contained in:
Frank Noirot
2025-02-27 05:01:08 -05:00
committed by GitHub
parent 1104d908c0
commit da236d9a3e
5 changed files with 20 additions and 3 deletions

View File

@ -74,9 +74,11 @@ function CommandBarKclInput({
arg.name,
previouslySetValue,
])
const [value, setValue] = useState(
previouslySetValue?.valueText || defaultValue || ''
const initialValue = useMemo(
() => previouslySetValue?.valueText || defaultValue || '',
[previouslySetValue, defaultValue]
)
const [value, setValue] = useState(initialValue)
const [createNewVariable, setCreateNewVariable] = useState(
(previouslySetValue && 'variableName' in previouslySetValue) ||
arg.createVariableByDefault ||
@ -105,7 +107,7 @@ function CommandBarKclInput({
}))
const varMentionsExtension = varMentions(varMentionData)
const { setContainer } = useCodeMirror({
const { setContainer, view } = useCodeMirror({
container: editorRef.current,
initialDocValue: value,
autoFocus: true,
@ -161,6 +163,21 @@ function CommandBarKclInput({
useEffect(() => {
if (editorRef.current) {
setContainer(editorRef.current)
// Reset the value when the arg changes and
// the new arg is also a KCL type, since the component
// sticks around.
view?.focus()
view?.dispatch({
changes: {
from: 0,
to: view.state.doc.length,
insert: initialValue,
},
selection: {
anchor: 0,
head: initialValue.length,
},
})
}
}, [arg, editorRef])