From 96a991f46f891b49f2f1367be986c6c4e5ae0138 Mon Sep 17 00:00:00 2001 From: 1on Date: Mon, 2 Nov 2020 11:59:26 +0300 Subject: [PATCH] fix: Fixed registry.getNested for array of fields --- src/form/registry.ts | 9 ++++++++- test/unit/FormularioForm.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/form/registry.ts b/src/form/registry.ts index 1cddf71..8319922 100644 --- a/src/form/registry.ts +++ b/src/form/registry.ts @@ -72,7 +72,14 @@ export default class Registry { const result = new Map() for (const i of this.registry.keys()) { - if (i === key || i.includes(key + '.')) { + const objectKey = key + '.' + const arrayKey = key + '[' + + if ( + i === key || + i.substring(0, objectKey.length) === objectKey || + i.substring(0, arrayKey.length) === arrayKey + ) { result.set(i, this.registry.get(i)) } } diff --git a/test/unit/FormularioForm.test.js b/test/unit/FormularioForm.test.js index 78d5cc5..e644f02 100644 --- a/test/unit/FormularioForm.test.js +++ b/test/unit/FormularioForm.test.js @@ -68,6 +68,33 @@ describe('FormularioForm', () => { expect(wrapper.findComponent(FormularioForm).vm.registry.keys()).toEqual(['sub2']) }) + it('Getting nested fields from registry', async () => { + const wrapper = mount({ + data: () => ({ active: true, nested: { groups: { value: 'value' } }, groups: [{ name: 'group1' }, { name: 'group2' }] }), + template: ` + + + + + + + + + + + ` + }) + await flushPromises() + expect(Array.from(wrapper.findComponent(FormularioForm).vm.registry.getNested('sub1').keys())).toEqual(['sub1']) + expect(Array.from(wrapper.findComponent(FormularioForm).vm.registry.getNested('groups').keys())) + .toEqual(['groups', 'groups[0].name', 'groups[1].name']) + + wrapper.setData({ active: true, groups: [{ name: 'group1' }] }) + await flushPromises() + expect(Array.from(wrapper.findComponent(FormularioForm).vm.registry.getNested('groups').keys())) + .toEqual(['groups', 'groups[0].name']) + }) + it('Can set a field’s initial value', async () => { const wrapper = mount(FormularioForm, { propsData: { formularioValue: { test: 'Has initial value' } },