-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy patheditable-section.js
147 lines (138 loc) · 4.09 KB
/
editable-section.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Field, Form, Formik } from 'formik'
import React, {Component} from 'react'
import {
Button,
Para,
Val
} from './styled'
/**
* This component will render a set of fields along with a 'Change/Edit' button
* that toggles an editable state. In its editable state, the fields/values are
* rendered as form inputs, which can be edited and then saved/persisted.
*/
export default class EditableSection extends Component {
state = {
isEditing: false
}
_exists = (val) => val !== null && typeof val !== 'undefined'
_getVal = (fieldName) => this._exists(this.props.request[fieldName])
? this.props.request[fieldName]
: ''
_onClickSave = data => {
const {intl, onChange, request} = this.props
// Convert all null values received to '',
// otherwise they will appear in the backend as the 'null' string.
for (const field in data) {
if (data[field] === null) data[field] = ''
}
onChange(request, data, intl)
this.setState({isEditing: false})
}
_toggleEditing = () => {
this.setState({isEditing: !this.state.isEditing})
}
render () {
const {children, fields, inputStyle, request, valueFirst} = this.props
const {isEditing} = this.state
if (!request) return null
return (
<Formik
// Force Formik to reload initialValues when we update them.
enableReinitialize
initialValues={request}
onSubmit={this._onClickSave}
>
{
formikProps => (
<Form>
{children}
<span
className='pull-right'
// Limit height so that it does not collide with form elements
// below when actively editing.
style={{height: '10px', marginRight: '15px'}}
>
{!isEditing
? <Button
bsSize='xsmall'
onClick={this._toggleEditing}
>
Change
</Button>
: <>
<Button
bsSize='xsmall'
bsStyle='link'
onClick={this._toggleEditing}
>
Cancel
</Button>
<Button
bsSize='xsmall'
bsStyle='link'
disabled={!formikProps.dirty}
type='submit'
>
Save
</Button>
</>
}
</span>
{fields.map(f => {
const input = (
<InputToggle
fieldName={f.fieldName}
inputProps={f.inputProps}
isEditing={isEditing}
key={f.fieldName}
options={f.options}
style={inputStyle}
value={this._getVal(f.fieldName)} />
)
return (
<Para key={f.fieldName}>
{valueFirst
? <>{input} {f.label}</>
: <>{f.label}: {input}</>
}
</Para>
)
})}
</Form>
)
}
</Formik>
)
}
}
/**
* This component renders either the specified value for a given field or, if
* in an active editing state, the associated field's respective input (e.g.
* text, number, or select).
*/
class InputToggle extends Component {
render () {
const {fieldName, inputProps, isEditing, options, style, value} = this.props
if (isEditing) {
if (options) {
return (
<Field
as='select'
name={fieldName}
>
{Object.keys(options).map(k =>
<option value={k}>{options[k]}</option>
)}
</Field>
)
} else {
return <Field
{...inputProps}
name={fieldName}
style={style}
/>
}
}
return <Val>{options ? options[value] : value}</Val>
}
}