-
Notifications
You must be signed in to change notification settings - Fork 18
/
JsonFunctionValue.js
205 lines (183 loc) · 6.43 KB
/
JsonFunctionValue.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Author: Alexandre Havrileck (Oxyno-zeta)
* Date: 13/11/16
* Licence: See Readme
*/
/* ************************************* */
/* ******** IMPORTS ******** */
/* ************************************* */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { HotKeys } from 'react-hotkeys';
import { isComponentWillChange } from '../utils/objectTypes';
import inputUsageTypes from '../types/inputUsageTypes';
/* ************************************* */
/* ******** VARIABLES ******** */
/* ************************************* */
// Prop types
const propTypes = {
name: PropTypes.string.isRequired,
value: PropTypes.any.isRequired,
originalValue: PropTypes.any,
keyPath: PropTypes.array,
deep: PropTypes.number,
handleRemove: PropTypes.func,
handleUpdateValue: PropTypes.func,
readOnly: PropTypes.func.isRequired,
dataType: PropTypes.string,
getStyle: PropTypes.func.isRequired,
editButtonElement: PropTypes.element,
cancelButtonElement: PropTypes.element,
textareaElementGenerator: PropTypes.func.isRequired,
minusMenuElement: PropTypes.element,
logger: PropTypes.object.isRequired,
onSubmitValueParser: PropTypes.func.isRequired,
};
// Default props
const defaultProps = {
keyPath: [],
deep: 0,
handleUpdateValue: () => {
},
editButtonElement: <button>e</button>,
cancelButtonElement: <button>c</button>,
minusMenuElement: <span> - </span>,
};
/* ************************************* */
/* ******** COMPONENT ******** */
/* ************************************* */
class JsonFunctionValue extends Component {
constructor(props) {
super(props);
const keyPath = [
...props.keyPath,
props.name,
];
this.state = {
value: props.value,
name: props.name,
keyPath,
deep: props.deep,
editEnabled: false,
inputRef: null,
};
// Bind
this.handleEditMode = this.handleEditMode.bind(this);
this.refInput = this.refInput.bind(this);
this.handleCancelEdit = this.handleCancelEdit.bind(this);
this.handleEdit = this.handleEdit.bind(this);
}
componentWillReceiveProps(nextProps) {
this.setState({
value: nextProps.value,
});
}
componentDidUpdate() {
const { editEnabled, inputRef, name, value, keyPath, deep } = this.state;
const { readOnly, dataType } = this.props;
const readOnlyResult = readOnly(name, value, keyPath, deep, dataType);
if (editEnabled && !readOnlyResult && (typeof inputRef.focus === 'function')) {
inputRef.focus();
}
}
handleEdit() {
const { handleUpdateValue, originalValue, logger, onSubmitValueParser, keyPath } = this.props;
const { inputRef, name, deep } = this.state;
const newValue = onSubmitValueParser(true, keyPath, deep, name, inputRef.value);
const result = {
value: newValue,
key: name,
};
// Run update
handleUpdateValue(result).then(() => {
// Cancel edit mode if necessary
if (!isComponentWillChange(originalValue, newValue)) {
this.handleCancelEdit();
}
}).catch(logger.error);
}
handleEditMode() {
this.setState({
editEnabled: true,
});
}
refInput(node) {
this.state.inputRef = node;
}
handleCancelEdit() {
this.setState({
editEnabled: false,
});
}
render() {
const { name, value, editEnabled, keyPath, deep } = this.state;
const {
handleRemove,
originalValue,
readOnly,
dataType,
getStyle,
editButtonElement,
cancelButtonElement,
textareaElementGenerator,
minusMenuElement,
keyPath: comeFromKeyPath,
} = this.props;
const style = getStyle(name, originalValue, keyPath, deep, dataType);
let result = null;
let minusElement = null;
const resultOnlyResult = readOnly(name, originalValue, keyPath, deep, dataType);
if (editEnabled && !resultOnlyResult) {
const textareaElement =
textareaElementGenerator(inputUsageTypes.VALUE, comeFromKeyPath, deep, name, originalValue, dataType);
const editButtonElementLayout = React.cloneElement(editButtonElement, {
onClick: this.handleEdit,
});
const cancelButtonElementLayout = React.cloneElement(cancelButtonElement, {
onClick: this.handleCancelEdit,
});
const textareaElementLayout = React.cloneElement(textareaElement, {
ref: this.refInput,
defaultValue: originalValue,
});
result = (<span className="rejt-edit-form" style={style.editForm}>
{textareaElementLayout} {cancelButtonElementLayout}{editButtonElementLayout}
</span>);
minusElement = null;
} else {
/* eslint-disable jsx-a11y/no-static-element-interactions */
result = (
<span
className="rejt-value" style={style.value} onClick={resultOnlyResult ? null : this.handleEditMode}
>
{value}
</span>
);
/* eslint-enable */
const minusMenuLayout = React.cloneElement(minusMenuElement, {
onClick: handleRemove,
className: 'rejt-minus-menu',
style: style.minus,
});
minusElement = (resultOnlyResult) ? null : minusMenuLayout;
}
const handlers = {
esc: this.handleCancelEdit,
enter: this.handleEdit,
};
return (
<HotKeys component={'li'} className="rejt-function-value-node" style={style.li} handlers={handlers}>
<span className="rejt-name" style={style.name}>{name} : </span>{result}
{minusElement}
</HotKeys>
);
}
}
// Add prop types
JsonFunctionValue.propTypes = propTypes;
// Add default props
JsonFunctionValue.defaultProps = defaultProps;
/* ************************************* */
/* ******** EXPORTS ******** */
/* ************************************* */
export default JsonFunctionValue;