Skip to content

Commit

Permalink
chore: update tags input
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Aug 16, 2023
1 parent a38d8e3 commit 23085e6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/hip-papayas-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@zag-js/tags-input": minor
---

- Rename `onHighlight` to `onFocusChange`
- Remove `onTagUpdate`, use `onChange` instead
2 changes: 1 addition & 1 deletion .xstate/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const fetchMachine = createMachine({
}],
TAG_INPUT_ENTER: {
target: "navigating:tag",
actions: ["submitEditedTagValue", "focusInput", "clearEditedId", "focusTagAtIndex", "invokeOnTagUpdate"]
actions: ["submitEditedTagValue", "focusInput", "clearEditedId", "focusTagAtIndex"]
}
}
}
Expand Down
20 changes: 3 additions & 17 deletions packages/machines/tags-input/src/tags-input.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export function machine(userContext: UserDefinedContext) {
],
TAG_INPUT_ENTER: {
target: "navigating:tag",
actions: ["submitEditedTagValue", "focusInput", "clearEditedId", "focusTagAtIndex", "invokeOnTagUpdate"],
actions: ["submitEditedTagValue", "focusInput", "clearEditedId", "focusTagAtIndex"],
},
},
},
Expand Down Expand Up @@ -301,15 +301,6 @@ export function machine(userContext: UserDefinedContext) {
raiseExternalBlurEvent(_, evt, { self }) {
self.send({ type: "EXTERNAL_BLUR", id: evt.id })
},
invokeOnHighlight(ctx) {
const value = dom.getFocusedTagValue(ctx)
ctx.onHighlight?.({ value })
},
invokeOnTagUpdate(ctx) {
if (ctx.idx == null) return
const value = ctx.value[ctx.idx]
ctx.onTagUpdate?.({ value, index: ctx.idx })
},
dispatchChangeEvent(ctx) {
dom.dispatchInputEvent(ctx)
},
Expand Down Expand Up @@ -546,14 +537,9 @@ const invoke = {
ctx.onChange?.({ values: ctx.value })
dom.dispatchInputEvent(ctx)
},
tagUpdate: (ctx: MachineContext) => {
if (ctx.idx == null) return
ctx.onTagUpdate?.({ value: ctx.value[ctx.idx], index: ctx.idx })
dom.dispatchInputEvent(ctx)
},
focusChange: (ctx: MachineContext) => {
const value = dom.getFocusedTagValue(ctx)
ctx.onHighlight?.({ value })
ctx.onFocusChange?.({ value })
},
}

Expand All @@ -564,7 +550,7 @@ const set = {
},
valueAtIndex: (ctx: MachineContext, index: number, value: string) => {
ctx.value[index] = value
invoke.tagUpdate(ctx)
invoke.change(ctx)
},
focusedId: (ctx: MachineContext, id: string | null) => {
ctx.focusedId = id
Expand Down
6 changes: 1 addition & 5 deletions packages/machines/tags-input/src/tags-input.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@ type PublicContext = DirectionProperty &
/**
* Callback fired when a tag is focused by pointer or keyboard navigation
*/
onHighlight?(details: { value: string | null }): void
onFocusChange?(details: { value: string | null }): void
/**
* Callback fired when the max tag count is reached or the `validateTag` function returns `false`
*/
onInvalid?(details: { reason: ValidityState }): void
/**
* Callback fired when a tag's value is updated
*/
onTagUpdate?(details: { value: string; index: number }): void
/**
* Returns a boolean that determines whether a tag can be added.
* Useful for preventing duplicates or invalid tag values.
Expand Down

0 comments on commit 23085e6

Please sign in to comment.