Skip to content

Commit 97e54ee

Browse files
authored
feat: add prop to not reset searchTerm (unovue#895)
* feat: add prop to not reset searchTerm * refactor: renaming prop name
1 parent 91e4148 commit 97e54ee

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/radix-vue/src/Combobox/Combobox.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ describe('given default Combobox', () => {
5656
expect(wrapper.html()).toContain('Apple')
5757
})
5858

59+
it('should reset searchTerm when close', async () => {
60+
const input = wrapper.find('input')
61+
input.element.value = 'Testing'
62+
await input.trigger('keydown', { key: 'Escape' })
63+
expect(input.element.value).toBe('')
64+
})
65+
66+
it('should not reset searchTerm when close', async () => {
67+
await wrapper.setProps({ resetSearchTermOnBlur: false, key: 'key' })
68+
const input = wrapper.find('input')
69+
input.element.value = 'Testing'
70+
await input.trigger('keydown', { key: 'Escape' })
71+
expect(input.element.value).toBe('Testing')
72+
})
73+
5974
describe('after selecting a value', () => {
6075
beforeEach(async () => {
6176
const selection = wrapper.findAll('[role=option]')[1]

packages/radix-vue/src/Combobox/ComboboxRoot.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export interface ComboboxRootProps<T = AcceptableValue> extends PrimitiveProps {
6565
filterFunction?: (val: ArrayOrWrapped<T>, term: string) => ArrayOrWrapped<T>
6666
/** The display value of input for selected item. Does not work with `multiple`. */
6767
displayValue?: (val: T) => string
68+
/** Whether to reset the searchTerm when the Combobox input blurred
69+
* @defaultValue `true`
70+
*/
71+
resetSearchTermOnBlur?: boolean
6872
}
6973
</script>
7074

@@ -78,6 +82,7 @@ import isEqual from 'fast-deep-equal'
7882
7983
const props = withDefaults(defineProps<ComboboxRootProps<T>>(), {
8084
open: undefined,
85+
resetSearchTermOnBlur: true,
8186
})
8287
const emit = defineEmits<ComboboxRootEmits<T>>()
8388
@@ -128,7 +133,8 @@ async function onOpenChange(val: boolean) {
128133
}
129134
else {
130135
isUserInputted.value = false
131-
resetSearchTerm()
136+
if (props.resetSearchTermOnBlur)
137+
resetSearchTerm()
132138
}
133139
}
134140

0 commit comments

Comments
 (0)