Back to Mantine

Native Select

apps/mantine.dev/src/pages/core/native-select.mdx

9.1.14.4 KB
Original Source

import { NativeSelectDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';

export default Layout(MDX_DATA.NativeSelect);

Usage

<InputFeatures component="NativeSelect" element="select" /> <Demo data={NativeSelectDemos.usage} />

Loading state

Set loading prop to display a loading indicator. By default, the loader is displayed on the right side of the input. You can change the position with the loadingPosition prop to 'left' or 'right'. This is useful for async operations like API calls, searches, or validations:

<Demo data={NativeSelectDemos.loading} />

Controlled

tsx
import { useState } from 'react';
import { NativeSelect } from '@mantine/core';

function Demo() {
  const [value, setValue] = useState('');

  return (
    <NativeSelect
      value={value}
      onChange={(event) => setValue(event.currentTarget.value)}
      data={['React', 'Angular', 'Svelte', 'Vue']}
    />
  );
}

Uncontrolled

NativeSelect can be used with uncontrolled forms the same way as a native select element. Set the name attribute to include native select value in FormData object on form submission. To control the initial value in uncontrolled forms, use the defaultValue prop.

Example usage of uncontrolled NativeSelect with FormData:

tsx
import { NativeSelect } from '@mantine/core';

function Demo() {
  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        const formData = new FormData(event.currentTarget);
        console.log('Select value:', formData.get('framework'));
      }}
    >
      <NativeSelect
        label="Select a framework"
        name="framework"
        data={['React', 'Angular', 'Svelte', 'Vue']}
        defaultValue="React"
      />
      <button type="submit">Submit</button>
    </form>
  );
}

Adding options

NativeSelect allows passing options in two ways:

  • data prop array
  • children prop with option components

Note that if children is used, the data will be ignored.

data prop

The data prop accepts values in one of the following formats:

  1. Array of strings:
tsx
import { NativeSelect } from '@mantine/core';

function Demo() {
  return (
    <NativeSelect data={['React', 'Angular', 'Svelte', 'Vue']} />
  );
}
  1. Array of objects with label, value and disabled keys:
tsx
import { NativeSelect } from '@mantine/core';

function Demo() {
  return (
    <NativeSelect
      data={[
        { label: 'React', value: 'react' },
        { label: 'Angular', value: 'angular' },
        { label: 'Svelte', value: 'svelte', disabled: true },
        { label: 'Vue', value: 'vue' },
      ]}
    />
  );
}
  1. Array of grouped options (string format):
tsx
import { NativeSelect } from '@mantine/core';

function Demo() {
  return (
    <NativeSelect
      data={[
        {
          group: 'Frontend libraries',
          items: ['React', 'Angular', 'Svelte', 'Vue'],
        },
        {
          group: 'Backend libraries',
          items: ['Express', 'Koa', 'Django'],
        },
      ]}
    />
  );
}
  1. Array of grouped options (object format):
tsx
import { NativeSelect } from '@mantine/core';

function Demo() {
  return (
    <NativeSelect
      data={[
        {
          group: 'Frontend libraries',
          items: [
            { label: 'React', value: 'react' },
            { label: 'Angular', value: 'angular' },
            { label: 'Vue', value: 'vue', disabled: true },
          ],
        },
        {
          group: 'Backend libraries',
          items: [
            { label: 'Express', value: 'express' },
            { label: 'Koa', value: 'koa' },
            { label: 'Django', value: 'django' },
          ],
        },
      ]}
    />
  );
}

Example of the data prop with an array of grouped options:

<Demo data={NativeSelectDemos.data} />

children options

To add options with the children prop, use option elements to add options and optgroup elements to group them:

<Demo data={NativeSelectDemos.options} />

With dividers

Use hr tags to add dividers between options:

<Demo data={NativeSelectDemos.dividers} /> <InputSections component="NativeSelect" /> <Demo data={NativeSelectDemos.sections} />

Disabled state

<Demo data={NativeSelectDemos.disabled} />

Error state

<Demo data={NativeSelectDemos.error} /> <StylesApiSelectors component="NativeSelect" /> <Demo data={NativeSelectDemos.stylesApi} /> <InputAccessibility component="NativeSelect" />