ScheduleMailConfig.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, {
  21. useEffect,
  22. useState,
  23. useCallback,
  24. useImperativeHandle,
  25. forwardRef
  26. } from 'react'
  27. import { Form, Row, Col, Input, Select, Icon, InputNumber, Spin } from 'antd'
  28. const FormItem = Form.Item
  29. const { Option } = Select
  30. import MailTag from './MailTag'
  31. import { FormComponentProps } from 'antd/lib/form'
  32. import { IScheduleMailConfig, IUserInfo } from './types'
  33. import {
  34. FormItemStyle,
  35. LongFormItemStyle,
  36. DefaultMailImageWidth,
  37. DefaultEmailContent
  38. } from './constants'
  39. import { RichText, RichTextNode } from 'components/RichText'
  40. interface IScheduleMailConfigProps
  41. extends FormComponentProps<IScheduleMailConfig> {
  42. config: IScheduleMailConfig
  43. loading: boolean
  44. onLoadMailList: (keyword: string) => void
  45. mailList: IUserInfo[]
  46. }
  47. export const ScheduleMailConfig: React.FC<IScheduleMailConfigProps> = (
  48. props,
  49. ref
  50. ) => {
  51. const { form, config, loading, mailList, onLoadMailList } = props
  52. const { getFieldDecorator } = form
  53. const [showBcc, setShowBcc] = useState(false)
  54. const ccLabel = (
  55. <span>
  56. <span>抄送</span>
  57. <Icon
  58. style={{ marginLeft: 8 }}
  59. type={showBcc ? 'up-circle' : 'down-circle'}
  60. onClick={() => setShowBcc(!showBcc)}
  61. />
  62. </span>
  63. )
  64. const resetMailList = useCallback(
  65. () => {
  66. onLoadMailList('')
  67. },
  68. [onLoadMailList]
  69. )
  70. useEffect(
  71. () => {
  72. if (config.bcc) {
  73. setShowBcc(true)
  74. }
  75. form.setFieldsValue({ content: config.content || DefaultEmailContent })
  76. },
  77. [config]
  78. )
  79. const checkContentMaxLength = (
  80. _,
  81. value: RichTextNode[],
  82. callback: (msg?: string) => void
  83. ) => {
  84. if (new Blob([JSON.stringify(value)]).size / 1024 > 60) {
  85. callback('邮件内容长度过长(不超过 64Kb)')
  86. return
  87. }
  88. callback()
  89. }
  90. useImperativeHandle(ref, () => ({ form }))
  91. return (
  92. <Form>
  93. <FormItem label="主题" {...LongFormItemStyle}>
  94. {getFieldDecorator<IScheduleMailConfig>('subject', {
  95. rules: [{ required: true, message: '主题不能为空' }],
  96. initialValue: config.subject
  97. })(<Input />)}
  98. </FormItem>
  99. <Row>
  100. <Col span={12}>
  101. <FormItem label="文件类型" {...FormItemStyle}>
  102. {getFieldDecorator<IScheduleMailConfig>('type', {
  103. rules: [{ required: true }],
  104. initialValue: config.type
  105. })(
  106. <Select>
  107. <Option value="excel">Excel</Option>
  108. <Option value="image">图片</Option>
  109. <Option value="imageAndExcel">图片 + Excel</Option>
  110. </Select>
  111. )}
  112. </FormItem>
  113. </Col>
  114. <Col span={12}>
  115. {form.getFieldValue('type') !== 'excel' && (
  116. <FormItem label="图片宽度" {...FormItemStyle}>
  117. {getFieldDecorator<IScheduleMailConfig>('imageWidth', {
  118. rules: [{ required: true }],
  119. initialValue: config.imageWidth || DefaultMailImageWidth
  120. })(<InputNumber min={100} />)}{' '}
  121. 像素
  122. </FormItem>
  123. )}
  124. </Col>
  125. </Row>
  126. <FormItem label="收件人" {...LongFormItemStyle}>
  127. {getFieldDecorator<IScheduleMailConfig>('to', {
  128. rules: [{ required: true, message: '请选择收件人' }],
  129. initialValue: config.to
  130. })(
  131. <MailTag
  132. allowCreate
  133. dataSource={mailList}
  134. onLoadDataSource={onLoadMailList}
  135. onBlur={resetMailList}
  136. />
  137. )}
  138. </FormItem>
  139. <FormItem label={ccLabel} {...LongFormItemStyle}>
  140. {getFieldDecorator<IScheduleMailConfig>('cc', {
  141. initialValue: config.cc
  142. })(
  143. <MailTag
  144. allowCreate
  145. dataSource={mailList}
  146. onLoadDataSource={onLoadMailList}
  147. onBlur={resetMailList}
  148. />
  149. )}
  150. </FormItem>
  151. {showBcc && (
  152. <FormItem label="密送" {...LongFormItemStyle}>
  153. {getFieldDecorator<IScheduleMailConfig>('bcc', {
  154. initialValue: config.bcc
  155. })(
  156. <MailTag
  157. allowCreate
  158. dataSource={mailList}
  159. onLoadDataSource={onLoadMailList}
  160. onBlur={resetMailList}
  161. />
  162. )}
  163. </FormItem>
  164. )}
  165. <FormItem label="内容" {...LongFormItemStyle}>
  166. {loading ? (
  167. <Spin />
  168. ) : (
  169. getFieldDecorator<IScheduleMailConfig>('content', {
  170. validateFirst: true,
  171. rules: [
  172. { required: true, message: '请输入邮件文本内容' },
  173. {
  174. validator: checkContentMaxLength
  175. }
  176. ],
  177. initialValue: config.content
  178. })(
  179. <RichText />
  180. )
  181. )}
  182. </FormItem>
  183. </Form>
  184. )
  185. }
  186. export default Form.create<IScheduleMailConfigProps>()(
  187. forwardRef(ScheduleMailConfig)
  188. )