Skip to content

Commit a14602e

Browse files
committed
fix: changed field ownership check when mixing
1 parent dbce50d commit a14602e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/classes/Form.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,14 @@ export default class Form extends EventEmitter implements FormDependence {
323323
.forEach(item => {
324324

325325
// Ранее было установлено простое поле
326-
const isAbolish = abolishNames.find(name => (new RegExp(`^${name}\..*`)).test(item.name));
327-
328-
if (isAbolish) return;
326+
// const isAbolish = abolishNames.find(name => (new RegExp(`^${name}\..*`)).test(item.name));
327+
const isAbolish = abolishNames.find(name => isPrefixName(item.name, name));
328+
329+
if (isAbolish) {
330+
debug.msg(`Field is %cskipped%c %c${item.name}%c because its child field was previously set.`,
331+
debug.colorError, debug.colorDefault, debug.colorName, debug.colorDefault);
332+
return;
333+
}
329334

330335
// Если текущее значение - примитивное, а предыдущее нет - необходимо пометить данное поле как конечное, то
331336
// есть все дальнейшие(внутренние поля) - является упразднёнными и их не нужно проецировать на форму.

src/utils/is-prefix-name.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
/**
2-
* @description Return true if the prefix is some parent of provided field name.
3-
* @example address.city.name address -> true
4-
* @example user.type.index user.type -> true
5-
* @example position.city.type city -> false
6-
* @example name name -> false
2+
* @description Return true if the prefix is some parent of provided field name. In other words, it checks that the
3+
* second parameter is the parent of the first.
4+
*
5+
* @example
6+
* // returns true
7+
* isPrefixName("address.city.name", "address");
8+
* @example
9+
* // return true
10+
* isPrefixName("user.type.index", "user.type")
11+
* @example
12+
* // returns false
13+
* isPrefixName("position.city.type", "city");
14+
* @example
15+
* // return false
16+
* isPrefixName("name", "name");
17+
*
18+
* @param fieldName Check Field
19+
* @param prefix Parent Field or Prefix value
720
* */
821
export default function isPrefixName(fieldName: string, prefix: string) {
922
return (new RegExp(`^${prefix}\\.`)).test(fieldName);

tests/units/form/form-set-values.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,5 +515,27 @@ describe("Form.setValues", () => {
515515
expect(form.changes).toEqual({})
516516
expect(form.values).toEqual({ username: "Jack" })
517517
})
518+
/**
519+
* При установке поля CoolName, поле Cool не должно как-то влиять на поле CoolName.
520+
*/
521+
test("Short name should not has affect for field, that don't include first name like prefix", () => {
522+
const form = new Form();
523+
form.setValues({
524+
Id: 13,
525+
IdentityDocument: {
526+
Name: "Test",
527+
Type: "P"
528+
}
529+
})
530+
531+
expect(form.values).toEqual({
532+
Id: 13,
533+
IdentityDocument: {
534+
Name: "Test",
535+
Type: "P"
536+
}
537+
})
538+
539+
})
518540

519541
})

0 commit comments

Comments
 (0)