知与行后台管理端

SelectGroup.jsx 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useState, useEffect } from 'react';
  2. import { Select, Modal } from 'antd';
  3. import { apis, fetch } from '../../utils/request';
  4. const getGroupList = fetch(apis.groupActivity.list)
  5. export default props => {
  6. const {
  7. value,
  8. onChange,
  9. ...rest
  10. } = props;
  11. const [list, setList] = useState([]);
  12. const [visible, setVisible] = useState(false);
  13. const [group, setGroup] = useState({ groupId: undefined, groupName: '请选择拼团' })
  14. const getGroupTitle = val => {
  15. return (list.filter(x => x.groupActicityId == val)[0] || {}).activityName || '请选择拼团'
  16. }
  17. const updateGroup = val => setGroup({ groupId: val, groupName: getGroupTitle(val) })
  18. const buildingId = props.buildingId()
  19. useEffect(() => {
  20. getGroupList({
  21. params: {
  22. buildingId,
  23. pageNum: 1,
  24. pageSize: 999,
  25. },
  26. }).then(data => {
  27. setList(data.records || [])
  28. updateGroup(buildingId ? undefined : value);
  29. })
  30. }, [buildingId]);
  31. if (value !== group.groupId) {
  32. updateGroup(value);
  33. }
  34. const handleChange = val => {
  35. onChange(val)
  36. }
  37. return (
  38. <div>
  39. <div onClick={() => setVisible(true)}>{group.groupName}</div>
  40. <Modal
  41. title="请选择拼团"
  42. visible={visible}
  43. onOk={() => {
  44. updateGroup(value)
  45. onChange(value)
  46. setVisible(false)
  47. }}
  48. onCancel={() => setVisible(false)}
  49. >
  50. <Select
  51. showSearch
  52. value={value ? parseInt(value) : undefined}
  53. onChange={handleChange}
  54. style={{ width: '90%' }}
  55. filterOption={(input, option) =>
  56. option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
  57. }>
  58. {
  59. list.map(x => (<Select.Option key={x.groupActicityId} value={x.groupActicityId}>{x.activityName}</Select.Option>))
  60. }
  61. </Select>
  62. </Modal>
  63. </div>
  64. );
  65. }