Skip to content

Commit

Permalink
fix(machine/combobox): use value context with clearTriggerProps h…
Browse files Browse the repository at this point in the history
…idden check (chakra-ui#1324)

fix(machine/combobox): initialize default value with `clearTriggerProps` check
  • Loading branch information
TylerAPfledderer authored and colinlienard committed Jun 18, 2024
1 parent f64824d commit 823c3c1
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/stale-waves-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@zag-js/combobox": patch
---

Fixes issue where on load -- if the user set initial `value` to context -- `value` should be used to check whether a
trigger button using `clearTriggerProps` should be visible on the page.
1 change: 1 addition & 0 deletions examples/next-ts/pages/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default function Page() {
<button data-testid="clear-value-button" onClick={() => api.clearValue()}>
Clear Value
</button>
<button {...api.clearTriggerProps}>Clear Trigger</button>
<br />
<div {...api.rootProps}>
<label {...api.labelProps}>Select country</label>
Expand Down
2 changes: 1 addition & 1 deletion examples/nuxt-ts/pages/combobox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const api = computed(() => combobox.connect(state.value, send, normalizeProps))
<div>
<button @click="() => api.setValue(['TG'])">Set to Togo</button>
<button data-testid="clear-value-button" @click="api.clearValue">Clear Value</button>

<button v-bind="api.clearTriggerProps">Clear Trigger</button>
<br />

<div v-bind="api.rootProps">
Expand Down
1 change: 1 addition & 0 deletions examples/solid-ts/src/pages/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function Page() {
<button data-testid="clear-value-button" onClick={() => api().clearValue()}>
Clear Value
</button>
<button {...api().clearTriggerProps}>Clear Trigger</button>
<br />

<div {...api().rootProps}>
Expand Down
1 change: 1 addition & 0 deletions examples/vue-ts/src/pages/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default defineComponent({
<button data-testid="clear-value-button" onClick={() => api.clearValue()}>
Clear Value
</button>
<button {...api.clearTriggerProps}>Clear Trigger</button>

<br />

Expand Down
2 changes: 1 addition & 1 deletion packages/machines/combobox/src/combobox.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export function connect<T extends PropTypes, V extends CollectionItem>(
tabIndex: -1,
disabled: isDisabled,
"aria-label": translations.clearTriggerLabel,
hidden: state.context.isInputValueEmpty,
hidden: !state.context.value.length,
onPointerDown(event) {
const evt = getNativeEvent(event)
if (!isInteractive || !isLeftClick(evt)) return
Expand Down
2 changes: 0 additions & 2 deletions packages/machines/combobox/src/combobox.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
...ctx.translations,
},
},

computed: {
isInputValueEmpty: (ctx) => ctx.inputValue.length === 0,
isInteractive: (ctx) => !(ctx.readOnly || ctx.disabled),
Expand Down Expand Up @@ -633,7 +632,6 @@ const set = {
invoke.selectionChange(ctx)
return
}

ctx.value = ctx.multiple ? addOrRemove(ctx.value, value!) : [value!]
invoke.selectionChange(ctx)
},
Expand Down

0 comments on commit 823c3c1

Please sign in to comment.