4.22.3 • Published 5 months ago

@uiw/react-search-select v4.22.3

Weekly downloads
260
License
MIT
Repository
github
Last release
5 months ago

SearchSelect 搜索选择器

Buy me a coffee Open in unpkg NPM Downloads npm version

搜索选择器

import { SearchSelect, Row ,Col } from 'uiw';
// or
import SearchSelect from '@uiw/react-search-select';

基础示例

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(
    <Row gutter={20}>
     <label>单选</label>
     <Row>
       <SearchSelect
         mode="single"
         style={{ width: 200 }}
         placeholder="请选择选项"
         option={option}
       />
     </Row>
     <label>多选</label>
     <Row>
       <SearchSelect
         mode="multiple"
         style={{ width: 200 }}
         allowClear={true}
         placeholder="请选择选项"
         option={option}
       />
     </Row>
     <label>禁用</label>
     <Row>
       <SearchSelect
         mode="single"
         style={{ width: 200 }}
         allowClear={true}
         placeholder="请选择选项"
         option={option}
         disabled={true}
       />
     </Row>
    </Row>
  );
};

尺寸

通过 size 属性设置选择器的尺寸,提供三个尺寸参数设置。

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(
    <Row style={{ flexDirection: 'column' }}>
      <Col>
        <Row>
          <SearchSelect
            mode="multiple"
            style={{ width: 150 }}
            option={option}
            value={[1]}
            allowClear={true}
            placeholder="请选择选项"
            size={'small'}
          />
        </Row>
      </Col>
      <Col style={{ margin:'10px 0px' }}>
        <Row>
          <SearchSelect
            mode="multiple"
            style={{ width: 175 }}
            option={option}
            value={[2]}
            allowClear={true}
            placeholder="请选择选项"
          />
        </Row>
      </Col>
      <Col  >
        <Row>
          <SearchSelect
            mode="multiple"
            style={{ width: 200 }}
            option={option}
            value={[3]}
            allowClear={true}
            placeholder="请选择选项"
            size={'large'}
          />
        </Row>
      </Col>
    </Row>
  );
};

不同的value类型

labelInValue参数控制value类型和onChange时间返回参数的类型,设置为true时,valueonChange回调返回的值类型从..., value变成..., { label, value}

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(
    <Row gutter={20}>
     <Row>
       <SearchSelect
         mode="multiple"
         style={{ width: 200 }}
         value={value}
         option={option}
         onChange={(value)=> {
           console.log('value', value)
           setValue(value)
         }}
         placeholder="请选择选项"
       />
     </Row>
     <Row>
       <SearchSelect
         mode="multiple"
         labelInValue={true}
         style={{ width: 200 }}
         option={option}
         value={labelValue}
         onChange={(value)=> {
           console.log('value', value)
           setLabelValue(value)
         }}
         plac
         placeholder="请选择选项"
       />
     </Row>
    </Row>
  );
};

限制选项个数

限制最多只能选择两个选项,达到最大后如果继续选择,会替换最后一个

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(
    <Row style={{ marginLeft: 10 }}>
        <SearchSelect
          mode="multiple"
          style={{ width: 200 }}
          valueAmount={valueAmount}
          allowClear
          value={values}
          placeholder="请选择选项"
          onSearch={handleSearch}
          // onSelect={(value)=>console.log('onSelect',value)}
          loading={loading}
          option={option}
          onChange={(value) => {
            setValues(value)
          }}
        />
    </Row>
  );
};

显示最大数量

使用maxTagCount限制显示tag的数量,超过后使用省略tag显示

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(
    <Row style={{ marginLeft: 10 }}>
        <SearchSelect
          mode="multiple"
          style={{ width: 200 }}
          labelInValue={true}
          maxTagCount={maxTagCount}
          allowClear
          value={values}
          placeholder="请选择选项"
          onSearch={handleSearch}
          loading={loading}
          option={option}
          onChange={(value) => {
            setValues(value)
          }}
        />
    </Row>
  );
};

启用搜索

