1.1.9 • Published 2 years ago

react-antd-search-select v1.1.9

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

SearchSelect

介绍

  1. SearchSelect基于ReactAnt Design,适用于React + Ant Design项目
  2. SearchSelect为受控组件
  3. SearchSelect设计为一般PC常用远程搜索需求,接口、入参、出参等参数均可自定义配置

sample

使用方式

安装

npm i @liepin/react-search-select-pc

使用

全局配置,定义你们的通用业务字段

如果你大范围使用组件的话,非常建议你进行全局配置

// @/components/search-select.jsx
import React from 'react'
import SearchSelect from '@liepin/react-search-select-pc'
import { axios } from '@/common'

export default (props) => {
  return (
    <SearchSelect
      axios={axios} // 你的 axios 实例
      startSearchIndex={1} // 接口搜索起始索引
      pageSize={15} // 每页条数
      flagKeyName='code' // 判断接口返回成功失败标识字段
      flagValue={200} // 判断接口返回成功失败标识键值
      responseDataKeyName='resData' // 响应数据res.data字段
      totalRowsKeyName='totalNumber' //  响应数据,总条数字段
      responseCurrentPageName='currentPage' // // 响应数据,当前页字段
      listKeyName='tableList' // 响应数据,列表字段
      { ...props } // 局部使用会覆盖全局设置
    />
  )
}

配置会以一个优先顺序进行合并。这个顺序是:在 lib/bundle.js 找到的库的默认值,然后是实例的 全局 配置,最后是局部配置 。后者将优先于前者

局部使用

import SearchSelect from '@/components/search-select'
<SearchSelect
  url='/user/list.json'
	queryKeyName='useName'
  labelKeyName='useName'
  valueKeyName='useId'
/>

搜索

<SearchSelect
  url='/user/list.json' // 接口
  queryParams={{ // 设置默认搜索字段
    type: 1,
    ...
  }}
  queryKeyName='useName' // 配置搜索关键字字段
  labelKeyName='useName'
  valueKeyName='useId'
/>

多选

<SearchSelect
  url='/user/list.json' // 接口
  mode='multiple'
  max={3} // 设置最多选择数量
/>

sample

回显

回显的策略是通过form.setFieldsValuedefaultValue设置value,因为组件是分页的,因此需要显式的传给组件回显数据项selectedOptionList

单选回显
useEffect(() => {
  form.setFieldsValue({
    userId: 405
  })
}, [])

<Form form={form}>
  <Form.Item name='searchSelect'>
    <SearchSelect
      url='/user/list.json'
      selectedOptionList={[
        {
          stringValue: "测试",
          text: "测试",
          value: 405
        }
      ]}
      />
  </Form.Item>
</Form>

sample

多选回显
useEffect(() => {
  form.setFieldsValue({
    userId: [987, 965]
  })
}, [])

<Form form={form}>
  <Form.Item name='searchSelect'>
    <SearchSelect
      url='/user/list.json'
      mode='multiple'
      max={3}
      selectedOptionList={[
        { 'text': '实习生(北京)科技有限公司', 'value': 987 },
        { 'text': '上海便利蜂商贸有限公司', 'value': 965 }
      ]}
      />
  </Form.Item>
</Form>

sample

onChange

返回值

  • value 为选中数据主键值;

  • row 为选中的数据项

<SearchSelect
  url='/user/list.json'
	onChange={(value, row) => {
    console.log(value, row)
  }}
/>

额外的后缀label

有时候我们可能会需要在option中显示一个其他字段,如:品牌名。这里提供两种方式

sample

1. 自定义option右侧字段

如不返回则不展示右侧额外字段。样式上一个optionlabelflex: 3extraSuffixflex: 1

<SearchSelect
  url='/goods/list.json'
  optionLabelProp='label' // 设置回填到选择框的 Option 的属性值
	extraSuffix={item => { // item为当前数据项
    return item.brandName // 返回要显示的字段
  }}
