Skip to content

Commit 94bab91

Browse files
committed
feat(Select): Add Select.required prop
1 parent 23d2e78 commit 94bab91

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

components/mdc/Select/Select.svelte

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export let width = '280px'
1515
export let disabled = false
1616
/** @type {string} The ID of the selected option. */
1717
export let selectedID = ''
18+
/** @type {boolean} makes a selection required and adds invalid class when none selected */
19+
export let required = false
1820
1921
const dispatch = createEventDispatcher()
2022
const labelID = generateRandomID('select-label-')
@@ -61,8 +63,18 @@ afterUpdate(() => {
6163
})
6264
</script>
6365

64-
<div class="mdc-select mdc-select--outlined {$$props.class || ''}" bind:this={element} style="width: {width}">
65-
<div class="mdc-select__anchor" role="button" aria-haspopup="listbox" aria-labelledby="{labelID} {selectedTextID}">
66+
<div
67+
class="mdc-select mdc-select--outlined {$$props.class || ''}"
68+
class:mdc-select--required={required}
69+
bind:this={element}
70+
style="width: {width}"
71+
>
72+
<div
73+
class="mdc-select__anchor"
74+
aria-required={required}
75+
aria-haspopup="listbox"
76+
aria-labelledby="{labelID} {selectedTextID}"
77+
>
6678
<span class="mdc-notched-outline">
6779
<span class="mdc-notched-outline__leading" />
6880
<span class="mdc-notched-outline__notch">

components/mdc/Select/_index.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
@use '@material/select/styles';
55

66
/* MDC select floating label */
7-
.mdc-select:not(.mdc-select--disabled).mdc-select--focused .mdc-floating-label {
7+
.mdc-select:not(.mdc-select--disabled, .mdc-select--invalid).mdc-select--focused .mdc-floating-label {
88
color: var(--mdc-theme-primary);
99
}

index.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,12 @@ declare module '@silintl/ui-components' {
235235
}
236236

237237
interface SelectProps {
238+
disabled?: boolean
238239
label?: string
239240
options?: { name: string; id: string }[]
240-
width?: string
241-
disabled?: boolean
241+
required?: boolean
242242
selectedID?: string
243+
width?: string
243244
class?: string
244245
}
245246
export class Select extends SvelteComponentTyped<SelectProps> {}

stories/Select.stories.svelte

+4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ const args = {
2626

2727
<Story name="Default" {args} />
2828

29+
<Story name="selectedID" args={copyAndModifyArgs(args, { selectedID: '5' })} />
30+
2931
<Story name="Label" args={copyAndModifyArgs(args, { label: 'Label' })} />
3032

3133
<Story name="Width" args={copyAndModifyArgs(args, { width: '560px' })} />
3234

3335
<Story name="Disabled" args={copyAndModifyArgs(args, { disabled: true })} />
36+
37+
<Story name="required" args={copyAndModifyArgs(args, { required: true })} />

0 commit comments

Comments
 (0)