在有些情况下, 尤其只针对移动端的应用(如小程序), 你可能倾向于使用原生的下拉选项. 这种场景下可以使用NativeSelect组件. 它使用原生的option元素作为下拉选项, 但提供了额外的功能.
NativeSelect组件也是基于InputBase构建, Input的功能全部支持, 本文档没有包含所有的功能和用例, 详细请查看Input文档.
NativeSelect支持两种形式提供选项:
data属性传递数组注意: 如果children被使用, 则data属性会被忽略
data属性设置选项NativeSelect的data属性和Select组件一样支持字符串数组和对象数组.
import { NativeSelect } from "@rtdui/core";
function Demo() {
return <NativeSelect data={["React", "Angular", "Svelte", "Vue"]} />;
}
import { NativeSelect } from "@rtdui/core";
function Demo() {
return (
<NativeSelect
data={[
{ label: "React", value: "react" },
{ label: "Angular", value: "angular" },
{ label: "Svelte", value: "svelte", disabled: true },
{ label: "Vue", value: "vue" },
]}
/>
);
}
data属性支持选项分组
NativeSelect的data属性和Select组件一样支持选项分组.
import { NativeSelect } from "@rtdui/core";
function Demo() {
return (
<NativeSelect
data={[
{
group: "Frontend libraries",
items: ["React", "Angular", "Svelte", "Vue"],
},
{
group: "Backend libraries",
items: ["Express", "Koa", "Django"],
},
]}
/>
);
}
import { NativeSelect } from "@rtdui/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" },
],
},
]}
/>
);
}
这种方式最接近原生select的使用方式:
import { NativeSelect } from "@rtdui/core";
function Demo() {
return (
<NativeSelect label="With children options">
<optgroup label="Frontend libraries">
<option value="react">React</option>
<option value="angular">Angular</option>
<option value="vue" disabled>
Vue
</option>
</optgroup>
<optgroup label="Backend libraries">
<option value="express">Express</option>
<option value="koa">Koa</option>
<option value="django">Django</option>
</optgroup>
</NativeSelect>
);
}
| 属性名 | 类型 | 是否必须 | 默认值 | 说明 |
|---|---|---|---|---|
| color | string | undefined | no | ||
| data | ComboboxData | undefined | no | ||
| 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 | |
| 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 | |
| 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 | |
| multiline | boolean | undefined | no | false | Determines whether the input can have multiple lines, for example when <code>component="textarea"</code> |
| 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 |
| 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 | |
| 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 | ||
| 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. |
| 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 |
| wrapperProps | Record<string, any> | undefined | no | Props passed down to the root element of the <code>Input</code> component |