AddForm.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { useMemo, useCallback, useImperativeHandle } from 'react'
  2. import { useSelector } from 'react-redux'
  3. import classnames from 'classnames'
  4. import debounce from 'lodash/debounce'
  5. import { Button, Form, Icon, Checkbox, Select, Spin } from 'antd'
  6. const Option = Select.Option
  7. import { WrappedFormUtils, FormComponentProps } from 'antd/lib/form/Form'
  8. const FormItem = Form.Item
  9. const styles = require('../Team.less')
  10. import { IInviteMemberProps } from '../types'
  11. import { uuid } from 'utils/util'
  12. import { makeSelectInviteMemberLoading } from '../selectors'
  13. const AddForm: React.FC<IInviteMemberProps & FormComponentProps> = (
  14. {
  15. form,
  16. category,
  17. addHandler,
  18. inviteMemberList,
  19. organizationDetail,
  20. handleSearchMember
  21. },
  22. ref
  23. ) => {
  24. useImperativeHandle(ref, () => ({ ...form }))
  25. const fetching = useSelector(makeSelectInviteMemberLoading())
  26. const debouncedSearch = useCallback(
  27. debounce((searchValue: string) => {
  28. if (searchValue && searchValue.length) {
  29. handleSearchMember(searchValue)
  30. }
  31. }, 1000),
  32. [handleSearchMember]
  33. )
  34. const submit = useCallback(() => {
  35. if (addHandler) {
  36. addHandler()
  37. }
  38. }, [addHandler])
  39. const getOptions = useMemo(() => {
  40. return inviteMemberList && inviteMemberList.length
  41. ? inviteMemberList.slice(0, 20).map((d) => (
  42. <Option key={`${uuid}${d.username}`} value={d.email}>
  43. <div className={styles.options}>
  44. <strong>{d.username}</strong>
  45. <span className={styles.email}>{d.email}</span>
  46. </div>
  47. </Option>
  48. ))
  49. : []
  50. }, [inviteMemberList])
  51. const { getFieldDecorator } = form
  52. return (
  53. <div className={styles.addFormWrapper}>
  54. <div className={styles.titleWrapper}>
  55. <div className={styles.icon}>
  56. <Icon type="user" />
  57. </div>
  58. <div className={styles.title}>
  59. 添加{category}到
  60. <span className={styles.orgName}>{organizationDetail?.name}</span>
  61. </div>
  62. </div>
  63. <div className={styles.search}>
  64. <Form>
  65. <Form.Item>
  66. {getFieldDecorator(
  67. 'members'
  68. )(
  69. <Select
  70. mode="tags"
  71. placeholder={'请搜索或粘贴用户名/邮箱(以英文逗号或英文分号分隔)'}
  72. tokenSeparators={[',', ';']}
  73. showSearch
  74. filterOption={false}
  75. onSearch={debouncedSearch}
  76. notFoundContent={fetching ? <Spin size="small" /> : null}
  77. >
  78. {getOptions}
  79. </Select>
  80. )}
  81. </Form.Item>
  82. <FormItem>
  83. {getFieldDecorator(
  84. 'needEmail',
  85. {
  86. initialValue: true,
  87. valuePropName: 'checked'
  88. }
  89. )(<Checkbox>需要被邀请成员邮件确认</Checkbox>)}
  90. </FormItem>
  91. </Form>
  92. </div>
  93. <div className={styles.submit}>
  94. <Button type="primary" onClick={submit}>
  95. 邀请
  96. </Button>
  97. </div>
  98. </div>
  99. )
  100. }
  101. export default Form.create<IInviteMemberProps & FormComponentProps>()(
  102. React.forwardRef<WrappedFormUtils>(AddForm)
  103. )