Select组件由InputBase和Combobox构建
data属性支持下列格式:
字符串数组:
import { Select } from "@rtdui/core";
function Demo() {
return <Select data={["React", "Angular"]} />;
}
对象数组, 对象包含value, label 和可选的disabled键:
import { Select } from "@rtdui/core";
function Demo() {
return (
<Select
data={[
{ value: "react", label: "React" },
{ value: "vue", label: "Vue" },
{ value: "ng", label: "Angular", disabled: true },
]}
/>
);
}
分组对象数组, 分组对象包含group, items键, items的值为字符串数组或对象数组.
分组对象数组, items为字符串数组:
import { Select } from "@rtdui/core";
function Demo() {
return (
<Select
data={[
{ group: "Frontend", items: ["React", "Angular"] },
{ group: "Backend", items: ["Express", "Django"] },
]}
/>
);
}
分组对象数组, items为对象数组:
import { Select } from "@rtdui/core";
function Demo() {
return (
<Select
data={[
{
group: "Frontend",
items: [
{ value: "react", label: "React" },
{ value: "ng", label: "Angular", disabled: true },
],
},
{
group: "Backend",
items: [
{ value: "express", label: "Express" },
{ value: "django", label: "Django" },
],
},
]}
/>
);
}
(这是帮助文本)
(这是帮助文本)
<form>中使用Popover内嵌套Popover要求子Popover必须禁用withPortal属性. 如果Portal没有被禁用, 点击子Popover外部会关闭嵌套的所有Popover
所有基于Combobox的组件(间接基于Popover), 如AutoComplete,Select,MultiSelect, TagsInput内部都使用了Popover, 以及直接基于Popover构建的组件, 如Tooltip组件, 这些组件用在Popover组件内, 必须设置它们的withPortal属性为false
import { Button, Popover, Select } from "@rtdui/core";
function Demo() {
return (
<Popover width={300} position="bottom" withArrow shadow="md">
<Popover.Target>
<Button>Toggle popover</Button>
</Popover.Target>
<Popover.Dropdown>
<AutoComplete
comboboxProps={{ withinPortal: false }} // 基于Combobox的通过comboboxProps.withinPortal禁用
label="DatePickerInput within Popover"
placeholder="DatePickerInput within Popover"
data={["React", "Angular", "Svelte", "Vue"]}
/>
<Select
comboboxProps={{ withinPortal: false }} // 基于Combobox的通过comboboxProps.withinPortal禁用
label="Select within Popover"
placeholder="Select within Popover"
data={["React", "Angular", "Svelte", "Vue"]}
/>
<MultiSelect
comboboxProps={{ withinPortal: false }} // 基于Combobox的通过comboboxProps.withinPortal禁用
label="Select within Popover"
placeholder="Select within Popover"
data={["React", "Angular", "Svelte", "Vue"]}
/>
<TagsInput
comboboxProps={{ withinPortal: false }} // 基于Combobox的通过comboboxProps.withinPortal禁用
label="Select within Popover"
placeholder="Select within Popover"
data={["React", "Angular", "Svelte", "Vue"]}
/>
<Tooltip
withinPortal={false} // 直接基于Popover的通过withinPortal禁用
tip="click me"
>
<button></button>
</Tooltip>
</Popover.Dropdown>
</Popover>
);
}| 属性名 | 类型 | 是否必须 | 默认值 | 说明 |
|---|---|---|---|---|
| allowDeselect | boolean | undefined | no | true | Determines whether it should be possible to deselect value by clicking on the selected option |
| checkIconPosition | "left" | "right" | undefined | no | "left" | Position of the check icon relative to the option label |
| clearButtonProps | ComboboxClearButtonProps | undefined | no | ||
| clearable | boolean | undefined | no | false | Determines whether the clear button should be displayed in the right section when the component has value |
| color | string | undefined | no | ||
| comboboxProps | ComboboxProps | undefined | no | Props passed down to <code>Combobox</code> component | |
| data | ComboboxData | undefined | no | Data used to generate options | |
| defaultDropdownOpened | boolean | undefined | no | Uncontrolled dropdown initial opened state | |
| defaultSearchValue | string | undefined | no | Default search value | |
| defaultValue | string | null | undefined | no | Uncontrolled component default value | |
| description | React.ReactNode | no | Contents of <code>Input.Description</code> component. If not set, description is not rendered. | |
| descriptionProps | Record<string, any> | undefined | no | Props passed down to the <code>Input.Description</code> component | |
| disabled | boolean | undefined | no | Sets <code>disabled</code> attribute on the <code>input</code> element | |
| dropdownOpened | boolean | undefined | no | Controlled dropdown opened state | |
| error | React.ReactNode | no | Determines whether the input should have error styles and <code>aria-invalid</code> attribute | |
| errorProps | Record<string, any> | undefined | no | Props passed down to the <code>Input.Error</code> component | |
| filter | OptionsFilter | undefined | no | Function based on which items are filtered and sorted | |
| hiddenInputProps | Omit<Omit<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "ref">, "value"> | undefined | no | Props passed down to the hidden input | |
| id | string | undefined | no | Static id used as base to generate <code>aria-</code> attributes, by default generates random id | |
| inputContainer | ((children: ReactNode) => ReactNode) | undefined | no | (children) => children | Input container component |
| inputWrapperOrder | ("input" | "label" | "error" | "description")[] | undefined | no | ['label', 'description', 'input', 'error'] | Controls order of the elements |
| label | React.ReactNode | no | Contents of <code>Input.Label</code> component. If not set, label is not rendered. | |
| labelElement | "div" | "label" | undefined | no | "label" | <code>Input.Label</code> root element |
| labelProps | Record<string, any> | undefined | no | Props passed down to the <code>Input.Label</code> component | |
| leftSection | React.ReactNode | no | Content section rendered on the left side of the input | |
| leftSectionPointerEvents | PointerEvents | undefined | no | "none" | Sets <code>pointer-events</code> styles on the <code>rightSection</code> element |
| leftSectionWidth | Width<string | number> | undefined | no | Left section width, used to set <code>width</code> of the section and input <code>padding-left</code>, by default equals to the input height | |
| limit | number | undefined | no | Maximum number of options displayed at a time, <code>Infinity</code> by default | |
| maxDropdownHeight | string | number | undefined | no | <code>max-height</code> of the dropdown, only applicable when <code>withScrollArea</code> prop is <code>true</code>, <code>250</code> by default | |
| multiline | boolean | undefined | no | false | Determines whether the input can have multiple lines, for example when <code>component="textarea"</code> |
| nothingFoundMessage | React.ReactNode | no | Message displayed when no option matched current search query, only applicable when <code>searchable</code> prop is set | |
| onChange | ((value: string | null, option: ComboboxItem) => void) | undefined | no | Called when value changes | |
| onClear | (() => void) | undefined | no | Called when the clear button is clicked | |
| onDropdownClose | (() => void) | undefined | no | Called when dropdown closes | |
| onDropdownOpen | (() => void) | undefined | no | Called when dropdown opens | |
| onOptionSubmit | ((value: string) => void) | undefined | no | Called when option is submitted from dropdown with mouse click or <code>Enter</code> key | |
| onSearchChange | ((value: string) => void) | undefined | no | Called when search changes | |
| pointer | boolean | undefined | no | false | Determines whether the input should have <code>cursor: pointer</code> style |
| radius | string | undefined | no | "md" | Key of <code>theme.radius</code> or any valid CSS value to set <code>border-radius</code>, numbers are converted to rem |
| renderOption | ((item: ComboboxLikeRenderOptionInput<ComboboxItem>) => ReactNode) | undefined | no | A function to render content of the option, replaces the default content of the option | |
| required | boolean | undefined | no | Sets <code>required</code> attribute on the <code>input</code> element | |
| rightSection | React.ReactNode | no | Content section rendered on the right side of the input | |
| rightSectionPointerEvents | PointerEvents | undefined | no | "none" | Sets <code>pointer-events</code> styles on the <code>rightSection</code> element |
| rightSectionWidth | Width<string | number> | undefined | no | Right section width, used to set <code>width</code> of the section and input <code>padding-right</code>, by default equals to the input height | |
| searchValue | string | undefined | no | Controlled search value | |
| searchable | boolean | undefined | no | false | Determines whether the select should be searchable |
| selectFirstOptionOnChange | boolean | undefined | no | Determines whether the first option should be selected when value changes, <code>false</code> by default | |
| size | string | undefined | no | "sm" | Controls input <code>height</code> and horizontal <code>padding</code> |
| slots | { left?: string | undefined; input?: string | undefined; right?: string | undefined; } | undefined | no | ||
| value | string | null | undefined | no | Controlled component value | |
| variant | "outline" | "default" | "ghost" | undefined | no | "outline" | input variant |
| withAria | boolean | undefined | no | true | Determines whether <code>aria-</code> and other accessibility attributes should be added to the input |
| withAsterisk | boolean | undefined | no | false | Determines whether the required asterisk should be displayed. Overrides <code>required</code> prop. Does not add required attribute to the input. |
| withCheckIcon | boolean | undefined | no | true | Determines whether check icon should be displayed near the selected option label |
| withErrorStyles | boolean | undefined | no | true | Determines whether the input should have red border and red text color when the <code>error</code> prop is set |
| withScrollArea | boolean | undefined | no | Determines whether the options should be wrapped with <code>ScrollArea.AutoSize</code>, <code>true</code> by default | |
| wrapperProps | Record<string, any> | undefined | no | Props passed down to the root element of the <code>Input</code> component |