1
0
mirror of synced 2024-11-29 08:36:12 +03:00

Merge pull request #13 from 1on/master

Fixed working with array of fields
This commit is contained in:
Kruglov Kirill 2020-11-02 12:03:39 +03:00 committed by GitHub
commit 5585b20d84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 167 additions and 55 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ coverage
*.sublime-workspace *.sublime-workspace
*.dev.tale.vue *.dev.tale.vue
*.dev.stories.js *.dev.stories.js
storybook-static

View File

@ -72,7 +72,14 @@ export default class Registry {
const result = new Map() const result = new Map()
for (const i of this.registry.keys()) { 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)) result.set(i, this.registry.get(i))
} }
} }

View File

@ -52,6 +52,9 @@ export function setNested (obj: Record<string, any>, field: string, value: any):
subProxy = subProxy[matches[2]] subProxy = subProxy[matches[2]]
} }
} else { } else {
if (subProxy === undefined) {
break
}
if (i === fieldParts.length - 1) { if (i === fieldParts.length - 1) {
subProxy[fieldPart] = value subProxy[fieldPart] = value
break break

View File

@ -1,20 +1,13 @@
<template> <template>
<FormularioForm v-model="values"> <FormularioForm v-model="values">
<FormularioGrouping name="group">
<FormularioInput <FormularioInput
v-slot="{ context }" v-slot="{ context }"
class="mb-3" class="mb-3"
name="text" name="groups"
validation="number|required" validation="min:1"
> validation-behavior="live"
<label for="text-field">Text field (number|required)</label>
<input
id="text-field"
v-model="context.model"
type="text"
class="form-control"
style="max-width: 250px;"
> >
<FormularioGroupingGroupTale v-model="context.model" />
<div <div
v-for="(error, index) in context.allErrors" v-for="(error, index) in context.allErrors"
@ -25,52 +18,24 @@
</div> </div>
</FormularioInput> </FormularioInput>
<FormularioInput
v-slot="{ context }"
:validation-messages="{ in: 'The value was different than expected' }"
class="mb-3"
name="abcdef-field"
validation="in:abcdef"
>
<label for="abcdef-field">Text field (in:abcdef)</label>
<input
id="abcdef-field"
v-model="context.model"
type="text"
class="form-control"
style="max-width: 250px;"
>
<div
v-for="(error, index) in context.allErrors"
:key="index"
class="text-danger"
>
{{ error }}
</div>
</FormularioInput>
</FormularioGrouping>
<div>{{ values }}</div> <div>{{ values }}</div>
</FormularioForm> </FormularioForm>
</template> </template>
<script> <script>
import FormularioForm from '@/FormularioForm' import FormularioForm from '@/FormularioForm'
import FormularioGrouping from '@/FormularioGrouping' import FormularioGroupingGroupTale from './FormularioGroupingGroup.tale.vue'
import FormularioInput from '@/FormularioInput'
export default { export default {
name: 'FormularioGroupingTale', name: 'FormularioGroupingTale',
components: { components: {
FormularioForm, FormularioForm,
FormularioGrouping, FormularioGroupingGroupTale,
FormularioInput,
}, },
data: () => ({ data: () => ({
values: {}, values: { groups: [] },
}), }),
} }
</script> </script>

View File

@ -0,0 +1,106 @@
<template>
<div>
<div
v-for="(item, groupIndex) in groups"
:key="groupIndex"
>
<FormularioGrouping :name="`groups[${groupIndex}]`">
<FormularioInput
v-slot="{ context }"
class="mb-3"
name="text"
validation="number|required"
validation-behavior="live"
>
<label for="text-field">Text field (number|required)</label>
<input
id="text-field"
v-model="context.model"
type="text"
class="form-control"
style="max-width: 250px;"
>
<div
v-for="(error, index) in context.allErrors"
:key="index"
class="text-danger"
>
{{ error }}
</div>
</FormularioInput>
<FormularioInput
v-slot="{ context }"
:validation-messages="{ in: 'The value was different than expected' }"
class="mb-3"
name="abcdef-field"
validation="in:abcdef"
validation-behavior="live"
>
<label for="abcdef-field">Text field (in:abcdef)</label>
<input
id="abcdef-field"
v-model="context.model"
type="text"
class="form-control"
style="max-width: 250px;"
>
<div
v-for="(error, index) in context.allErrors"
:key="index"
class="text-danger"
>
{{ error }}
</div>
</FormularioInput>
</FormularioGrouping>
<button @click="onRemoveGroup(groupIndex)">
Remove Group
</button>
</div>
<button @click="onAddGroup">
Add Group
</button>
</div>
</template>
<script>
import FormularioGrouping from '@/FormularioGrouping'
import FormularioInput from '@/FormularioInput'
export default {
name: 'FormularioGroupingGroupTale',
components: {
FormularioGrouping,
FormularioInput,
},
model: {
prop: 'groups',
event: 'change'
},
props: {
groups: {
type: Array,
required: true,
},
},
methods: {
onAddGroup () {
this.$emit('change', this.groups.concat([{}]))
},
onRemoveGroup (removedIndex) {
this.$emit('change', this.groups.filter((item, index) => {
return index !== removedIndex
}))
}
}
}
</script>

View File

@ -12,6 +12,9 @@ Vue.mixin({
methods: { methods: {
$t (text) { $t (text) {
return text return text
},
$tc (text) {
return text
} }
} }
}) })

View File

@ -68,6 +68,33 @@ describe('FormularioForm', () => {
expect(wrapper.findComponent(FormularioForm).vm.registry.keys()).toEqual(['sub2']) 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: `
<FormularioForm>
<FormularioInput name="sub1" />
<FormularioInput name="sub2" />
<FormularioInput name="nested.groups.value" />
<FormularioInput name="groups">
<FormularioGrouping :name="'groups[' + index + ']'" v-for="(item, index) in groups" :key="index">
<FormularioInput name="name" />
</FormularioGrouping>
</FormularioInput>
</FormularioForm>
`
})
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 fields initial value', async () => { it('Can set a fields initial value', async () => {
const wrapper = mount(FormularioForm, { const wrapper = mount(FormularioForm, {
propsData: { formularioValue: { test: 'Has initial value' } }, propsData: { formularioValue: { test: 'Has initial value' } },