Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 1a26480

Browse files
committed
feat: select now accepts children instead of options
Closes #60
1 parent c226981 commit 1a26480

File tree

2 files changed

+76
-79
lines changed

2 files changed

+76
-79
lines changed

docs/components/Select.mdx

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,29 @@ import { Select } from '@smooth-ui/core-sc'
99

1010
# Select
1111

12+
```js
13+
import { Select } from '@smooth-ui/core-sc'
14+
```
15+
1216
## Sizes
1317

1418
Set sizes using `size` prop like `"sm"`, `"md"` or `"lg"`.
1519

1620
<Playground>
1721
<BlockList>
18-
<Select size="sm" placeholder="Small" options={['One', 'Two']} />
19-
<Select size="md" placeholder="Medium" options={['Yes', 'No', 'Maybe']} />
20-
<Select size="lg" placeholder="Large" options={['Amazing', 'Great']} />
22+
<Select size="sm" placeholder="Small">
23+
<option value="one">One</option>
24+
<option value="two">Two</option>
25+
</Select>
26+
<Select size="md" placeholder="Medium">
27+
<option value="yes">Yes</option>
28+
<option value="no">No</option>
29+
<option value="maybe">Maybe</option>
30+
</Select>
31+
<Select size="lg" placeholder="Large">
32+
<option value="amazing">Amazing</option>
33+
<option value="great">Great</option>
34+
</Select>
2135
</BlockList>
2236
</Playground>
2337

@@ -26,39 +40,24 @@ Set sizes using `size` prop like `"sm"`, `"md"` or `"lg"`.
2640
Disable using `disabled` prop.
2741

2842
<Playground>
29-
<Select disabled placeholder="Disabled" options={['Disabled']} />
30-
</Playground>
31-
32-
## Custom labels
33-
34-
Specify custom labels by passing an object to `options`.
35-
36-
<Playground>
37-
<Select
38-
options={[{ label: 'One', value: 'one' }, { label: 'Two', value: 'two' }]}
39-
/>
43+
<Select disabled placeholder="Disabled">
44+
<option>Disabled</option>
45+
</Select>
4046
</Playground>
4147

4248
## Groups
4349

44-
Define groups in `options` props.
45-
4650
<Playground>
47-
<Select
48-
options={[
49-
{
50-
label: 'Group 1',
51-
options: [{ label: 'One', value: 'one' }, { label: 'Two', value: 'two' }],
52-
},
53-
{
54-
label: 'Group 2',
55-
options: [
56-
{ label: 'Three', value: 'three' },
57-
{ label: 'Four', value: 'four' },
58-
],
59-
},
60-
]}
61-
/>
51+
<Select>
52+
<optgroup label="Group 1">
53+
<option value="one">One</option>
54+
<option value="two">Two</option>
55+
</optgroup>
56+
<optgroup label="Group 2">
57+
<option value="three">Three</option>
58+
<option value="four">Four</option>
59+
</optgroup>
60+
</Select>
6261
</Playground>
6362

6463
## Control & Validation
@@ -68,29 +67,18 @@ Define groups in `options` props.
6867

6968
<Playground>
7069
<BlockList>
71-
<Select
72-
control
73-
options={[
74-
{ label: 'Control', value: 'one' },
75-
{ label: 'Two', value: 'two' },
76-
]}
77-
/>
78-
<Select
79-
control
80-
valid
81-
options={[
82-
{ label: 'Valid', value: 'one' },
83-
{ label: 'Two', value: 'two' },
84-
]}
85-
/>
86-
<Select
87-
control
88-
valid={false}
89-
options={[
90-
{ label: 'Invalid', value: 'one' },
91-
{ label: 'Two', value: 'two' },
92-
]}
93-
/>
70+
<Select control>
71+
<option>Control</option>
72+
<option>Other</option>
73+
</Select>
74+
<Select control valid>
75+
<option>Valid</option>
76+
<option>Other</option>
77+
</Select>
78+
<Select control valid={false}>
79+
<option>Invalid</option>
80+
<option>Other</option>
81+
</Select>
9482
</BlockList>
9583
</Playground>
9684

@@ -103,7 +91,6 @@ Define groups in `options` props.
10391
arrow: PropDesc.bool,
10492
children: PropDesc.node,
10593
control: PropDesc.bool,
106-
options: PropDesc.array,
10794
size: PropDesc.oneOf(['sm', 'lg']),
10895
valid: PropDesc.bool,
10996
...getSystemPropDesc(Select),

packages/shared/core/Select.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,31 +114,41 @@ const Select = createComponent(() => ({
114114
options,
115115
size,
116116
valid,
117+
children,
117118
...props
118-
}) => (
119-
<Component className={className}>
120-
{arrow && !props.multiple ? (
121-
<svg className="sui-select-arrow" viewBox="0 0 10 5">
122-
<g fill="none" fillRule="evenodd">
123-
<path d="M17 14H-7v-24h24" />
124-
<path fill="currentColor" opacity={0.5} d="M0 0l5 5 5-5" />
125-
</g>
126-
</svg>
127-
) : null}
128-
<select {...props}>
129-
{options.map(
130-
node =>
131-
node.options ? (
132-
<optgroup key={node.label} label={node.label}>
133-
{node.options.map(renderOption)}
134-
</optgroup>
135-
) : (
136-
renderOption(node)
137-
),
138-
)}
139-
</select>
140-
</Component>
141-
),
119+
}) => {
120+
if (options) {
121+
// eslint-disable-next-line no-console
122+
console.warn(
123+
'Smooth UI: `options` prop of "Select" is deprecated and will be removed in v7, use <option> instead.',
124+
)
125+
}
126+
return (
127+
<Component className={className}>
128+
{arrow && !props.multiple ? (
129+
<svg className="sui-select-arrow" viewBox="0 0 10 5">
130+
<g fill="none" fillRule="evenodd">
131+
<path d="M17 14H-7v-24h24" />
132+
<path fill="currentColor" opacity={0.5} d="M0 0l5 5 5-5" />
133+
</g>
134+
</svg>
135+
) : null}
136+
<select {...props}>
137+
{children ||
138+
options.map(
139+
node =>
140+
node.options ? (
141+
<optgroup key={node.label} label={node.label}>
142+
{node.options.map(renderOption)}
143+
</optgroup>
144+
) : (
145+
renderOption(node)
146+
),
147+
)}
148+
</select>
149+
</Component>
150+
)
151+
},
142152
style: css`
143153
display: inline-block;
144154
position: relative;

0 commit comments

Comments
 (0)