Skip to content

Commit 4549d71

Browse files
authored
feat(Select): add nullableValue prop (#2641)
1 parent 5ba6908 commit 4549d71

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

docs/content/meta/SelectRoot.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
'type': 'AcceptableValue | AcceptableValue[]',
4545
'required': false
4646
},
47+
{
48+
'name': 'nullableValue',
49+
'description': '<p>The value of the hidden native select option when the model value is nullish.</p>\n',
50+
'type': 'string',
51+
'required': false,
52+
'default': '\'\''
53+
},
4754
{
4855
'name': 'multiple',
4956
'description': '<p>Whether multiple options can be selected or not.</p>\n',
@@ -110,6 +117,7 @@
110117
| `dir` | The reading direction of the combobox when applicable. <br> If omitted, inherits globally from ConfigProvider or assumes LTR (left-to-right) reading mode. | `"ltr" \| "rtl"` | No | - |
111118
| `disabled` | When true, prevents the user from interacting with Select | `boolean` | No | - |
112119
| `modelValue` | The controlled value of the Select. Can be bind as v-model. | `AcceptableValue \| AcceptableValue[]` | No | - |
120+
| `nullableValue` | The value of the hidden native select option when the model value is nullish. | `string` | No | `""` |
113121
| `multiple` | Whether multiple options can be selected or not. | `boolean` | No | - |
114122
| `name` | The name of the field. Submitted with its owning form as part of a name/value pair. | `string` | No | - |
115123
| `open` | The controlled open state of the Select. Can be bind as v-model:open. | `boolean` | No | - |

packages/core/src/Select/Select.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,18 @@ describe('given Select in a form', async () => {
295295
expect(wrapper.find('select').exists()).toBe(true)
296296
})
297297

298+
it('should use the nullableValue for the hidden select when the value is nullish', async () => {
299+
const wrapper = mount({
300+
components: { Select },
301+
template: '<form><Select name="test" nullable-value="null" /></form>',
302+
}, {
303+
attachTo: document.body,
304+
})
305+
306+
const options = wrapper.findAll('select option')
307+
expect((options[0].element as HTMLOptionElement).value).toBe('null')
308+
})
309+
298310
describe('after selecting option and clicking submit button', () => {
299311
beforeEach(async () => {
300312
await wrapper.find('button').trigger('pointerdown', {

packages/core/src/Select/SelectRoot.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface SelectRootProps<T = AcceptableValue> extends FormFieldProps {
1414
defaultValue?: T | Array<T>
1515
/** The controlled value of the Select. Can be bind as `v-model`. */
1616
modelValue?: T | Array<T>
17+
/** The value of the hidden native select option when the model value is nullish. */
18+
nullableValue?: string
1719
/** Use this to compare objects by a particular field, or pass your own comparison function for complete control over how objects are compared. */
1820
by?: string | ((a: T, b: T) => boolean)
1921
/** The reading direction of the combobox when applicable. <br> If omitted, inherits globally from `ConfigProvider` or assumes LTR (left-to-right) reading mode. */
@@ -75,6 +77,7 @@ defineOptions({
7577
const props = withDefaults(defineProps<SelectRootProps<T>>(), {
7678
modelValue: undefined,
7779
open: undefined,
80+
nullableValue: '',
7881
})
7982
const emits = defineEmits<SelectRootEmits<T>>()
8083
@@ -214,7 +217,7 @@ provideSelectRootContext({
214217
>
215218
<option
216219
v-if="isNullish(modelValue)"
217-
value=""
220+
:value="nullableValue"
218221
/>
219222
<option
220223
v-for="option in Array.from(optionsSet)"

0 commit comments

Comments
 (0)