diff --git a/src/classes/Form.ts b/src/classes/Form.ts index 64ffd01..8b380fb 100644 --- a/src/classes/Form.ts +++ b/src/classes/Form.ts @@ -323,9 +323,14 @@ export default class Form extends EventEmitter implements FormDependence { .forEach(item => { // Ранее было установлено простое поле - const isAbolish = abolishNames.find(name => (new RegExp(`^${name}\..*`)).test(item.name)); - - if (isAbolish) return; + // const isAbolish = abolishNames.find(name => (new RegExp(`^${name}\..*`)).test(item.name)); + const isAbolish = abolishNames.find(name => isPrefixName(item.name, name)); + + if (isAbolish) { + debug.msg(`Field is %cskipped%c %c${item.name}%c because its child field was previously set.`, + debug.colorError, debug.colorDefault, debug.colorName, debug.colorDefault); + return; + } // Если текущее значение - примитивное, а предыдущее нет - необходимо пометить данное поле как конечное, то // есть все дальнейшие(внутренние поля) - является упразднёнными и их не нужно проецировать на форму. diff --git a/src/utils/is-prefix-name.ts b/src/utils/is-prefix-name.ts index 2e9c1ef..15ea7e8 100644 --- a/src/utils/is-prefix-name.ts +++ b/src/utils/is-prefix-name.ts @@ -1,9 +1,22 @@ /** - * @description Return true if the prefix is some parent of provided field name. - * @example address.city.name address -> true - * @example user.type.index user.type -> true - * @example position.city.type city -> false - * @example name name -> false + * @description Return true if the prefix is some parent of provided field name. In other words, it checks that the + * second parameter is the parent of the first. + * + * @example + * // returns true + * isPrefixName("address.city.name", "address"); + * @example + * // return true + * isPrefixName("user.type.index", "user.type") + * @example + * // returns false + * isPrefixName("position.city.type", "city"); + * @example + * // return false + * isPrefixName("name", "name"); + * + * @param fieldName Check Field + * @param prefix Parent Field or Prefix value * */ export default function isPrefixName(fieldName: string, prefix: string) { return (new RegExp(`^${prefix}\\.`)).test(fieldName); diff --git a/tests/units/form/form-set-values.spec.ts b/tests/units/form/form-set-values.spec.ts index 9ccbe84..5cd1366 100644 --- a/tests/units/form/form-set-values.spec.ts +++ b/tests/units/form/form-set-values.spec.ts @@ -515,5 +515,27 @@ describe("Form.setValues", () => { expect(form.changes).toEqual({}) expect(form.values).toEqual({ username: "Jack" }) }) + /** + * При установке поля CoolName, поле Cool не должно как-то влиять на поле CoolName. + */ + test("Short name should not has affect for field, that don't include first name like prefix", () => { + const form = new Form(); + form.setValues({ + Id: 13, + IdentityDocument: { + Name: "Test", + Type: "P" + } + }) + + expect(form.values).toEqual({ + Id: 13, + IdentityDocument: { + Name: "Test", + Type: "P" + } + }) + + }) }) \ No newline at end of file