Skip to content

Commit 8facd2e

Browse files
committed
Fix #55: Use other symbol than dash for joining coordinates
From now on, the section sign (§) will be used for creating coordinates.
1 parent ef8339b commit 8facd2e

File tree

5 files changed

+47
-32
lines changed

5 files changed

+47
-32
lines changed

src/data.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {normalizeKeyword, getKeyword} from './util';
2+
import {FILLER} from './constants';
23

34

45
export function getBlankObject(schema, getRef) {
@@ -115,24 +116,22 @@ function getSyncedArray(data, schema, getRef) {
115116
let type = normalizeKeyword(schema.items.type);
116117
let minItems = schema.minItems || schema.min_items || 0;
117118

118-
const filler = '__JSONRORM_FILLER__'; // filler for minItems
119-
120119
while (data.length < minItems)
121-
data.push(filler);
120+
data.push(FILLER);
122121

123122
for (let i = 0; i < data.length; i++) {
124123
let item = data[i];
125124

126125
if (type === 'array') {
127-
if (item === filler)
126+
if (item === FILLER)
128127
item = [];
129128
newData[i] = getSyncedArray(item, schema.items, getRef);
130129
} else if (type === 'object') {
131-
if (item === filler)
130+
if (item === FILLER)
132131
item = {};
133132
newData[i] = getSyncedObject(item, schema.items, getRef);
134133
} else {
135-
if (item === filler) {
134+
if (item === FILLER) {
136135
if (type === 'integer' || type === 'number')
137136
newData[i] = schema.items.default === 0 ? 0 : (schema.items.default || null);
138137
else if (type === 'boolean')

src/dataValidation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {normalizeKeyword, getKeyword, getKey} from './util';
1+
import {normalizeKeyword, getKeyword, getKey, joinCoords} from './util';
2+
import {JOIN_SYMBOL} from './constants';
23
import EditorState from './editorState';
34

45

@@ -70,9 +71,9 @@ export default function DataValidator(schema) {
7071
};
7172

7273
this.joinCoords = function(coords) {
73-
let c = coords.join('-');
74+
let c = joinCoords(coords);
7475

75-
if (c.startsWith('-'))
76+
if (c.startsWith(JOIN_SYMBOL))
7677
c = c.slice(1);
7778

7879
return c;

src/form.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import {getArrayFormRow, getObjectFormRow} from './ui';
3-
import {EditorContext} from './util';
3+
import {EditorContext, joinCoords, splitCoords} from './util';
4+
import {FIELD_NAME_PREFIX} from './constants';
45
import EditorState from './editorState';
56

67

@@ -14,7 +15,7 @@ export default class ReactJSONForm extends React.Component {
1415
1516
This first coordinate is not important and should be removed.
1617
*/
17-
coords = coords.split('-');
18+
coords = splitCoords(coords);
1819

1920
coords.shift(); // remove first coord
2021

@@ -45,7 +46,7 @@ export default class ReactJSONForm extends React.Component {
4546
let args = {
4647
data: data,
4748
schema: schema,
48-
name: 'rjf',
49+
name: FIELD_NAME_PREFIX,
4950
onChange: this.handleChange,
5051
onAdd: this.addFieldset,
5152
onRemove: this.removeFieldset,
@@ -66,7 +67,7 @@ export default class ReactJSONForm extends React.Component {
6667
}
6768

6869
addFieldset = (blankData, coords) => {
69-
coords = coords.split('-');
70+
coords = splitCoords(coords);
7071
coords.shift();
7172

7273
// :TODO: use immutable JS instead of JSON-ising the data
@@ -76,7 +77,7 @@ export default class ReactJSONForm extends React.Component {
7677
}
7778

7879
removeFieldset = (coords) => {
79-
coords = coords.split('-');
80+
coords = splitCoords(coords);
8081
coords.shift();
8182

8283
// :TODO: use immutable JS instead of JSON-ising the data
@@ -92,10 +93,10 @@ export default class ReactJSONForm extends React.Component {
9293
oldCoords willbe removed
9394
*/
9495

95-
newCoords = newCoords.split('-');
96+
newCoords = splitCoords(newCoords);
9697
newCoords.shift();
9798

98-
oldCoords = oldCoords.split('-');
99+
oldCoords = splitCoords(oldCoords);
99100
oldCoords.shift();
100101

101102
let data = addDataUsingCoords(newCoords, JSON.parse(JSON.stringify(this.props.editorState.getData())), value);
@@ -106,10 +107,10 @@ export default class ReactJSONForm extends React.Component {
106107
}
107108

108109
moveFieldset = (oldCoords, newCoords) => {
109-
oldCoords = oldCoords.split("-");
110+
oldCoords = splitCoords(oldCoords);
110111
oldCoords.shift();
111112

112-
newCoords = newCoords.split("-");
113+
newCoords = splitCoords(newCoords);
113114
newCoords.shift();
114115

115116
// :TODO: use immutable JS instead of JSON-ising the data

src/ui.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {getBlankData} from './data';
22
import {Button, FormInput, FormCheckInput, FormRadioInput, FormSelectInput,
33
FormFileInput, FormRow, FormGroup, GroupTitle, FormRowControls, FormTextareaInput,
44
FormDateTimeInput, FormMultiSelectInput, FileUploader, AutoCompleteInput} from './components';
5-
import {getVerboseName, convertType, getCoordsFromName, getKeyword, normalizeKeyword} from './util';
5+
import {getVerboseName, convertType, getCoordsFromName, getKeyword, normalizeKeyword,
6+
joinCoords, splitCoords} from './util';
67

78

89
function handleChange(e, fieldType, callback) {
@@ -240,17 +241,17 @@ export function getArrayFormRow(args) {
240241

241242
for (let i = 0; i < data.length; i++) {
242243
nextArgs.data = data[i];
243-
nextArgs.name = name + '-' + i;
244+
nextArgs.name = joinCoords(name, i);
244245

245246
if (i === 0)
246247
nextArgs.onMoveUp = null;
247248
else
248-
nextArgs.onMoveUp = (e) => onMove(name + '-' + i, name + '-' + (i - 1));
249+
nextArgs.onMoveUp = (e) => onMove(joinCoords(name, i), joinCoords(name, i - 1));
249250

250251
if (i === data.length - 1)
251252
nextArgs.onMoveDown = null;
252253
else
253-
nextArgs.onMoveDown = (e) => onMove(name + '-' + i, name + '-' + (i + 1));
254+
nextArgs.onMoveDown = (e) => onMove(joinCoords(name, i), joinCoords(name, i + 1));
254255

255256
if (type === 'array') {
256257
groups.push(getArrayFormRow(nextArgs));
@@ -312,9 +313,9 @@ export function getArrayFormRow(args) {
312313
{groups.map((i, index) => (
313314
<div className="rjf-form-group-wrapper" key={'group_wrapper_' + name + '_' + index}>
314315
<FormRowControls
315-
onRemove={removable ? (e) => onRemove(name + '-' + index) : null}
316-
onMoveUp={index > 0 ? (e) => onMove(name + '-' + index, name + '-' + (index - 1)) : null}
317-
onMoveDown={index < groups.length - 1 ? (e) => onMove(name + '-' + index, name + '-' + (index + 1)) : null}
316+
onRemove={removable ? (e) => onRemove(joinCoords(name, index)) : null}
317+
onMoveUp={index > 0 ? (e) => onMove(joinCoords(name, index), joinCoords(name, index - 1)) : null}
318+
onMoveDown={index < groups.length - 1 ? (e) => onMove(joinCoords(name, index), joinCoords(name, index + 1)) : null}
318319
/>
319320
{i}
320321
</div>
@@ -354,7 +355,7 @@ export function getObjectFormRow(args) {
354355
for (let i = 0; i < keys.length; i++) {
355356
let key = keys[i];
356357
let value = data[key];
357-
let childName = name + '-' + key;
358+
let childName = joinCoords(name, key);
358359
let schemaValue = schema_keys.hasOwnProperty(key) ? {...schema_keys[key]} : undefined;
359360

360361
if (typeof schemaValue === 'undefined') {
@@ -458,7 +459,7 @@ function handleKeyValueAdd(data, coords, onAdd, newSchema, getRef) {
458459
else if (data.hasOwnProperty(key))
459460
alert("(!) Duplicate keys not allowed. This key already exists.\r\n\r\n‎");
460461
else
461-
onAdd(getBlankData(newSchema, getRef), coords + '-' + key);
462+
onAdd(getBlankData(newSchema, getRef), joinCoords(coords, key));
462463
}
463464

464465

@@ -477,10 +478,10 @@ function handleKeyEdit(data, key, value, coords, onEdit) {
477478
else if (data.hasOwnProperty(newKey))
478479
return alert("(!) Duplicate keys not allowed. This key already exists.\r\n\r\n‎");
479480

480-
let newCoords = coords.split('-');
481+
let newCoords = splitCoords(coords);
481482
newCoords.pop();
482483
newCoords.push(newKey);
483-
newCoords = newCoords.join('-');
484+
newCoords = joinCoords.apply(null, newCoords);
484485

485486

486487
onEdit(value, newCoords, coords);

src/util.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import {JOIN_SYMBOL, FIELD_NAME_PREFIX} from './constants';
23

34

45
export const EditorContext = React.createContext();
@@ -62,15 +63,27 @@ export function getCsrfCookie() {
6263
}
6364

6465

66+
export function joinCoords() {
67+
/* Generates coordinates from given arguments */
68+
return Array.from(arguments).join(JOIN_SYMBOL);
69+
}
70+
71+
72+
export function splitCoords(coords) {
73+
/* Generates coordinates */
74+
return coords.split(JOIN_SYMBOL);
75+
}
76+
77+
6578
export function getCoordsFromName(name) {
6679
/* Returns coordinates of a field in the data from
6780
* the given name of the input.
68-
* Field names have rjf- prefix but the coordinates don't.
81+
* Field names have FIELD_NAME_PREFIX prepended but the coordinates don't.
6982
* e.g.:
70-
* name: rjf-0-field
83+
* name: rjf-0-field (where rjf- is the FIELD_NAME_PREFIX)
7184
* coords: 0-field
7285
*/
73-
return name.slice('4');
86+
return name.slice((FIELD_NAME_PREFIX + JOIN_SYMBOL).length);
7487
}
7588

7689

0 commit comments

Comments
 (0)