Skip to content

Commit

Permalink
feat(Select): Add Select.name for convenient form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbitronics committed Apr 11, 2024
1 parent 3d8f95d commit 8f7380b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions components/mdc/Select/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export let selectedID = ''
export let required = false
/** @type {boolean} applies the mdc-select--invalid class */
export let showError = false
/** @type {string} a name for the hidden input field for form submission*/
export let name = ''
const dispatch = createEventDispatcher()
const labelID = generateRandomID('select-label-')
Expand Down Expand Up @@ -72,6 +74,7 @@ afterUpdate(() => {
bind:this={element}
style="width: {width}"
>
<input {required} type="hidden" {name} />
<div
class="mdc-select__anchor"
aria-required={required}
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ declare module '@silintl/ui-components' {
interface SelectProps {
disabled?: boolean
label?: string
name?: string
options?: { name: string; id: string }[]
required?: boolean
showError?: boolean
Expand Down
14 changes: 12 additions & 2 deletions stories/Select.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script>
import { Meta, Template, Story } from '@storybook/addon-svelte-csf'
import { Select } from '../components/mdc'
import { Button, Select, Snackbar, setNotice } from '../components/mdc'
import { copyAndModifyArgs } from './helpers.js'
import { Form } from '../components/custom'
const args = {
options: [
Expand All @@ -21,7 +22,14 @@ const args = {
<Meta title="Atoms/Select" component={Select} />

<Template let:args>
<Select {...args} on:change={args['on:change']} on:populated={args['on:populated']} />
<Form on:submit={(e) => setNotice('submitted')}>
<p>
<Select {...args} on:change={args['on:change']} on:populated={args['on:populated']} />
</p>
<Button raised>Submit</Button>
</Form>

<Snackbar />
</Template>

<Story name="Default" {args} />
Expand All @@ -37,3 +45,5 @@ const args = {
<Story name="required" args={copyAndModifyArgs(args, { required: true })} />

<Story name="showError" args={copyAndModifyArgs(args, { showError: true })} />

<Story name="name" args={copyAndModifyArgs(args, { name: 'category' })} />

0 comments on commit 8f7380b

Please sign in to comment.