showSearch设置为true启用搜索功能,组件没有内置搜索功能,但option是监听变化的,可以通过配合onSearch实现

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(
    <Row style={{ marginLeft: 10 }}>
      <Col fixed style={{width:200}}>
        <SearchSelect
          mode="singe"
          showSearch={true}
          value={value}
          option={option}
          loading={loading}
          onSearch={handleSearch}
          placeholder="请选择选项"
          style={{ width: 200 }}
          onChange={(value) => {
            setValue(value)
          }}
        />
      </Col>
      <Col fixed style={{width:200, marginLeft: 20}}>
        <SearchSelect
          mode="multiple"
          showSearch={true}
          value={values}
          option={option}
          loading={loading}
          onSearch={handleSearch}
          placeholder="请选择选项"
          style={{ width: 200 }}
          onChange={(value) => {
            setValues(value)
          }}
        />
      </Col>
    </Row>
  );
};

在表单中使用

<Form /> 表单中应用 <SearchSelect /> 组件。

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 (
    <div>
      <Form
        ref={refForm}
        onSubmitError={(error) => {
          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: (
              <SearchSelect
                allowClear
                labelInValue={true}
                showSearch={true}
                mode="multiple"
                disabled={false}
                placeholder="请选择选项"
                onSearch={handleSearch}
                onChange={(v)=>{
                  console.log('onChange',v)
                }}
                option={option}
                loading={loading}
              />
            )
          },
          selectSingle: {
            initialValue:[{label: 'a7', value: 7}],
            children: (
              <SearchSelect
                mode="single"
                labelInValue={true}
                showSearch={true}
                allowClear
                disabled={false}
                placeholder="请选择选项"
                onSearch={handleSearch}
                onChange={(v)=>{
                  console.log('onChange',v)
                }}
                option={option}
                loading={loading}
              />
            )
          },
        }}
      >
        {({ fields, state, canSubmit }) => {
          return (
            <div style={{ marginLeft:10}}>
              <Row >
                <Col fixed style={{width:200}}>{fields.selectField}</Col>
              </Row>
              <Row>
                <Col fixed style={{width:200}}>{fields.selectSingle}</Col>
              </Row>
              <Row>
                <Col fixed>
                  <Button disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
                </Col>
                <Col fixed>
                  <Button onClick={reSetForm}>重置</Button>
                </Col>
              </Row>
              <Row>
                <Col>
                  <pre style={{ padding: 10, marginTop: 10 }}>
                    {JSON.stringify(state.current, null, 2)}
                  </pre>
                </Col>
              </Row>
            </div>
          )
        }}
      </Form>
    </div>
  );
}

Props

参数说明类型默认值版本
allowClear支持清除Booleanfalse-
disabled禁用选择器Booleanfalse-
mode选择模式: multiple singleStringsingle-
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 }Booleanfalse-
showSearch是否可搜索Boolean--
size选择框尺寸Enum{large, default, small }default-
tagProps将参数传递给 <Tag> 组件TagProps{}4.13.0
onChange选中 option,或 input 的 value,调用此函数function(value:String | Number | String[] | Number[] | LabeledValue | LabeledValue[])--
onSearch文本框值变化时回调function(value: String)--
onSelect被选中时调用,参数为选中项的 valuefunction(value: String/Number )--
loading加载中状态Booleanfalse-
valueAmount多选模式下,限制最多选择多少个(value的长度)number--
4.22.0

8 months ago

4.22.1

6 months ago

4.22.2

6 months ago

4.22.3

5 months ago

4.21.26

1 year ago

4.21.25

1 year ago

4.21.28

1 year ago

4.21.27

1 year ago

5.0.0-bate.2.1

1 year ago

5.0.0-bate.2.0

1 year ago

4.21.24

1 year ago

5.0.0-bate-19

1 year ago

4.21.22

1 year ago

4.21.21

1 year ago

4.21.23

1 year ago

5.0.0-bate-18

1 year ago

5.0.0-bate-14

1 year ago

5.0.0-bate-17

1 year ago

5.0.0-bate-10

1 year ago

5.0.0-bate-11

1 year ago

5.0.0-bate-12

1 year ago

5.0.0-bate-9

1 year ago

5.0.0-bate-7

1 year ago

5.0.0-bate-8

1 year ago

5.0.0-bate-6

1 year ago

4.21.20

1 year ago

4.21.19

2 years ago

4.21.18

2 years ago

4.21.15

2 years ago

4.21.17

2 years ago

4.21.16

2 years ago

