Skip to content

Commit 05358f5

Browse files
committed
docs(form): Add simple examples for Select/NativeSelect
Relates to #1396
1 parent ec7de5d commit 05358f5

File tree

7 files changed

+113
-47
lines changed

7 files changed

+113
-47
lines changed
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
The `NativeSelect` component is a simple wrapper for the `<select>` element with
2-
`TextField` styles. Just like native `<select>` elements, this wrapper **does
3-
not support**:
4-
5-
- placeholder text
6-
- enabling `readOnly` (it can almost manually be done by disabiling each option
7-
yourself, but it'll make it impossible to close on mobile devices if it there
8-
are so many options that it covers the entire viewport)
9-
10-
That being said, the demo below will show you some patterns you can use to fake
11-
placeholder text using the floating label and an empty `<option>` element as
12-
well as a read-only view by disabling all `<option>`s.
1+
This demo below will show you some patterns you can use to fake placeholder text
2+
using the floating label and an empty `<option>` element as well as a read-only
3+
view by disabling all `<option>`s.
Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,2 @@
1-
The `Select` component is a custom widget that allows you to have additional
2-
styling controls for a native `<select>` element while still being accessible.
3-
This component inherits all the `TextField` styles just like the `NativeSelect`,
4-
but also allows each `option` to be rendered like a `ListItem` from the #list
5-
and #menu packages.
6-
7-
> Note: Even though the `Select` component supports the `inline` prop, it does
8-
> not behave the same was as the `NativeSelect` or a native `<select>` component
9-
> since it will not automatically update it's width to be the longest renderable
10-
> option. The size will update whenever the value of the select changes.
11-
12-
This component is fully controlled, so you will need to provide the current
13-
`value`, an `onChange` handler, and a list of `options`. An option can be:
14-
15-
- a number
16-
- a string
17-
- or an object of props to pass to a `ListItem` (see the next example for more
18-
details here)
19-
20-
The `onChange` handler **will not be a native change event** since there are no
21-
`<input>` or `<select>` elements being rendered. Instead, the `onChange` handler
22-
will be provided the next value string as the first argument and the option as
23-
the second: `onChange(nextValue: string, option: object | string | number)`.
24-
25-
Just like a native `<select>` component, the list of options can be shown by
26-
clicking, pressing the space key, or using the up and down arrow keys. Once the
27-
list of options are shown, the user can:
28-
29-
- type letters to find a match starting with the same letters
30-
- use the up and down arrow keys to focus the previous and next items
31-
- use the home and end keys to focus the first and last items
32-
- press the escape key to close the listbox
33-
- use the space or enter key to select and close the listbox
34-
35-
> Check out the next example for some better examples of the typeahead feature
1+
This demo will showcase some of the of the theming options available for a
2+
`Select` and some other built-in functionality.

packages/documentation/src/components/Demos/Form/SelectFields/SelectFields.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ import { IconProvider } from "@react-md/icon";
44
import DemoPage from "components/Demos/DemoPage";
55
import type { DemoConfig } from "components/Demos/types";
66

7+
import SimpleNativeSelect from "./SimpleNativeSelect";
8+
import simpleNativeSelect from "./SimpleNativeSelect.md";
9+
710
import NativeSelectExample from "./NativeSelectExample";
811
import nativeSelectExample from "./NativeSelectExample.md";
912

13+
import SimpleSelectExample from "./SimpleSelectExample";
14+
import simpleSelectExample from "./SimpleSelectExample.md";
15+
1016
import SelectExample from "./SelectExample";
1117
import selectExample from "./SelectExample.md";
1218

@@ -16,11 +22,21 @@ import {
1622
} from "./CustomizingSelectOptions";
1723

