Skip to content

Commit 50ac85b

Browse files
harish2704jarvelov
authored andcommitted
Fix unique field id generation algorithm.
Unique id of every element is derived from 1. parentid 2. position index of current element. This commmit introduce an optional property "id" to global component. Default value is randomly generated using Math.random. Fixes #61
1 parent 11cbb3b commit 50ac85b

File tree

3 files changed

+15
-36
lines changed

3 files changed

+15
-36
lines changed

src/vfjs-global-mixin/methods/vfjs-helpers/index.js

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,6 @@ const vfjsHelpers = {
8181
},
8282
}));
8383
},
84-
vfjsHelperHashString(string, binary = 62) {
85-
let integer = 0;
86-
87-
for (let i = 0; i < string.length; i++) {
88-
const char = string.charCodeAt(i);
89-
integer = (integer * 33) ^ char; // eslint-disable-line no-bitwise
90-
}
91-
92-
// Convert int to unsigned to get a positive number
93-
integer >>>= 0; // eslint-disable-line no-bitwise
94-
95-
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
96-
const array = [];
97-
98-
// Create an alphanumeric hash of unsigned int
99-
while (integer >= binary) {
100-
const char = integer % binary;
101-
array.push(chars[char]);
102-
integer = Math.floor(integer / binary);
103-
}
104-
105-
return array.join('');
106-
},
10784
vfjsHelperCreateComponent({ children = [], component, props }) {
10885
// If the component matches one of the local components
10986
// passed in with the `components` prop
@@ -140,25 +117,23 @@ const vfjsHelpers = {
140117
set(newVfjsModel, key, value);
141118
return newVfjsModel;
142119
},
143-
// The level param helps us to differentiate further between fields.
144-
// As the same field configuration may be present throughout the ui schema
145-
// and thus have the same hash.
146-
//
147-
// We mediate this by providing the depth level as a second param
148-
// which will make the hash unique for every field
149-
vfjsHelperGenerateField(field, level = 0) {
120+
/*
121+
* Unique id for every element is generated by simply appending the position index with parentid.
122+
* The parentId is the unique id of parent component ( it can be either a form or a field )
123+
* The id of top most form component is auto generated using Math.random.
124+
* Since id is a function of parentId and element index, we will get consistent , but unique id for each component
125+
*/
126+
vfjsHelperGenerateField(field, parentId) {
150127
if (!field) {
151128
return false;
152129
}
153130

154-
const { children = [], ...fieldWithoutChildren } = field;
155-
const objString = JSON.stringify({ fieldWithoutChildren, level });
156-
const id = this.vfjsHelperHashString(objString);
131+
const { children = [] } = field;
157132

158133
return {
159134
...field,
160-
id,
161-
children: children.map((child, i) => this.vfjsHelperGenerateField(child, (i + 1) * (level + 1))),
135+
id: parentId,
136+
children: children.map((child, i) => this.vfjsHelperGenerateField(child, `${parentId}-${i}`)),
162137
};
163138
},
164139
vfjsHelperChildArrayMapper({ model, children = [], ...child }, parentModel, index) {

src/vfjs-global-mixin/methods/vfjs-ui/setters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { VFJS_EVENT_STATE_UPDATED } from '../../../constants';
44
const vfjsUiSetters = {
55
setVfjsUiSchema(uiSchema = []) {
66
const newVfjsUiSchema = uiSchema.reduce(
7-
(fields, field, index) => [...fields, this.vfjsHelperGenerateField(field, index)],
7+
(fields, field, index) => [...fields, this.vfjsHelperGenerateField(field, `${this.id}-${index}`)],
88
[],
99
);
1010

src/vfjs-global-mixin/props.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const props = {
1616
type: Object,
1717
default: () => ({}),
1818
},
19+
id: {
20+
type: String,
21+
default: () => Math.random().toString(36).substr(2, 9),
22+
},
1923
options: {
2024
type: Object,
2125
default: () => ({}),

0 commit comments

Comments
 (0)