Skip to content

Commit

Permalink
Fix switching between KCL arguments (#5512)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2025
1 parent 1104d90 commit da236d9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 20 additions & 3 deletions src/components/CommandBar/CommandBarKclInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down Expand Up @@ -105,7 +107,7 @@ function CommandBarKclInput({
}))
const varMentionsExtension = varMentions(varMentionData)

const { setContainer } = useCodeMirror({
const { setContainer, view } = useCodeMirror({
container: editorRef.current,
initialDocValue: value,
autoFocus: true,
Expand Down Expand Up @@ -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])

Expand Down

0 comments on commit da236d9

Please sign in to comment.