-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
109 lines (95 loc) · 2.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { useState } from 'react'
import { Input, Form } from 'antd'
import PropTypes from 'prop-types'
import { useClickAway } from '@umijs/hooks'
import { isIp, isDomains } from '@/helpers/object'
import usePermissionModel from '@/stores/usePermissionModel'
import useHostsModel from '@/stores/useHostsModel'
import styles from '@/pages/index/components/EditableCell/index.less'
const EditArea = Form.create()(RawEditArea)
function EditableCell({ record, property }) {
const { acquired } = usePermissionModel()
const [editing, setEditing] = useState(false)
const { modifyHost } = useHostsModel()
const ref = useClickAway(() => {
setEditing(false)
})
if (!editing) {
return (
<div
style={{ height: '21px', width: '100%' }}
onDoubleClick={() => {
acquired && setEditing(true)
}}
>
{record[property]}
</div>
)
}
return (
<div ref={ref}>
<EditArea
record={record}
property={property}
onChange={val => {
setEditing(false)
modifyHost(record, {
...record,
[property]: val
})
}}
/>
</div>
)
}
EditableCell.propTypes = {
record: PropTypes.object,
property: PropTypes.string
}
export default EditableCell
function RawEditArea({ form, property, record, onChange }) {
const { getFieldDecorator, validateFields } = form
function modify(e) {
e.preventDefault()
validateFields((err, values) => {
if (err) {
return
}
onChange && onChange(values.host)
})
}
return (
<Form className={styles.editArea} onSubmit={modify}>
<Form.Item>
{getFieldDecorator('host', {
initialValue: record[property],
rules: [
{
validator(rule, value, callback) {
if (['ip', 'domain'].includes(property) && !value) {
return callback(`${property} is required`)
}
if ('ip' === property && !isIp(value)) {
return callback('incorrect ip format')
}
if ('domain' === property && !isDomains(value)) {
return callback('incorrect domains format')
}
if ('alias' === property && value.length > 15) {
return callback('alias cannot be longer than 15 characters')
}
callback()
}
}
]
})(<Input autoFocus style={{ height: '26px' }} />)}
</Form.Item>
</Form>
)
}
RawEditArea.propTypes = {
form: PropTypes.object,
record: PropTypes.object,
property: PropTypes.string,
onChange: PropTypes.func
}