1824
const demos: DemoConfig[] = [
25+
{
26+
name: "Simple Native Select",
27+
description: simpleNativeSelect,
28+
children: <SimpleNativeSelect />,
29+
},
1930
{
2031
name: "Native Select Example",
2132
description: nativeSelectExample,
2233
children: <NativeSelectExample />,
2334
},
35+
{
36+
name: "Simple Select Example",
37+
description: simpleSelectExample,
38+
children: <SimpleSelectExample />,
39+
},
2440
{
2541
name: "Select Example",
2642
description: selectExample,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The `NativeSelect` component is a simple wrapper for the `<select>` element with
2+
`TextField` styles. Just like native `<select>` elements, this wrapper **does
3+
not support**:
4+
5+
- placeholder text
6+
- enabling `readOnly` (it can almost manually be done by disabling each option
7+
yourself, but it'll make it impossible to close on mobile devices if it there
8+
are so many options that it covers the entire viewport)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { ReactElement } from "react";
2+
import { useState } from "react";
3+
import { NativeSelect } from "@react-md/form";
4+
5+
import states from "constants/states";
6+
7+
export default function SimpleNativeSelect(): ReactElement {
8+
const [value, setValue] = useState("");
9+
return (
10+
<NativeSelect
11+
id="simple-native-select"
12+
name="select"
13+
label="A Label"
14+
value={value}
15+
onChange={(event) => setValue(event.currentTarget.value)}
16+
>
17+
<option value="" disabled hidden />
18+
{states.map(({ name, abbreviation }) => (
19+
<option key={abbreviation} value={abbreviation}>
20+
{name}
21+
</option>
22+
))}
23+
</NativeSelect>
24+
);
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
The `Select` component is a custom widget that allows you to have additional
2+
styling controls for a native `<select>` element while still being accessible.
3+
This component inherits all the `TextField` styles just like the `NativeSelect`,
4+
but also allows each `option` to be rendered like a `ListItem` from the #list
5+
and #menu packages.
6+
7+
> Note: Even though the `Select` component supports the `inline` prop, it does
8+
> not behave the same was as the `NativeSelect` or a native `<select>` component
9+
> since it will not automatically update it's width to be the longest renderable
10+
> option. The size will update whenever the value of the select changes.
11+
12+
This component is fully controlled, so you will need to provide the current
13+
`value`, an `onChange` handler, and a list of `options`. An option can be:
14+
15+
- a number
16+
- a string
17+
- or an object of props to pass to a `ListItem` (see the next example for more
18+
details here)
19+
20+
The `onChange` handler **will not be a native change event** since there are no
21+
`<input>` or `<select>` elements being rendered. Instead, the `onChange` handler
22+
will be provided the next value string as the first argument and the option as
23+
the second: `onChange(nextValue: string, option: object | string | number)`.
24+
25+
Just like a native `<select>` component, the list of options can be shown by
26+
clicking, pressing the space key, or using the up and down arrow keys. Once the
27+
list of options are shown, the user can:
28+
29+
- type letters to find a match starting with the same letters
30+
- use the up and down arrow keys to focus the previous and next items
31+
- use the home and end keys to focus the first and last items
32+
- press the escape key to close the listbox
33+
- use the space or enter key to select and close the listbox
34+
35+
> Check out the [Customizing Select Options](#customizing-select-options)
36+
> example for some better examples of the typeahead feature
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { ReactElement } from "react";
2+
import { useState } from "react";
3+
import { Select } from "@react-md/form";
4+
5+
import states from "constants/states";
6+
7+
export default function SimpleSelectExample(): ReactElement {
8+
const [value, setValue] = useState("");
9+
return (
10+
<Select
11+
id="simple-select-example"
12+
label="A Label"
13+
placeholder="Choose..."
14+
name="select"
15+
options={states.map(({ abbreviation, name }) => ({
16+
label: name,
17+
value: abbreviation,
18+
}))}
19+
value={value}
20+
onChange={(value) => setValue(value)}
21+
/>
22+
);
23+
}

0 commit comments

Comments
 (0)