SelectField Props
import { useState } from "react";
import { SelectField } from "@servicetitan/anvil2/beta";
const ExampleComponent = () => {
const [selectedOption, setSelectedOption] = useState(null);
return (
<SelectField
label="Search users"
loadOptions={async (searchValue) => {
const response = await fetch(`/api/users?q=${searchValue}`);
const users = await response.json();
return users.map((user) => ({
id: user.id,
label: user.name,
}));
}}
onSelectedOptionChange={setSelectedOption}
value={selectedOption}
/>
);
};
The label of the select field.
Function to load options. The signature depends on the lazy mode:
- Non-lazy:
(searchValue: string) => SelectFieldOption[] | Promise<SelectFieldOption[]>
- Page-based:
(searchValue: string, pageNumber: number, pageSize: number) => { options: SelectFieldOption[], hasMore?: boolean }
- Offset-based:
(searchValue: string, offset: number, limit: number) => { options: SelectFieldOption[], hasMore?: boolean }
- Group-based:
(searchValue: string, previousGroupKey: string | number | null) => { options: SelectFieldGroupedOption[], hasMore?: boolean }
onSelectedOptionChange
(option: SelectFieldOption | null) => void
required
Callback fired when the selected option changes.
value
SelectFieldOption | null
required
The currently selected option. Must be controlled state.
Configuration for caching loadOptions results:
enabled — Whether caching is enabled (default: true)
maxSize — Maximum number of search values to cache before clearing (default: 15)
Custom CSS class name for the wrapper element.
Milliseconds to debounce search input before calling loadOptions.
Description text displayed below the input field.
Whether to hide the clear button.
Whether the field is disabled. When disabled, the input and toggle button are disabled, and the dropdown cannot open.
How to display the options menu:
auto — Popover on desktop, dialog on mobile
popover — Always display as popover
dialog — Always display as dialog
error
ReactElement | string | boolean
Error state for the field. When true, shows error styling. When a string or ReactElement, also displays as an error message below the field.
errorAriaLive
'off' | 'assertive' | 'polite'
default:"assertive"
Controls the aria-live behavior for error messages. See MDN docs. groupToString
(groupValue: SelectFieldGroupByValue) => string
Function to convert group values to display labels. Only used with grouped options. SelectFieldGroupByValue is string | number.
Visually hides the label above the input while keeping it accessible to screen readers. Note: This does not affect the label displayed in the adaptive dialog view on mobile devices.
Hint text displayed below the input field.
The id of the select field.
initialLoad
'auto' | 'immediate' | 'open'
default:"auto"
Controls when loadOptions is first called:
auto — Currently equivalent to immediate
immediate — Load on component mount
open — Load when dropdown is opened
lazy
'page' | 'offset' | 'group' | false
default:"false"
Enables lazy loading with the specified pagination strategy.
Custom ReactNode to render as the label above the input, overriding the default label text. The label prop is still required for accessibility purposes. Note: This does not affect the label displayed in the adaptive dialog view on mobile devices.
Configuration for lazy loading:
- Page mode:
{ pageSize?: number } (default pageSize: 20)
- Offset mode:
{ limit?: number } (default limit: 20)
- Group mode:
{}
Placeholder text for the input field.
Content to display before the input field.
Whether the field is read-only. When read-only, the input remains focusable and the dropdown can open, but options cannot be selected.
Whether the field is required. Shows a red asterisk (*) next to the label.
size
'small' | 'medium' | 'large'
The size of the select field.
Custom inline styles for the wrapper element.
Content to display after the input field.
SelectFieldSync Props
SelectFieldSync accepts all props from SelectField except loadOptions, lazy, and debounceMs, plus the following:import { useState } from "react";
import { SelectFieldSync } from "@servicetitan/anvil2/beta";
const options = [
{ id: 1, label: "Option One" },
{ id: 2, label: "Option Two" },
{ id: 3, label: "Option Three" },
];
const ExampleComponent = () => {
const [selectedOption, setSelectedOption] = useState(null);
return (
<SelectFieldSync
label="Select an option"
onSelectedOptionChange={setSelectedOption}
options={options}
value={selectedOption}
/>
);
};
The label of the select field.
onSelectedOptionChange
(option: SelectFieldOption | null) => void
required
Callback fired when the selected option changes.
options
SelectFieldOption[]
required
The array of options to display in the select field.
value
SelectFieldOption | null
required
The currently selected option. Must be controlled state.
Custom CSS class name for the wrapper element.
Description text displayed below the input field.
Whether to hide the clear button.
Whether the field is disabled. When disabled, the input and toggle button are disabled, and the dropdown cannot open.
How to display the options menu.
error
ReactElement | string | boolean
Error state for the field. When true, shows error styling. When a string or ReactElement, also displays as an error message below the field.
errorAriaLive
'off' | 'assertive' | 'polite'
default:"assertive"
Controls the aria-live behavior for error messages.
filter
function | MatchSorterOptions
Custom filter configuration. Can be:
- A function:
(options: SelectFieldOption[], searchValue: string) => SelectFieldOption[]
- A match-sorter options object to customize the default filtering
Default: Filters by label and searchText fields using match-sorter. Visually hides the label above the input while keeping it accessible to screen readers. Note: This does not affect the label displayed in the adaptive dialog view on mobile devices.
Hint text displayed below the input field.
Custom ReactNode to render as the label above the input, overriding the default label text. The label prop is still required for accessibility purposes. Note: This does not affect the label displayed in the adaptive dialog view on mobile devices.
Placeholder text for the input field.
Content to display before the input field.
Whether the field is read-only. When read-only, the input remains focusable and the dropdown can open, but options cannot be selected.
Whether the field is required. Shows a red asterisk (*) next to the label.
size
'small' | 'medium' | 'large'
The size of the select field.
Custom inline styles for the wrapper element.
Content to display after the input field.