• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mac-s-g / react-json-view / #1215

pending completion
#1215

push

web-flow
Merge pull request #342 from mac-s-g/bump-patch

bump patch version

284 of 349 branches covered (81.38%)

Branch coverage included in aggregate %.

504 of 597 relevant lines covered (84.42%)

90.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.41
/src/js/components/VariableEditor.js
1
import React from 'react';
2
import AutosizeTextarea from 'react-textarea-autosize';
3

4
import { toType } from './../helpers/util';
5
import dispatcher from './../helpers/dispatcher';
6
import parseInput from './../helpers/parseInput';
7
import stringifyVariable from './../helpers/stringifyVariable';
8
import CopyToClipboard from './CopyToClipboard';
9

10
//data type components
11
import {
12
    JsonBoolean,
13
    JsonDate,
14
    JsonFloat,
15
    JsonFunction,
16
    JsonInteger,
17
    JsonNan,
18
    JsonNull,
19
    JsonRegexp,
20
    JsonString,
21
    JsonUndefined
22
} from './DataTypes/DataTypes';
23

24
//clibboard icon
25
import { Edit, CheckCircle, RemoveCircle as Remove } from './icons';
26

27
//theme
28
import Theme from './../themes/getStyle';
29

30
class VariableEditor extends React.PureComponent {
31
    constructor(props) {
32
        super(props);
170✔
33
        this.state = {
170✔
34
            editMode: false,
35
            editValue: '',
36
            hovered: false,
37
            renameKey: false,
38
            parsedInput: {
39
                type: false,
40
                value: null
41
            }
42
        };
43
    }
44

45
    render() {
46
        const {
47
            variable,
48
            singleIndent,
49
            type,
50
            theme,
51
            namespace,
52
            indentWidth,
53
            enableClipboard,
54
            onEdit,
55
            onDelete,
56
            onSelect,
57
            displayArrayKey,
58
            quotesOnKeys
59
        } = this.props;
240✔
60
        const { editMode } = this.state;
240✔
61
        return (
240✔
62
            <div
63
                {...Theme(theme, 'objectKeyVal', {
64
                    paddingLeft: indentWidth * singleIndent
65
                })}
66
                onMouseEnter={() =>
67
                    this.setState({ ...this.state, hovered: true })
×
68
                }
69
                onMouseLeave={() =>
70
                    this.setState({ ...this.state, hovered: false })
×
71
                }
72
                class="variable-row"
73
                key={variable.name}
74
            >
75
                {type == 'array' ? (
240✔
76
                    displayArrayKey ? (
52✔
77
                        <span
78
                            {...Theme(theme, 'array-key')}
79
                            key={variable.name + '_' + namespace}
80
                        >
81
                            {variable.name}
82
                            <div {...Theme(theme, 'colon')}>:</div>
83
                        </span>
84
                    ) : null
85
                ) : (
86
                    <span>
87
                        <span
88
                            {...Theme(theme, 'object-name')}
89
                            class="object-key"
90
                            key={variable.name + '_' + namespace}
91
                        >
92
                            {!!quotesOnKeys && (
290✔
93
                                <span style={{ verticalAlign: 'top' }}>"</span>
94
                            )}
95
                            <span style={{ display: 'inline-block' }}>
96
                                {variable.name}
97
                            </span>
98
                            {!!quotesOnKeys && (
290✔
99
                                <span style={{ verticalAlign: 'top' }}>"</span>
100
                            )}
101
                        </span>
102
                        <span {...Theme(theme, 'colon')}>:</span>
103
                    </span>
104
                )}
105
                <div
106
                    class="variable-value"
107
                    onClick={
108
                        onSelect === false && onEdit === false
598✔
109
                            ? null
110
                            : e => {
111
                                  let location = [...namespace];
×
112
                                  if (
×
113
                                      (e.ctrlKey || e.metaKey) &&
×
114
                                      onEdit !== false
115
                                  ) {
116
                                      this.prepopInput(variable);
×
117
                                  } else if (onSelect !== false) {
×
118
                                      location.shift();
×
119
                                      onSelect({
×
120
                                          ...variable,
121
                                          namespace: location
122
                                      });
123
                                  }
124
                              }
125
                    }
126
                    {...Theme(theme, 'variableValue', {
127
                        cursor: onSelect === false ? 'default' : 'pointer'
240✔
128
                    })}
129
                >
130
                    {this.getValue(variable, editMode)}
131
                </div>
132
                {enableClipboard ? (
240✔
133
                    <CopyToClipboard
134
                        rowHovered={this.state.hovered}
135
                        hidden={editMode}
136
                        src={variable.value}
137
                        clickCallback={enableClipboard}
138
                        {...{ theme, namespace: [...namespace, variable.name] }}
139
                    />
140
                ) : null}
141
                {onEdit !== false && editMode == false
600✔
142
                    ? this.getEditIcon()
143
                    : null}
144
                {onDelete !== false && editMode == false
602✔
145
                    ? this.getRemoveIcon()
146
                    : null}
147
            </div>
148
        );
149
    }
150

151
    getEditIcon = () => {
170✔
152
        const { variable, theme } = this.props;
88✔
153

154
        return (
88✔
155
            <div
156
                class="click-to-edit"
157
                style={{
158
                    verticalAlign: 'top',
159
                    display: this.state.hovered ? 'inline-block' : 'none'
88!
160
                }}
161
            >
162
                <Edit
163
                    class="click-to-edit-icon"
164
                    {...Theme(theme, 'editVarIcon')}
165
                    onClick={() => {
166
                        this.prepopInput(variable);
26✔
167
                    }}
168
                />
169
            </div>
170
        );
171
    };
172

173
    prepopInput = variable => {
170✔
174
        if (this.props.onEdit !== false) {
26!
175
            const stringifiedValue = stringifyVariable(variable.value);
26✔
176
            const detected = parseInput(stringifiedValue);
26✔
177
            this.setState({
26✔
178
                editMode: true,
179
                editValue: stringifiedValue,
180
                parsedInput: {
181
                    type: detected.type,
182
                    value: detected.value
183
                }
184
            });
185
        }
186
    };
187

188
    getRemoveIcon = () => {
170✔
189
        const { variable, namespace, theme, rjvId } = this.props;
90✔
190

191
        return (
90✔
192
            <div
193
                class="click-to-remove"
194
                style={{
195
                    verticalAlign: 'top',
196
                    display: this.state.hovered ? 'inline-block' : 'none'
90!
197
                }}
198
            >
199
                <Remove
200
                    class="click-to-remove-icon"
201
                    {...Theme(theme, 'removeVarIcon')}
202
                    onClick={() => {
203
                        dispatcher.dispatch({
×
204
                            name: 'VARIABLE_REMOVED',
205
                            rjvId: rjvId,
206
                            data: {
207
                                name: variable.name,
208
                                namespace: namespace,
209
                                existing_value: variable.value,
210
                                variable_removed: true
211
                            }
212
                        });
213
                    }}
214
                />
215
            </div>
216
        );
217
    };
218

219
    getValue = (variable, editMode) => {
170✔
220
        const type = editMode ? false : variable.type;
240✔
221
        const { props } = this;
240✔
222
        switch (type) {
240!
223
            case false:
224
                return this.getEditInput();
32✔
225
            case 'string':
226
                return <JsonString value={variable.value} {...props} />;
82✔
227
            case 'integer':
228
                return <JsonInteger value={variable.value} {...props} />;
48✔
229
            case 'float':
230
                return <JsonFloat value={variable.value} {...props} />;
4✔
231
            case 'boolean':
232
                return <JsonBoolean value={variable.value} {...props} />;
18✔
233
            case 'function':
234
                return <JsonFunction value={variable.value} {...props} />;
10✔
235
            case 'null':
236
                return <JsonNull {...props} />;
10✔
237
            case 'nan':
238
                return <JsonNan {...props} />;
10✔
239
            case 'undefined':
240
                return <JsonUndefined {...props} />;
4✔
241
            case 'date':
242
                return <JsonDate value={variable.value} {...props} />;
×
243
            case 'regexp':
244
                return <JsonRegexp value={variable.value} {...props} />;
8✔
245
            default:
246
                // catch-all for types that weren't anticipated
247
                return (
14✔
248
                    <div class="object-value">
249
                        {JSON.stringify(variable.value)}
250
                    </div>
251
                );
252
        }
253
    };
254

255
    getEditInput = () => {
170✔
256
        const { theme } = this.props;
32✔
257
        const { editValue } = this.state;
32✔
258

259
        return (
32✔
260
            <div>
261
                <AutosizeTextarea
262
                    type="text"
263
                    inputRef={input => input && input.focus()}
×
264
                    value={editValue}
265
                    class="variable-editor"
266
                    onChange={event => {
267
                        const value = event.target.value;
×
268
                        const detected = parseInput(value);
×
269
                        this.setState({
×
270
                            editValue: value,
271
                            parsedInput: {
272
                                type: detected.type,
273
                                value: detected.value
274
                            }
275
                        });
276
                    }}
277
                    onKeyDown={e => {
278
                        switch (e.key) {
×
279
                            case 'Escape': {
280
                                this.setState({
×
281
                                    editMode: false,
282
                                    editValue: ''
283
                                });
284
                                break;
×
285
                            }
286
                            case 'Enter': {
287
                                if (e.ctrlKey || e.metaKey) {
×
288
                                    this.submitEdit(true);
×
289
                                }
290
                                break;
×
291
                            }
292
                        }
293
                        e.stopPropagation();
×
294
                    }}
295
                    placeholder="update this value"
296
                    minRows={2}
297
                    {...Theme(theme, 'edit-input')}
298
                />
299
                <div {...Theme(theme, 'edit-icon-container')}>
300
                    <Remove
301
                        class="edit-cancel"
302
                        {...Theme(theme, 'cancel-icon')}
303
                        onClick={() => {
304
                            this.setState({ editMode: false, editValue: '' });
4✔
305
                        }}
306
                    />
307
                    <CheckCircle
308
                        class="edit-check string-value"
309
                        {...Theme(theme, 'check-icon')}
310
                        onClick={() => {
311
                            this.submitEdit();
2✔
312
                        }}
313
                    />
314
                    <div>{this.showDetected()}</div>
315
                </div>
316
            </div>
317
        );
318
    };
319

320
    submitEdit = submit_detected => {
170✔
321
        const { variable, namespace, rjvId } = this.props;
2✔
322
        const { editValue, parsedInput } = this.state;
2✔
323
        let new_value = editValue;
2✔
324
        if (submit_detected && parsedInput.type) {
2!
325
            new_value = parsedInput.value;
×
326
        }
327
        this.setState({
2✔
328
            editMode: false
329
        });
330
        dispatcher.dispatch({
2✔
331
            name: 'VARIABLE_UPDATED',
332
            rjvId: rjvId,
333
            data: {
334
                name: variable.name,
335
                namespace: namespace,
336
                existing_value: variable.value,
337
                new_value: new_value,
338
                variable_removed: false
339
            }
340
        });
341
    };
342

343
    showDetected = () => {
170✔
344
        const { theme, variable, namespace, rjvId } = this.props;
32✔
345
        const { type, value } = this.state.parsedInput;
32✔
346
        const detected = this.getDetectedInput();
32✔
347
        if (detected) {
32✔
348
            return (
16✔
349
                <div>
350
                    <div {...Theme(theme, 'detected-row')}>
351
                        {detected}
352
                        <CheckCircle
353
                            class="edit-check detected"
354
                            style={{
355
                                verticalAlign: 'top',
356
                                paddingLeft: '3px',
357
                                ...Theme(theme, 'check-icon').style
358
                            }}
359
                            onClick={() => {
360
                                this.submitEdit(true);
×
361
                            }}
362
                        />
363
                    </div>
364
                </div>
365
            );
366
        }
367
    };
368

369
    getDetectedInput = () => {
170✔
370
        const { parsedInput } = this.state;
32✔
371
        const { type, value } = parsedInput;
32✔
372
        const { props } = this;
32✔
373
        const { theme } = props;
32✔
374

375
        if (type !== false) {
32✔
376
            switch (type.toLowerCase()) {
16!
377
                case 'object':
378
                    return (
2✔
379
                        <span>
380
                            <span
381
                                style={{
382
                                    ...Theme(theme, 'brace').style,
383
                                    cursor: 'default'
384
                                }}
385
                            >
386
                                {'{'}
387
                            </span>
388
                            <span
389
                                style={{
390
                                    ...Theme(theme, 'ellipsis').style,
391
                                    cursor: 'default'
392
                                }}
393
                            >
394
                                ...
395
                            </span>
396
                            <span
397
                                style={{
398
                                    ...Theme(theme, 'brace').style,
399
                                    cursor: 'default'
400
                                }}
401
                            >
402
                                {'}'}
403
                            </span>
404
                        </span>
405
                    );
406
                case 'array':
407
                    return (
2✔
408
                        <span>
409
                            <span
410
                                style={{
411
                                    ...Theme(theme, 'brace').style,
412
                                    cursor: 'default'
413
                                }}
414
                            >
415
                                {'['}
416
                            </span>
417
                            <span
418
                                style={{
419
                                    ...Theme(theme, 'ellipsis').style,
420
                                    cursor: 'default'
421
                                }}
422
                            >
423
                                ...
424
                            </span>
425
                            <span
426
                                style={{
427
                                    ...Theme(theme, 'brace').style,
428
                                    cursor: 'default'
429
                                }}
430
                            >
431
                                {']'}
432
                            </span>
433
                        </span>
434
                    );
435
                case 'string':
436
                    return <JsonString value={value} {...props} />;
×
437
                case 'integer':
438
                    return <JsonInteger value={value} {...props} />;
4✔
439
                case 'float':
440
                    return <JsonFloat value={value} {...props} />;
2✔
441
                case 'boolean':
442
                    return <JsonBoolean value={value} {...props} />;
×
443
                case 'function':
444
                    return <JsonFunction value={value} {...props} />;
×
445
                case 'null':
446
                    return <JsonNull {...props} />;
2✔
447
                case 'nan':
448
                    return <JsonNan {...props} />;
2✔
449
                case 'undefined':
450
                    return <JsonUndefined {...props} />;
2✔
451
                case 'date':
452
                    return <JsonDate value={new Date(value)} {...props} />;
×
453
            }
454
        }
455
    };
456
}
457

458
//export component
459
export default VariableEditor;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc