ResetConnectionModal.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, { useCallback, useRef, useEffect } from 'react'
  21. import { Modal, Form, Input } from 'antd'
  22. const FormItem = Form.Item
  23. import { FormComponentProps } from 'antd/lib/form'
  24. import { SourceResetConnectionProperties } from './types'
  25. import { ISource } from '../types'
  26. const formItemStyle = {
  27. labelCol: { span: 6 },
  28. wrapperCol: { span: 16 }
  29. }
  30. interface IResetConnectionModalProps extends FormComponentProps {
  31. visible: boolean
  32. source: ISource
  33. onConfirm: (properties: SourceResetConnectionProperties) => void
  34. onCancel: () => void
  35. }
  36. const ResetConnectionModal: React.FC<IResetConnectionModalProps> = (props) => {
  37. const { form, source, visible, onConfirm, onCancel } = props
  38. if (!source) {
  39. return null
  40. }
  41. const usernameRef = useRef<Input>(null)
  42. useEffect(
  43. () => {
  44. if (visible && usernameRef.current) {
  45. usernameRef.current.focus()
  46. }
  47. },
  48. [visible]
  49. )
  50. const { getFieldDecorator } = form
  51. const save = useCallback(
  52. () => {
  53. form.validateFields((err, values) => {
  54. if (err) {
  55. return
  56. }
  57. const { id: sourceId } = source
  58. const { username, password } = values
  59. onConfirm({ sourceId, username, password })
  60. })
  61. },
  62. [source, onConfirm]
  63. )
  64. const resetForm = useCallback(() => {
  65. form.resetFields()
  66. }, [])
  67. return (
  68. <Modal
  69. title={`重置 ${source.name} 连接`}
  70. wrapClassName="ant-modal-small"
  71. maskClosable={false}
  72. visible={visible}
  73. afterClose={resetForm}
  74. onCancel={onCancel}
  75. onOk={save}
  76. >
  77. <Form>
  78. <FormItem label="用户名" {...formItemStyle}>
  79. {getFieldDecorator('username', {
  80. rules: [{ required: true, message: '用户名不能为空' }],
  81. initialValue: ''
  82. })(
  83. // use 'new-[fieldname]' to avoid auto complete in Chrome
  84. // https://bugs.chromium.org/p/chromium/issues/detail?id=370363#c7
  85. <Input ref={usernameRef} autoFocus autoComplete="new-username" />
  86. )}
  87. </FormItem>
  88. <FormItem label="密码" {...formItemStyle}>
  89. {getFieldDecorator('password', {
  90. rules: [{ required: true, message: '密码不能为空' }],
  91. initialValue: ''
  92. })(<Input.Password autoComplete="new-password" />)}
  93. </FormItem>
  94. </Form>
  95. </Modal>
  96. )
  97. }
  98. export default Form.create<IResetConnectionModalProps>()(ResetConnectionModal)