<SelectField>

When you need to display an enumerated field, <SelectField> maps the value to a string.

SelectField

Usage

For instance, if the gender field can take values โ€œMโ€ and โ€œFโ€, here is how to display it as either โ€œMaleโ€ or โ€œFemaleโ€:

import { SelectField } from 'react-admin';

<SelectField source="gender" choices={[
   { id: 'M', name: 'Male' },
   { id: 'F', name: 'Female' },
]} />

Props

Prop Required Type Default Description
choices Required Object[] - List of items to show as options
optionText Optional string | Function | Element โ€˜nameโ€™ Name of the field to use to display the matching choice, or function returning that field name, or a React element to render for that choice
optionValue Optional string โ€˜idโ€™ Name of the field to compare to the value to find the matching choice
translateChoice Optional boolean true Whether or not the choice text should be translated

<SelectField> also accepts the common field props.

choices

An array of objects with two keys:

  • id to map the field value
  • name for the string to display
const languages = [
  { id: "ab", name: "Abkhaz" },
  { id: "aa", name: "Afar" },
  { id: "af", name: "Afrikaans" },
  { id: "ak", name: "Akan" },
  { id: "sq", name: "Albanian" },
  { id: "am", name: "Amharic" },
  { id: "ar", name: "Arabic" },
  // ...
];

<SelectField source="language" choices={languages} />

You can customize the properties to use for the lookup value and text, thanks to the optionValue and optionText attributes.

const languages = [
  { id: "ab", name: "Abkhaz", nativeName:"ะฐางััƒะฐ" },
  { id: "aa", name: "Afar", nativeName:"Afaraf" },
  { id: "af", name: "Afrikaans", nativeName:"Afrikaans" },
  { id: "ak", name: "Akan", nativeName:"Akan" },
  { id: "sq", name: "Albanian", nativeName:"Shqip" },
  { id: "am", name: "Amharic", nativeName:"แŠ แˆ›แˆญแŠ›" },
  { id: "ar", name: "Arabic", nativeName:"ุงู„ุนุฑุจูŠุฉ" },
  // ...
];

<SelectField source="language" choices={languages} optionText="nativeName" />

Tip: If you need to fetch the choices, you probably need a <ReferenceField> instead.

optionText

You can customize the property to use for the lookup text instead of name using the optionText prop.

const currencies = [
   // ...
   {
      id: 'USD',
      name: 'US Dollar',
      namePlural: 'US dollars',
      symbol: '$',
      symbolNative: '$',
   },
   {
      id: 'RUB',
      name: 'Russian Ruble',
      namePlural: 'Russian rubles',
      symbol: 'RUB',
      symbolNative: 'โ‚ฝ.',
   },
   // ...
];
<SelectField source="currency" choices={choices} optionText="symbol" />

optionText also accepts a function, so you can shape the option text at will:

const authors = [
   { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
   { id: 456, first_name: 'Jane', last_name: 'Austen' },
];
const optionRenderer = choice => `${choice.first_name} ${choice.last_name}`;
<SelectField source="author" choices={authors} optionText={optionRenderer} />

optionText also accepts a React Element. React-admin renders it once per choice, within a RecordContext containing the related choice. You can use Field components there.

const choices = [
   { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
   { id: 456, first_name: 'Jane', last_name: 'Austen' },
];
const FullNameField = () => {
   const record = useRecordContext();
   return record ? (
      <Chip>{record.first_name} {record.last_name}</Chip>
   ) : null;
};

<SelectField source="author_id" choices={choices} optionText={<FullNameField />}/>

optionValue

You can customize the property to use for the lookup value instead of id using the optionValue prop.

const countries = [
  { name: 'Afghanistan', code: 'AF'},
  { name: 'ร…land Islands', code: 'AX'},
  { name: 'Albania', code: 'AL'},
  { name: 'Algeria', code: 'DZ'},
  // ...
];
<SelectField source="country" choices={choices} optionValue="code" />

translateChoice

The current choice is translated by default, so you can use translation identifiers as choices:

const choices = [
   { id: 'M', name: 'myroot.gender.male' },
   { id: 'F', name: 'myroot.gender.female' },
];

However, in some cases (e.g. inside a <ReferenceField>), you may not want the choice to be translated. In that case, set the translateChoice prop to false.

<SelectField source="gender" choices={choices} translateChoice={false}/>

Tip: <SelectField> sets translateChoice to true by default.