index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-12-07 09:47:50
  4. * @LastEditTime: 2022-01-01 14:29:55
  5. * @LastEditors: Please set LastEditors
  6. * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  7. * @FilePath: /MedicalWisdomCheckSys/src/components/MccsProFormSelect/index.tsx
  8. */
  9. import React,{ useState,useEffect } from 'react'
  10. import {
  11. ProFormSelect,
  12. } from '@ant-design/pro-form';
  13. import { ProFormSelectProps } from '@ant-design/pro-form/lib/components/Select';
  14. import { Input,Form } from 'antd';
  15. import './style.less';
  16. type MccsSelectProps = {
  17. withInput?: boolean, //是否带有输入功能
  18. selectList?: { label: string, value: string }[], //select可选项
  19. value?:string|number,
  20. onChange?:(val:any)=>void
  21. } & ProFormSelectProps
  22. //props 参考proComponent
  23. const MccsSelect = (props:MccsSelectProps) => {
  24. const { withInput, fieldProps, selectList,name,label,value,onChange,...restProperty } = props;
  25. const [selectMode, setSelectMode] = useState<'SELECT' | 'INPUT'>('SELECT');
  26. const [keyValue,setKeyValue] = useState<string>(''); //输入关键字
  27. const [find,setFind] = useState(false);
  28. const onSearchHandle = (val: string) => {
  29. setKeyValue(val);
  30. if (selectList&&withInput&&val.length>0) {
  31. //且需要带有输入框时
  32. const findeIndex = selectList.findIndex((t) => {
  33. return t.value == val;
  34. });
  35. if (findeIndex != -1) {
  36. setFind(true);
  37. setSelectMode('SELECT');
  38. } else {
  39. setFind(false);
  40. setSelectMode('INPUT');
  41. }
  42. }
  43. }
  44. const inputOnChange = (e:any)=>{
  45. // console.log(e.target.value);
  46. setKeyValue(e.target.value);
  47. if (selectList&&withInput) {
  48. //且需要带有输入框时
  49. const findeIndex = selectList.findIndex((t) => {
  50. return t.value == e.target.value;
  51. });
  52. if (findeIndex != -1) {
  53. setSelectMode('SELECT');
  54. } else {
  55. setSelectMode('INPUT');
  56. }
  57. }
  58. }
  59. const onSelectionchange = (values:any)=>{
  60. // console.log({values});
  61. if(withInput){
  62. //带有输入框且单选的时候
  63. if(Object.prototype.toString.call(values) == '[object String]'){
  64. setKeyValue(values);
  65. }
  66. }
  67. }
  68. return (
  69. <>
  70. {
  71. selectMode === 'SELECT' && (
  72. <ProFormSelect
  73. options={selectList}
  74. label={label}
  75. name={name}
  76. fieldProps={{
  77. onSearch: onSearchHandle,
  78. searchValue:keyValue,
  79. autoFocus:true,
  80. onChange:val=>{onSelectionchange(val);onChange&&onChange(val)},
  81. value,
  82. ...fieldProps
  83. }}
  84. {...restProperty}
  85. />
  86. )
  87. }
  88. {
  89. selectMode === 'INPUT' && (
  90. <Form.Item name={name} label={label} >
  91. <Input style={{width:'216px'}} onChange={inputOnChange} value={keyValue} placeholder="请输入" />
  92. </Form.Item>
  93. )
  94. }
  95. </>
  96. )
  97. }
  98. export default MccsSelect;