Skip to content

Commit 62aeefc

Browse files
eirikbackermimarz
authored andcommitted
fix(Select): use field (#2709)
Part of #2311
1 parent 86fe890 commit 62aeefc

File tree

5 files changed

+30
-117
lines changed

5 files changed

+30
-117
lines changed

packages/react/src/components/form/Select/Select.mdx

+10-8
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ For eksempel er det ikke mulig å overstyre formateringen av alternativene.
1818
## Bruk
1919

2020
```tsx
21-
import { Select } from '@digdir/designsystemet-react';
22-
23-
<Label htmlFor='my-select'>Ta et valg</Label>
24-
<Select id='my-select'>
25-
<Select.Option value='1'>Alternativ 1</Select.Option>
26-
<Select.Option value='2'>Alternativ 2</Select.Option>
27-
<Select.Option value='3'>Alternativ 3</Select.Option>
28-
</Select>;
21+
import { Select, Field } from '@digdir/designsystemet-react';
22+
23+
<Field>
24+
<Label>Ta et valg</Label>
25+
<Select>
26+
<Select.Option value='1'>Alternativ 1</Select.Option>
27+
<Select.Option value='2'>Alternativ 2</Select.Option>
28+
<Select.Option value='3'>Alternativ 3</Select.Option>
29+
</Select>
30+
</Field>
2931
```
3032

3133
## Eksempler
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
11
import type { Meta, StoryFn } from '@storybook/react';
22

3-
import { Label } from '../../Label';
4-
import { Select } from './';
3+
import { Field, Label, Select, ValidationMessage } from '../../';
54

65
export default {
76
title: 'Komponenter/Select',
87
component: Select,
98
parameters: {
109
layout: 'padded',
1110
},
12-
decorators: [
13-
(Story) => (
14-
<div
15-
style={{
16-
display: 'flex',
17-
gap: 'var(--ds-spacing-2)',
18-
flexDirection: 'column',
19-
}}
20-
>
21-
<Story />
22-
</div>
23-
),
24-
],
2511
} as Meta;
2612

2713
export const Preview: StoryFn<typeof Select> = (args) => (
28-
<>
29-
<Label htmlFor={args.id}>Velg et fjell</Label>
14+
<Field>
15+
<Label>Velg et fjell</Label>
3016
<Select {...args}>
3117
<Select.Option value='blank'>Velg &hellip;</Select.Option>
3218
<Select.Option value='everest'>Mount Everest</Select.Option>
@@ -38,20 +24,19 @@ export const Preview: StoryFn<typeof Select> = (args) => (
3824
<Select.Option value='puncakjaya'>Puncak Jaya</Select.Option>
3925
<Select.Option value='kosciuszko'>Mount Kosciuszko</Select.Option>
4026
</Select>
41-
</>
27+
</Field>
4228
);
4329

4430
Preview.args = {
4531
'aria-invalid': false,
4632
'data-size': 'md',
4733
disabled: false,
4834
readOnly: false,
49-
id: 'my-select',
5035
};
5136

5237
export const Disabled: StoryFn<typeof Select> = (args) => (
53-
<>
54-
<Label htmlFor={args.id}>Velg et fjell</Label>
38+
<Field>
39+
<Label>Velg et fjell</Label>
5540
<Select {...args}>
5641
<Select.Option value='blank'>Velg &hellip;</Select.Option>
5742
<Select.Option value='everest'>Mount Everest</Select.Option>
@@ -63,17 +48,16 @@ export const Disabled: StoryFn<typeof Select> = (args) => (
6348
<Select.Option value='puncakjaya'>Puncak Jaya</Select.Option>
6449
<Select.Option value='kosciuszko'>Mount Kosciuszko</Select.Option>
6550
</Select>
66-
</>
51+
</Field>
6752
);
6853

6954
Disabled.args = {
7055
disabled: true,
71-
id: 'my-select',
7256
};
7357

7458
export const WithError: StoryFn<typeof Select> = (args) => (
75-
<>
76-
<Label htmlFor={args.id}>Velg et fjell</Label>
59+
<Field>
60+
<Label>Velg et fjell</Label>
7761
<Select {...args}>
7862
<Select.Option value='blank'>Velg &hellip;</Select.Option>
7963
<Select.Option value='everest'>Mount Everest</Select.Option>
@@ -85,17 +69,17 @@ export const WithError: StoryFn<typeof Select> = (args) => (
8569
<Select.Option value='puncakjaya'>Puncak Jaya</Select.Option>
8670
<Select.Option value='kosciuszko'>Mount Kosciuszko</Select.Option>
8771
</Select>
88-
</>
72+
<ValidationMessage>Velg et fjell</ValidationMessage>
73+
</Field>
8974
);
9075

9176
WithError.args = {
9277
'aria-invalid': true,
93-
id: 'my-select',
9478
};
9579

9680
export const WithOptgroup: StoryFn<typeof Select> = (args) => (
97-
<>
98-
<Label htmlFor={args.id}>Velg et fjell</Label>
81+
<Field>
82+
<Label>Velg et fjell</Label>
9983
<Select {...args}>
10084
<Select.Optgroup label='Gruppe 1'>
10185
<Select.Option value='everest'>Mount Everest</Select.Option>
@@ -110,9 +94,5 @@ export const WithOptgroup: StoryFn<typeof Select> = (args) => (
11094
<Select.Option value='kosciuszko'>Mount Kosciuszko</Select.Option>
11195
</Select.Optgroup>
11296
</Select>
113-
</>
97+
</Field>
11498
);
115-
116-
WithOptgroup.args = {
117-
id: 'my-select',
118-
};

packages/react/src/components/form/Select/Select.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export type SelectProps = {
88
* @default false
99
*/
1010
readOnly?: boolean;
11-
/** Defines the width of <Select> in count of characters.
11+
/** Defines the width of Select in count of characters.
1212
*/
1313
size?: number;
14-
} & Omit<SelectHTMLAttributes<HTMLSelectElement>, 'size' | 'multiple'> &
14+
} & Omit<SelectHTMLAttributes<HTMLSelectElement>, 'multiple'> &
1515
DefaultProps;
1616

1717
export const Select = forwardRef<HTMLSelectElement, SelectProps>(

packages/react/src/components/form/Select/index.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import { Select as SelectParent } from './Select';
22
import { SelectOptgroup } from './SelectOptgroup';
33
import { SelectOption } from './SelectOption';
44

5-
type SelectComponent = typeof SelectParent & {
6-
Option: typeof SelectOption;
7-
Optgroup: typeof SelectOptgroup;
8-
};
9-
10-
const Select = SelectParent as SelectComponent;
11-
12-
Select.Option = SelectOption;
13-
Select.Optgroup = SelectOptgroup;
5+
const Select = Object.assign(SelectParent, {
6+
Option: SelectOption,
7+
Optgroup: SelectOptgroup,
8+
});
149

1510
Select.Option.displayName = 'Select.Option';
1611
Select.Optgroup.displayName = 'Select.Optgroup';

packages/react/src/components/form/Select/useSelect.ts

-64
This file was deleted.

0 commit comments

Comments
 (0)