Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/vuetify/src/components/VSelect/VSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { VVirtualScroll } from '@/components/VVirtualScroll'

// Composables
import { useScrolling } from './useScrolling'
import { useAutocomplete } from '@/composables/autocomplete'
import { useForm } from '@/composables/form'
import { forwardRefs } from '@/composables/forwardRefs'
import { IconValue } from '@/composables/icons'
Expand Down Expand Up @@ -160,6 +161,7 @@ export const VSelect = genericComponent<new <
: model.value.length
})
const form = useForm(props)
const autocomplete = useAutocomplete(props)
const selectedValues = computed(() => model.value.map(selection => selection.value))
const isFocused = shallowRef(false)

Expand Down Expand Up @@ -387,7 +389,8 @@ export const VSelect = genericComponent<new <
<VTextField
ref={ vTextFieldRef }
{ ...textFieldProps }
modelValue={ model.value.map(v => v.props.value).join(', ') }
modelValue={ model.value.map(v => v.props.title).join(', ') }
name={ undefined }
onUpdate:modelValue={ onModelUpdate }
v-model:focused={ isFocused.value }
validationValue={ model.externalValue }
Expand Down Expand Up @@ -420,6 +423,20 @@ export const VSelect = genericComponent<new <
...slots,
default: () => (
<>
<select
hidden
multiple={ props.multiple }
name={ autocomplete.fieldName.value }
>
{ items.value.map(item => (
<option
key={ item.value }
value={ item.value }
selected={ selectedValues.value.includes(item.value) }
/>
))}
</select>

<VMenu
id={ menuId.value }
ref={ vMenuRef }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,70 @@ describe('VSelect', () => {
expect(screen.getAllByCSS('.v-checkbox-btn input:checked')).toHaveLength(1)
})

describe('native form submission', () => {
const items = [
{ title: 'Item 1', value: 1 },
{ title: 'Item 2', value: 2 },
{ title: 'Item 3', value: 3 },
]

it('should include selected value in form data for single selection', async () => {
let submittedData: FormData | null = null

render(() => (
<form
onSubmit={ e => {
e.preventDefault()
submittedData = new FormData(e.target as HTMLFormElement)
}}
>
<VSelect
name="select"
items={ items }
modelValue={ items[0] }
/>
<button type="submit">Submit</button>
</form>
))

const submitButton = screen.getByRole('button', { name: 'Submit' })
await userEvent.click(submitButton)

expect(submittedData).not.toBeNull()
expect(submittedData!.get('select')).toBe('1')
})

it('should include selected values in form data for multiple selection', async () => {
let submittedData: FormData | null = null

render(() => (
<form
onSubmit={ e => {
e.preventDefault()
submittedData = new FormData(e.target as HTMLFormElement)
}}
>
<VSelect
multiple
name="select"
items={ items }
modelValue={[items[0], items[1]]}
/>
<button type="submit">Submit</button>
</form>
))

const submitButton = screen.getByRole('button', { name: 'Submit' })
await userEvent.click(submitButton)

expect(submittedData).not.toBeNull()
const select = submittedData!.getAll('select')
expect(select).toHaveLength(2)
expect(select).toContain('1')
expect(select).toContain('2')
})
})

describe('Showcase', () => {
generate({ stories })
})
Expand Down
2 changes: 2 additions & 0 deletions packages/vuetify/src/composables/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function useAutocomplete (props: InputAutocompleteProps) {
const isSuppressing = toRef(() => props.autocomplete === 'suppress')

const fieldName = toRef(() => {
if (!props.name) return undefined

return isSuppressing.value
? `${props.name}-${uniqueId}-${reloadTrigger.value}`
: props.name
Expand Down