5.0.0-bate-5

2 years ago

5.0.0-bate-3

2 years ago

5.0.0-bate-4

2 years ago

5.0.0-bate-1

2 years ago

5.0.0-bate-2

2 years ago

5.0.0-bate-0

2 years ago

4.21.13

2 years ago

4.21.14

2 years ago

4.21.11

2 years ago

4.21.12

2 years ago

4.21.10

2 years ago

4.21.1

2 years ago

4.21.2

2 years ago

4.21.3

2 years ago

4.21.4

2 years ago

4.21.0

2 years ago

4.20.0

2 years ago

4.21.9

2 years ago

4.21.5

2 years ago

4.21.6

2 years ago

4.21.7

2 years ago

4.21.8

2 years ago

4.18.1

2 years ago

4.18.0

2 years ago

4.17.0

2 years ago

4.16.0

2 years ago

4.16.1

2 years ago

4.16.2

2 years ago

4.19.0

2 years ago

4.15.1

2 years ago

4.14.1

2 years ago

4.14.2

2 years ago

4.14.0

2 years ago

4.13.7

2 years ago

4.13.8

2 years ago

4.13.9

2 years ago

4.13.11

2 years ago

4.13.12

2 years ago

4.13.10

2 years ago

4.15.0

2 years ago

4.10.5

2 years ago

4.10.6

2 years ago

4.10.7

2 years ago

4.9.9

2 years ago

4.10.1

2 years ago

4.10.2

2 years ago

4.10.3

2 years ago

4.10.4

2 years ago

4.10.0

2 years ago

4.13.6

2 years ago

4.13.2

2 years ago

4.13.3

2 years ago

4.13.4

2 years ago

4.13.5

2 years ago

4.13.0

2 years ago

4.13.1

2 years ago

4.12.0

2 years ago

4.12.1

2 years ago

4.12.2

2 years ago

4.11.4

2 years ago

4.11.5

2 years ago

4.11.6

2 years ago

4.9.11

2 years ago

4.9.10

2 years ago

4.11.0

2 years ago

4.11.1

2 years ago

4.11.2

2 years ago

4.11.3

2 years ago

4.9.7

3 years ago

4.9.6

3 years ago

4.9.4

3 years ago

4.9.3

3 years ago

4.9.2

3 years ago

4.9.1

3 years ago

4.9.0

3 years ago

4.8.9

3 years ago

4.8.8

3 years ago

4.7.16

3 years ago

4.8.5

3 years ago

4.8.4

3 years ago

4.8.7

3 years ago

4.8.6

3 years ago

4.8.1

3 years ago

4.8.0

3 years ago

4.8.3

3 years ago

4.8.2

3 years ago

4.7.13

3 years ago

4.7.14

3 years ago

4.7.15

3 years ago

4.7.12

3 years ago

4.7.10

3 years ago

4.7.11

3 years ago

4.7.9

3 years ago

4.7.8

3 years ago

4.7.6

3 years ago

4.7.7

3 years ago

4.7.5

3 years ago

4.7.4

3 years ago

4.7.3

3 years ago

4.7.2

3 years ago

4.7.0

3 years ago

4.6.19

3 years ago

4.6.15

3 years ago

4.6.16

3 years ago

4.6.17

3 years ago

4.6.18

3 years ago

4.6.14

4 years ago

4.6.13

4 years ago

4.6.12

4 years ago

4.6.11

4 years ago

4.6.10

4 years ago

4.6.9

4 years ago

4.6.8

4 years ago

4.6.7

4 years ago

4.6.6

4 years ago

4.6.3

4 years ago

4.6.4

4 years ago

4.6.2

4 years ago

4.6.1

4 years ago

4.6.0

4 years ago

4.5.0

4 years ago

4.4.1

4 years ago

4.4.0

4 years ago

4.3.1

4 years ago

4.2.3

4 years ago

4.2.5

4 years ago

4.3.0

4 years ago

4.2.14

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.1.3

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

4.0.0-alpha.7

4 years ago

4.0.0-alpha.8

4 years ago

4.0.0-alpha.6

4 years ago

4.0.0-alpha.3

4 years ago

4.0.0-alpha.2

4 years ago

4.0.0-alpha.1

4 years ago

4.0.0-alpha.0

4 years ago