Native Select
The NativeSelect
component is just a wrapper around the <select>
element to
apply some of the default theme styles. If additional customization that is not
supported by the browser is required, use the Select
instead.
Simple Native Select
import { box } from "@react-md/core/box/styles";
import { Form } from "@react-md/core/form/Form";
import { NativeSelect } from "@react-md/core/form/NativeSelect";
import { type ReactElement } from "react";
export default function SimpleNativeSelect(): ReactElement {
return (
<Form
className={box({
justify: "stretch",
disablePadding: true,
fullWidth: true,
})}
>
<NativeSelect label="Label">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
</Form>
);
}
Native Select Theme
The NativeSelect
supports the different form themes:
"outline"
(default) - Applies an outline mimicking a native text box"underline"
- Only applies an underline that will grow from the left, center, or right"filled"
- An extension of the"underline"
that also adds a background color to make it appear clickable"none"
- no styling. This is useful if a custom theme should be applied
The default theme can be configured by the form config.
import { box } from "@react-md/core/box/styles";
import { Form } from "@react-md/core/form/Form";
import { NativeSelect } from "@react-md/core/form/NativeSelect";
import { type ReactElement } from "react";
export default function NativeSelectTheme(): ReactElement {
return (
<Form
className={box({
justify: "stretch",
disablePadding: true,
fullWidth: true,
})}
>
<NativeSelect label="Label" theme="none">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
<NativeSelect label="Label" theme="underline">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
<NativeSelect label="Label" theme="filled">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
<NativeSelect label="Label" theme="outline">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
</Form>
);
}
Required Native Select
Making a <select>
required in a form is a bit tricky. It must:
- have a
name
- set
required
- create an
<option>
that has a value of an empty string - set the
defaultValue
to an empty string
import { Box } from "@react-md/core/box/Box";
import { box } from "@react-md/core/box/styles";
import { Button } from "@react-md/core/button/Button";
import { Form } from "@react-md/core/form/Form";
import { NativeSelect } from "@react-md/core/form/NativeSelect";
import { type ReactElement } from "react";
export default function RequiredNativeSelect(): ReactElement {
return (
<Form
className={box({
justify: "stretch",
disablePadding: true,
fullWidth: true,
})}
>
<NativeSelect label="Label" name="choice" required defaultValue="">
<option value="" disabled hidden />
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
<Box disablePadding align="start">
<Button type="submit" theme="primary" themeType="contained">
Submit
</Button>
</Box>
</Form>
);
}
Required with a placeholder
This example shows how to make a required select that has placeholder text.
import { Box } from "@react-md/core/box/Box";
import { box } from "@react-md/core/box/styles";
import { Button } from "@react-md/core/button/Button";
import { Form } from "@react-md/core/form/Form";
import { NativeSelect } from "@react-md/core/form/NativeSelect";
import { type ReactElement } from "react";
export default function RequiredWithAPlaceholder(): ReactElement {
return (
<Form
className={box({
justify: "stretch",
disablePadding: true,
fullWidth: true,
})}
>
<NativeSelect
label="Label"
name="choice"
required
defaultValue=""
labelProps={{ floatingActive: true }}
>
<option value="" disabled>
Select an option
</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
<Box disablePadding align="start" justify="space-between">
<Button type="reset" theme="warning" themeType="outline">
Reset
</Button>
<Button type="submit" theme="primary" themeType="contained">
Submit
</Button>
</Box>
</Form>
);
}
Help and Error Text
The NativeSelect
component is wrapped in the
FormMessageContainer so additional
hint or error messages can be displayed.
import { box } from "@react-md/core/box/styles";
import { Form } from "@react-md/core/form/Form";
import { NativeSelect } from "@react-md/core/form/NativeSelect";
import { type ReactElement } from "react";
export default function HelpAndErrorText(): ReactElement {
return (
<Form
className={box({
stacked: true,
disablePadding: true,
})}
>
<NativeSelect
label="Label"
messageProps={{
children: "This is some help text.",
}}
>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
<NativeSelect
label="Label"
error
messageProps={{
children: "This field has an error!",
}}
>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</NativeSelect>
</Form>
);
}
Multiselect Example
import { Box } from "@react-md/core/box/Box";
import { Button } from "@react-md/core/button/Button";
import { Form } from "@react-md/core/form/Form";
import { NativeSelect } from "@react-md/core/form/NativeSelect";
import { type ReactElement } from "react";
export default function MultiselectExample(): ReactElement {
return (
<Form>
<NativeSelect
label="Select a state"
name="state"
required
multiple
size={8}
theme="underline"
>
<option value="" disabled hidden />
{states.map(({ name, abbreviation }) => (
<option key={abbreviation} value={abbreviation}>
{name}
</option>
))}
</NativeSelect>
<Box justify="space-between">
<Button type="reset" theme="warning">
Reset
</Button>
<Button type="submit" theme="primary">
Submit
</Button>
</Box>
</Form>
);
}
export const states = [
{
name: "Alabama",
abbreviation: "AL",
},
{
name: "Alaska",
abbreviation: "AK",
},
{
name: "American Samoa",
abbreviation: "AS",
},
{
name: "Arizona",
abbreviation: "AZ",
},
{
name: "Arkansas",
abbreviation: "AR",
},
{
name: "California",
abbreviation: "CA",
},
{
name: "Colorado",
abbreviation: "CO",
},
{
name: "Connecticut",
abbreviation: "CT",
},
{
name: "Delaware",
abbreviation: "DE",
},
{
name: "District Of Columbia",
abbreviation: "DC",
},
{
name: "Federated States Of Micronesia",
abbreviation: "FM",
},
{
name: "Florida",
abbreviation: "FL",
},
{
name: "Georgia",
abbreviation: "GA",
},
{
name: "Guam",
abbreviation: "GU",
},
{
name: "Hawaii",
abbreviation: "HI",
},
{
name: "Idaho",
abbreviation: "ID",
},
{
name: "Illinois",
abbreviation: "IL",
},
{
name: "Indiana",
abbreviation: "IN",
},
{
name: "Iowa",
abbreviation: "IA",
},
{
name: "Kansas",
abbreviation: "KS",
},
{
name: "Kentucky",
abbreviation: "KY",
},
{
name: "Louisiana",
abbreviation: "LA",
},
{
name: "Maine",
abbreviation: "ME",
},
{
name: "Marshall Islands",
abbreviation: "MH",
},
{
name: "Maryland",
abbreviation: "MD",
},
{
name: "Massachusetts",
abbreviation: "MA",
},
{
name: "Michigan",
abbreviation: "MI",
},
{
name: "Minnesota",
abbreviation: "MN",
},
{
name: "Mississippi",
abbreviation: "MS",
},
{
name: "Missouri",
abbreviation: "MO",
},
{
name: "Montana",
abbreviation: "MT",
},
{
name: "Nebraska",
abbreviation: "NE",
},
{
name: "Nevada",
abbreviation: "NV",
},
{
name: "New Hampshire",
abbreviation: "NH",
},
{
name: "New Jersey",
abbreviation: "NJ",
},
{
name: "New Mexico",
abbreviation: "NM",
},
{
name: "New York",
abbreviation: "NY",
},
{
name: "North Carolina",
abbreviation: "NC",
},
{
name: "North Dakota",
abbreviation: "ND",
},
{
name: "Northern Mariana Islands",
abbreviation: "MP",
},
{
name: "Ohio",
abbreviation: "OH",
},
{
name: "Oklahoma",
abbreviation: "OK",
},
{
name: "Oregon",
abbreviation: "OR",
},
{
name: "Pennsylvania",
abbreviation: "PA",
},
{
name: "Puerto Rico",
abbreviation: "PR",
},
{
name: "Rhode Island",
abbreviation: "RI",
},
{
name: "South Carolina",
abbreviation: "SC",
},
{
name: "South Dakota",
abbreviation: "SD",
},
{
name: "Tennessee",
abbreviation: "TN",
},
{
name: "Texas",
abbreviation: "TX",
},
{
name: "Utah",
abbreviation: "UT",
},
{
name: "Vermont",
abbreviation: "VT",
},
{
name: "Virgin Islands",
abbreviation: "VI",
},
{
name: "Virginia",
abbreviation: "VA",
},
{
name: "Washington",
abbreviation: "WA",
},
{
name: "West Virginia",
abbreviation: "WV",
},
{
name: "Wisconsin",
abbreviation: "WI",
},
{
name: "Wyoming",
abbreviation: "WY",
},
] as const;
export type StateName = (typeof states)[number]["name"];
export type StateAbbreviation = (typeof states)[number]["abbreviation"];
export interface State {
name: StateName;
abbreviation: StateAbbreviation;
}