AddMessage.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { useRef } from 'react'
  2. import { Button, Form, message } from 'antd'
  3. import {
  4. ModalForm,
  5. ProFormDependency,
  6. ProFormRadio,
  7. ProFormSelect,
  8. ProFormText
  9. } from '@ant-design/pro-form'
  10. import RighEditor from './RighEditor'
  11. import { Plus } from '@icon-park/react'
  12. export const msgTypeEnum = {
  13. NotificationOne: 'notification',
  14. Message: 'message'
  15. }
  16. export const mediaType = {
  17. VersionUpdate: 1,
  18. HolidayNotice: 2
  19. }
  20. const AddMessage = props => {
  21. const { onConfirm } = props
  22. const formRef = useRef(null)
  23. const handleFormValues = values => {
  24. const newValues = { ...values }
  25. switch (values.msgType) {
  26. case msgTypeEnum.NotificationOne:
  27. switch (values.media) {
  28. case mediaType.VersionUpdate:
  29. newValues.avatar = 'https://zhcld.com/resource/notice_email.png'
  30. break
  31. case mediaType.HolidayNotice:
  32. newValues.avatar = 'https://zhcld.com/resource/notice_airplane.png'
  33. break
  34. default:
  35. break
  36. }
  37. break
  38. default:
  39. break
  40. }
  41. return newValues
  42. }
  43. return (
  44. <div>
  45. <ModalForm
  46. isKeyPressSubmit={false}
  47. key="modalForm"
  48. formRef={formRef}
  49. trigger={
  50. <Button type="primary">
  51. <Plus className="mr-1" />
  52. 新建通知
  53. </Button>
  54. }
  55. initialValues={{ msgType: 'notification', refresh: 0 }}
  56. onFinish={async values => {
  57. try {
  58. const nValues = handleFormValues(values)
  59. await onConfirm(nValues)
  60. formRef.current?.resetFields()
  61. message.success('添加成功')
  62. return true
  63. } catch (error) {
  64. return false
  65. }
  66. }}>
  67. <ProFormText
  68. name="title"
  69. label="标题"
  70. placeholder="请输入标题"
  71. rules={[{ required: true, message: '请输入标题' }]}
  72. />
  73. <Form.Item
  74. label="内容"
  75. name="content"
  76. rules={[{ required: true, message: '请输入通知内容' }]}>
  77. <RighEditor />
  78. </Form.Item>
  79. <ProFormRadio.Group
  80. label="类型"
  81. name="msgType"
  82. required
  83. options={[
  84. { label: '通知', value: msgTypeEnum.NotificationOne }
  85. // { label: '消息', value: msgTypeEnum.Message }
  86. ]}
  87. />
  88. <ProFormDependency name={['msgType']}>
  89. {({ msgType }) =>
  90. msgType === msgTypeEnum.NotificationOne ? (
  91. <ProFormSelect
  92. name="media"
  93. label="通知类型"
  94. rules={[{ required: true, message: '请选择类型' }]}
  95. options={[
  96. { label: '版本更新', value: mediaType.VersionUpdate },
  97. { label: '节假通知', value: mediaType.HolidayNotice }
  98. ]}
  99. />
  100. ) : null
  101. }
  102. </ProFormDependency>
  103. <ProFormDependency name={['msgType', 'media']}>
  104. {({ msgType, media }) =>
  105. msgType === msgTypeEnum.NotificationOne && media === mediaType.VersionUpdate ? (
  106. <ProFormRadio.Group
  107. name="refresh"
  108. rules={[{ required: true, message: '请选择刷新机制' }]}
  109. options={[
  110. { label: '手动刷新', value: 0 },
  111. { label: '强制刷新', value: 1 }
  112. ]}
  113. />
  114. ) : null
  115. }
  116. </ProFormDependency>
  117. </ModalForm>
  118. </div>
  119. )
  120. }
  121. export default AddMessage