数组渲染Select

  1. import { Select } from 'antd'
  2. const { Option } = Select
  3. const STATUS = [
  4. { label: '日常', value: 1 },
  5. { label: '预发', value: 2 }
  6. ]
  7. // 多语言
  8. <Select placeholder={formatMessage(lang.placeholder)}>
  9. <Select>
  10. {array.map((item, keys) => (
  11. <Select.Option key={keys} value={item.value}>
  12. {item.label}
  13. </Select.Option>
  14. ))}
  15. </Select>
  16. function SelectENV() {
  17. return (
  18. <Select placeholder="请选择环境" allowClear>
  19. {
  20. STATUS.map(r =>
  21. <Option value={r.value} key={r.value}>{r.label}</Option>)
  22. }
  23. </Select>
  24. )
  25. }

labelInValue

  1. <Form.Item label="名称">
  2. {getFieldDecorator('name', {
  3. // initialValue: state.id || undefined,
  4. initialValue: state.id ? { key: state.id } : undefined,
  5. rules: [{ required: true, message: '请输入名称' }],
  6. })(<SelectApp native labelInValue />)}
  7. </Form.Item>

Select动态渲染

optionKey 抛出值的类型是 字符串,不是 id
labelInValue 传值 {key: id}
form.setFieldsValue({})

Select自带Ajax请求

Select labelInValue 默认 false,返回 id
labelInValue={true} 返回值是一个 对象 {key: 23, label: '倍镜'}

  1. import React, { useEffect, useState } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { Select, Divider, message, Icon } from 'antd'
  4. import { getUsers, putUser } from './_server'
  5. const { Option } = Select;
  6. SelectUser.propTypes = {
  7. native: PropTypes.bool,
  8. labelInValue: PropTypes.bool,
  9. // value: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
  10. onChange: PropTypes.func,
  11. }
  12. SelectUser.defaultProps = {
  13. native: false,
  14. labelInValue: false,
  15. // value: undefined,
  16. onChange: () => {},
  17. }
  18. function SelectUser(props) {
  19. const {
  20. value,
  21. onChange,
  22. optionKey = 'id',
  23. native = false,
  24. labelInValue = false,
  25. allowClear = false,
  26. } = props;
  27. const [name, setName] = useState('');
  28. const [data, setData] = useState([])
  29. useEffect(() => { init() }, [])
  30. async function init() {
  31. const res = await getUsers();
  32. if(res.code !== 0) return;
  33. const arr = res.map(r => ({ label: r.value, value: r.id }))
  34. setData(arr)
  35. }
  36. // 新增接口
  37. async function add() {
  38. if(!name) {
  39. message.error('请输入用户')
  40. }
  41. const res = putUser({name})
  42. if(res.code === 0) init();
  43. }
  44. function onSearch(index) {
  45. if(index !== undefined) setName(index)
  46. }
  47. // 下拉框显示,新增按钮
  48. function dropdownRender(menu) {
  49. // native 只显示菜单,不显示按钮
  50. if (native) return <>{menu}</>;
  51. return (
  52. <>
  53. {menu}
  54. <Divider style={{ margin: '4px 0' }} />
  55. <div
  56. style={{ padding: '4px 8px', cursor: 'pointer' }}
  57. onMouseDown={e => e.preventDefault()}
  58. >
  59. <a href={null} rel="noopener noreferrer" onClick={add}>
  60. <Icon type="plus" /> 新增用户
  61. </a>
  62. </div>
  63. </>
  64. );
  65. }
  66. return (
  67. <Select
  68. allowClear={allowClear}
  69. labelInValue={labelInValue}
  70. showSearch
  71. placeholder="请选择所属应用"
  72. notFoundContent=""
  73. optionFilterProp="children"
  74. value={value}
  75. filterOption={(input, option) => option.props.children.includes(input)}
  76. dropdownRender={menu => dropdownRender(menu)}
  77. onSearch={onSearch}
  78. onChange={onChange}
  79. >
  80. {
  81. data.map(r => <Option value={r.value} key={r.value}>{r.label}</Option>)
  82. }
  83. </Select>
  84. )
  85. }
  86. export default SelectUser;

父组件调用

  1. <Form.Item label="应用场景">
  2. {getFieldDecorator('scene', {
  3. // initialValue: data.sceneId, labelInValue={false}
  4. initialValue: data.sceneId ? { key: data.sceneId } : undefined,
  5. rules: [{ required: true, message: '请选择场景' }],
  6. })(<SelectUser native labelInValue allowclear/>)}
  7. </Form.Item>

Select placeholder不显示

select的 value默认值,必须是 undefined 才显示 placeholder
initialValue: data.id ? { key:data.id } : undefined