/>
2. 自定义option

row字段必须设置,为onChange时获取的数据项

<SearchSelect
  url='/goods/list.json'
	optionLabelProp='label' // 设置回填到选择框的 Option 的属性值
  renderOptions={dataList => { // dataList为下拉选数据
    return dataList.map(item => (
      <Option
        disabled={item.disabled}
        key={item.value}
        value={item.value}
        label={item.text}
        row={item} // ***此字段必须设置,为onChange时获取的数据项***
        >
        <div style={{ display: 'flex', justifyContent: 'space-between' }}>
          <span>{item.text}</span>
          <span>{item.value}</span>
        </div>
      </Option>
    ))
  }}
/>

自定义接口返回数据字段、结构

组件默认的接口返回去数据结构和字段如下

{
  data: {
    curPage: 0,
    dataList: [],
    pageSize: 15,
    totalRows: 56
  },
  flag: 1
}

但是你的接口返回的数据结构和字段比不是这样,组件提供两种方式解决

结构相同,字段不一致
<SearchSelect
  url='/user/list.json'
  flagKeyName='code' // 判断接口返回成功失败标识字段,对应 flag
  flagValue={200} // 判断接口返回成功失败标识键值,对应 flag: 1
  responseDataKeyName='resData' // 响应数据res.data字段,对应 data
  totalRowsKeyName='totalNumber' //  响应数据,总条数字段,对应 totalRows
  responseCurrentPageName='currentPage' // // 响应数据,当前页字段,对应 curPage
  listKeyName='tableList' // 响应数据,列表字段,对应 dataList
/>
结构不同

比如你的接口返回数据结构如下,要转换为组件需要的数据结构和字段

{
  code: 200,
  message: 'OK',
  list: [],
  totalNumber: 100,
  current: 1
}

通过responseDataFormat修改

<SearchSelect
  url='/user/list.json'
  responseDataFormat={(resData) => { // resData为接口响应数据
    return {
      flag: resData.code === 200 ? 1 : 0,
      data: {
        totalRows: resData.totalNumber,
        curPage: resData.current,
        dataList: resData.data.list
      }
    }
  }}
/>

API

Props

属性说明默认值类型可选值版本
axiosaxios实例,使用post请求方式,必填Function
url接口,必填String
queryParams搜索参数Object
queryKeyName搜索关键字字段queryKeywordString
pageSize搜索,每页条数键值10Number|String
pageSizeKeyName搜索条件,每页条数字段pageSizeString
currentPageKeyName搜索条件,当前页字段curPageString
labelKeyNameoption label 字段labelString
valueKeyNameoption value 字段valueString
disabledKeyNameoption disabled 字段disabledString
flagKeyName判断接口返回成功失败标识字段flagString
flagValue判断接口返回成功失败标识键值1StringNumberBooleanSymbol
responseDataKeyName响应数据res.data字段dataString
responseCurrentPageName响应数据,当前页字段默认取currentPageKeyName值String
totalRowsKeyName响应数据,总条数字段totalRowsString
listKeyName响应数据,列表字段dataListString
allowClear支持清除trueBoolean
startSearchIndex自定义接口搜索起始索引0Number|String
qsStringify是否使用qs模块序列化参数falseBoolean
selectStyleselect样式Object
extraSuffix额外的后缀labelFunction
selectedOptionList回显数据,需要配合form.setFieldsValue才能回显成功[]Array
max最多选择数量,只在 mode 为 multiple 或 tags 时有效999Number
lang选择数量超过max值时的提示语国际化类型zhStringzhenesja
renderOptions自定义optionFunction
responseDataFormat修改接口返回数据Function
requestFail请求失败后的回调,参数为接口响应数据Function1.0.2

Antd Select API

Antd Select被组件占用的且无法修改的props,其他都可用

PropTypes.node
showSearch
loading
notFoundContent
getPopupContainer
dropdownRender
onSearch
filterOption
1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago