Skip to content

Commit

Permalink
fix(react): <Editable /> sync internal value and external value (#883)
Browse files Browse the repository at this point in the history
Co-authored-by: Segun Adebayo <[email protected]>
  • Loading branch information
junghyeonsu and segunadebayo authored Sep 21, 2023
1 parent fccc9de commit 53a2789
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-monkeys-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/editable": patch
---

Fix issue where the input value doesn't get synced correctly when controlled
17 changes: 15 additions & 2 deletions packages/machines/editable/src/editable.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ export function machine(userContext: UserDefinedContext) {
ctx.onEdit?.()
},
syncInputValue(ctx) {
const inputEl = dom.getInputEl(ctx)
dom.setValue(inputEl, ctx.value)
sync.value(ctx)
},
setValue(ctx, evt) {
set.value(ctx, evt.value)
Expand All @@ -184,6 +183,13 @@ export function machine(userContext: UserDefinedContext) {
)
}

const sync = {
value: (ctx: MachineContext) => {
const inputEl = dom.getInputEl(ctx)
dom.setValue(inputEl, ctx.value)
},
}

const invoke = {
change(ctx: MachineContext) {
ctx.onValueChange?.({ value: ctx.value })
Expand All @@ -195,5 +201,12 @@ const set = {
if (isEqual(ctx.value, value)) return
ctx.value = value
invoke.change(ctx)

/**
* sync when the value(ctx.value) entered from outside and the internal value(value) are different after invoke.
*/
if (value !== ctx.value) {
sync.value(ctx)
}
},
}

4 comments on commit 53a2789

@vercel
Copy link

@vercel vercel bot commented on 53a2789 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-nextjs – ./examples/next-ts

zag-nextjs-chakra-ui.vercel.app
zag-two.vercel.app
zag-nextjs-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 53a2789 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-vue – ./examples/vue-ts

zag-vue-chakra-ui.vercel.app
zag-vue.vercel.app
zag-vue-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 53a2789 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-solid – ./examples/solid-ts

zag-solid-git-main-chakra-ui.vercel.app
zag-solid.vercel.app
zag-solid-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 53a2789 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.