使用 我的应用 也是一种 支持 我的方式:
DockLift: 点击 Dock 应用窗口回到当前屏幕 Zipora: Zip/RAR/7Z 解压工具 Scap: Screenshot & Markup Edit Screen Test Deskmark Keyzer Vidwall Hub VidCrop Vidwall Mousio Hint Mousio Musicer Audioer FileSentinel FocusCursor Videoer KeyClicker DayBar Iconed Menuist Quick RSS Quick RSS Web Serve Copybook Generator DevTutor for SwiftUI RegexMate Time Passage Iconize Folder Textsound Saver Create Custom Symbols DevHub Resume Revise Palette Genius Symbol Scribe

SearchSelect 搜索选择器 === [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) [![Telegram Channel](https://img.shields.io/badge/%20-Telegram-00618f.svg?logo=Telegram)](https://t.me/s/AwesomeMacApps) [![Follow On X](https://img.shields.io/badge/Follow%20on%20X-333333?logo=x&logoColor=white)](https://x.com/jaywcjlove) [![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-search-select/file/README.md) [![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-search-select.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-search-select) [![npm version](https://img.shields.io/npm/v/@uiw/react-search-select.svg?label=@uiw/react-search-select)](https://npmjs.com/@uiw/react-search-select) 搜索选择器 ```jsx import { SearchSelect, Row ,Col } from 'uiw'; // or import SearchSelect from '@uiw/react-search-select'; ``` ## 基础示例 ```jsx mdx:preview&bg=#fff import React from 'react'; import { SearchSelect,Row,Col } from 'uiw'; export default function Demo() { const option=[ { label: 'a1', value: 1 }, { label: 'a2', value: 2 }, { label: 'a3', value: 3 }, { label: 'a4', value: 4 }, { label: 'a5', value: 5 }, { label: 'a6', value: 6 }, { label: 'a7', value: 7 }, { label: 'a8', value: 8 }, ] return( ); }; ``` ## 尺寸 通过 size 属性设置选择器的尺寸,提供三个尺寸参数设置。 ```jsx mdx:preview&bg=#fff import React from 'react'; import { SearchSelect,Row,Col } from 'uiw'; export default function Demo() { const option=[ { label: '小尺寸', value: 1 }, { label: '默认尺寸', value: 2 }, { label: '大尺寸', value: 3 }, ] return( ); }; ``` ## 不同的value类型 通`labelInValue`参数控制value类型和onChange时间返回参数的类型,设置为`true`时,`value`和`onChange`回调返回的值类型从[..., value]变成[..., { label, value}] ```jsx mdx:preview&bg=#fff import React from 'react'; import { SearchSelect,Row,Col } from 'uiw'; export default function Demo() { const option=[ { label: 'a1', value: 1 }, { label: 'a2', value: 2 }, { label: 'a3', value: 3 }, { label: 'a4', value: 4 }, { label: 'a5', value: 5 }, { label: 'a6', value: 6 }, { label: 'a7', value: 7 }, { label: 'a8', value: 8 }, ] const [loading, setLoading] = React.useState(false); const [value, setValue] = React.useState([1 ,7]); const [labelValue, setLabelValue] = React.useState([{ label: 'a1', value: 1 }, { label: 'a7', value: 7 }]); return( { console.log('value', value) setValue(value) }} placeholder="请选择选项" /> { console.log('value', value) setLabelValue(value) }} plac placeholder="请选择选项" /> ); }; ``` ## 限制选项个数 限制最多只能选择两个选项,达到最大后如果继续选择,会替换最后一个 ```jsx mdx:preview&bg=#fff import React from 'react'; import { SearchSelect,Row,Col } from 'uiw'; export default function Demo() { const selectOption=[ { label: 'a1', value: 1 }, { label: 'a2', value: 2 }, { label: 'a3', value: 3 }, { label: 'a4', value: 4 }, { label: 'a5', value: 5 }, { label: 'a6', value: 6 }, { label: 'a7', value: 7 }, { label: 'a8', value: 8 }, ] const valueAmount = 2 const [option, setOption] = React.useState(selectOption); const [loading, setLoading] = React.useState(false); const [values, setValues] = React.useState([ 1, 2, 7]); function handleSearch(e) { setLoading(true) setTimeout(() => { const filterOpion= selectOption.filter(item=>!!item.label.includes(e.trim())) setOption([...filterOpion]); setLoading(false); }, 500); } return( console.log('onSelect',value)} loading={loading} option={option} onChange={(value) => { setValues(value) }} /> ); }; ``` ## 显示最大数量 使用`maxTagCount`限制显示tag的数量,超过后使用省略tag显示 ```jsx mdx:preview&bg=#fff import React from 'react'; import { SearchSelect,Row,Col } from 'uiw'; export default function Demo() { const selectOption=[ { label: 'a1', value: 1 }, { label: 'a2', value: 2 }, { label: 'a3', value: 3 }, { label: 'a4', value: 4 }, { label: 'a5', value: 5 }, { label: 'a6', value: 6 }, { label: 'a7', value: 7 }, { label: 'a8', value: 8 }, ] const maxTagCount = 4 const [option, setOption] = React.useState(selectOption); const [loading, setLoading] = React.useState(false); const [values, setValues] = React.useState([ { label: 'a1', value: 1 }, { label: 'a2', value: 2 }, { label: 'a5', value: 5 }, { label: 'a7', value: 7 }, { label: 'a8', value: 8 }, ]); function handleSearch(e) { setLoading(true) setTimeout(() => { const filterOpion= selectOption.filter(item=>!!item.label.includes(e.trim())) setOption([...filterOpion]); setLoading(false); }, 500); } return( { setValues(value) }} /> ); }; ``` ## 启用搜索 将`showSearch`设置为`true`启用搜索功能,组件没有内置搜索功能,但`option`是监听变化的,可以通过配合`onSearch`实现 ```jsx mdx:preview&bg=#fff import React from 'react'; import { SearchSelect,Row,Col } from 'uiw'; export default function Demo() { const selectOption=[ { label: 'a1', value: 1 }, { label: 'a2', value: 2 }, { label: 'a3', value: 3 }, { label: 'a4', value: 4 }, { label: 'a5', value: 5 }, { label: 'a6', value: 6 }, { label: 'a7', value: 7 }, { label: 'a8', value: 8 }, ] const [value, setValue] = React.useState([1,7]); const [values, setValues] = React.useState([1,7]); const [option, setOption] = React.useState(selectOption); const [loading, setLoading] = React.useState(false); function handleSearch(e) { setLoading(true) setTimeout(() => { const filterOpion= selectOption.filter(item=>!!item.label.includes(e.trim())) setOption([...filterOpion]); setLoading(false); }, 500); } return( { setValue(value) }} /> { setValues(value) }} /> ); }; ``` ### 在表单中使用 在 [`
`](#/components/form) 表单中应用 `` 组件。 ```jsx mdx:preview&bg=#fff import React from 'react'; import { Form, Row, Col, SearchSelect, Button, Notify } from 'uiw'; export default function Demo() { const selectOption =[ { label: 'a1', value: 1 }, { label: 'a2', value: 2 }, { label: 'a3', value: 3 }, { label: 'a4', value: 4 }, { label: 'a5', value: 5 }, { label: 'a6', value: 6 }, { label: 'a7', value: 7 }, { label: 'a8', value: 8 }, ]; const [option, setOption] = React.useState(selectOption); const [loading, setLoading] = React.useState(false); const refForm = React.useRef() function handleSearch(e) { setLoading(true) setTimeout(() => { const filterOpion= selectOption.filter(item=>!!item.label.includes(e.trim())) setOption([...filterOpion]); setLoading(false); }, 500); } const reSetForm = () => { console.log('form', refForm.current.resetForm()) } return (
{ if (error.filed) { return { ...error.filed }; } return null; }} onSubmit={({initial, current}) => { const errorObj = {}; if (!current.selectField) { errorObj.selectField = '默认需要选择内容,选择入内容'; } if(Object.keys(errorObj).length > 0) { const err = new Error(); err.filed = errorObj; Notify.error({ title: '提交失败!', description: '请确认提交表单是否正确!' }); throw err; } Notify.success({ title: '提交成功!', description: `表单提交成功,选择值为:${current.selectField},将自动填充初始化值!`, }); }} fields={{ selectField: { initialValue:[{label: 'a7', value: 7},{label: 'a8', value: 8}], children: ( { console.log('onChange',v) }} option={option} loading={loading} /> ) }, selectSingle: { initialValue:[{label: 'a7', value: 7}], children: ( { console.log('onChange',v) }} option={option} loading={loading} /> ) }, }} > {({ fields, state, canSubmit }) => { return (
{fields.selectField} {fields.selectSingle}
                    {JSON.stringify(state.current, null, 2)}
                  
) }}
); } ``` ## Props | 参数 | 说明 | 类型 | 默认值 | 版本 | | ---- | ---- | ---- | ---- | ---- | | allowClear | 支持清除 | Boolean | `false` | - | | disabled | 禁用选择器 | Boolean | `false` | - | | mode | 选择模式: `multiple` `single` | String | `single` | - | | defaultValue | 指定默认选中的条目 | String/Number | - | - | | value | 指定当前选中的条目,多选时为一个数组 | String \| Number \| String[] \| Number[] \| LabeledValue \| LabeledValue[] | - | - | | placeholder | 选择框默认文字 | String | - | - | | maxTagCount | 多选模式下展示tag的个数,默认所有 | number | - | - | | labelInValue | 开启会把 Select 的 value 类型从 `string/number` 变为 `{ value: string/number, label: string }` | Boolean | `false` | - | | showSearch | 是否可搜索 | Boolean | - | - | | size | 选择框尺寸 | Enum{large, default, small } | `default` | - | | tagProps | 将参数传递给 [``](#/components/tag) 组件 | `TagProps` | `{}` | `4.13.0` | | onChange | 选中 option,或 input 的 value,调用此函数 | function(value:String \| Number \| String[] \| Number[] \| LabeledValue \| LabeledValue[]) | - | - | | onSearch | 文本框值变化时回调 | function(value: String) | - | - | | onSelect | 被选中时调用,参数为选中项的 value | function(value: String/Number ) | - | - | | loading | 加载中状态 | Boolean | `false` | - | | valueAmount | 多选模式下,限制最多选择多少个(value的长度) | number | - | - |