Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: focusOptions changes, replaced focusName - with field and index, added focusOptions to all array methods #107

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@
</div>
<button
type="button"
@click="
prepend({ firstName: 'prepend' }, { focusName: 'arrayField.0.firstName' })
"
@click="prepend({ firstName: 'prepend' }, { name: 'firstName' })"
>
Prepend
</button>
Expand Down
55 changes: 38 additions & 17 deletions src/components/ValidationFieldArray.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,51 +88,72 @@ function touch() {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function append(value: Record<string, any>, options?: FocusOptions) {
function append(value: Record<string, any>, focusOptions?: FocusOptions) {
value[keyName.value] = getId(value);
fields.value.push(value);
touch();
handleFocus(options);
if (focusOptions) {
// by default focus on last field
handleFocus({ index: fields.value.length - 1, ...focusOptions });
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function prepend(value: Record<string, any>, options?: FocusOptions) {
function prepend(value: Record<string, any>, focusOptions?: FocusOptions) {
value[keyName.value] = getId(value);
fields.value.unshift(value);
touch();
handleFocus(options);
if (focusOptions) {
// by default focus on first field
handleFocus(focusOptions);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function insert(index: number, value: Record<string, any>, options?: FocusOptions) {
function insert(index: number, value: Record<string, any>, focusOptions?: FocusOptions) {
value[keyName.value] = getId(value);
fields.value.splice(index, 0, value);
touch();
handleFocus(options);
if (focusOptions) {
// by default focus on inserted field
handleFocus({ index, ...focusOptions });
}
}
function swap(from: number, to: number) {
function swap(from: number, to: number, focusOptions?: FocusOptions) {
const temp = fields.value[from];
fields.value[from] = fields.value[to];
fields.value[to] = temp;
touch();
if (focusOptions) {
// by default focus on swapped field
handleFocus({ index: to, ...focusOptions });
}
}
function move(from: number, to: number) {
function move(from: number, to: number, focusOptions?: FocusOptions) {
fields.value.splice(to, 0, fields.value.splice(from, 1)[0]);
touch();
if (focusOptions) {
// by default focus on moved field
handleFocus({ index: to, ...focusOptions });
}
}
function remove(index: number) {
function remove(index: number, focusOptions?: FocusOptions) {
fields.value.splice(index, 1);
touch();

if (focusOptions && fields.value.length) {
// by default focus on previous field, if there is no previous field focus on first field
handleFocus({ index: Math.max(index - 1, 0), ...focusOptions });
}
}

function handleFocus(options?: FocusOptions) {
if (!options) {
return;
function handleFocus({ field, index = 0 }: FocusOptions) {
if (!field) {
throw new Error(`Field name is required for focus, please provide field name in focus options`);
}

const itemName = `${name.value}.${index || 0}.${field}`;
nextTick(() => {
const fieldComponent = fieldComponents.value.find(({ name }) => name === options.focusName);
if (!fieldComponent) {
return;
}
fieldComponent.onFocus();
const fieldComponent = fieldComponents.value.find(({ name }) => name === itemName);
fieldComponent?.onFocus();
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/types/field-array.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface FocusOptions {
focusName: string;
field: string;
index?: number;
}
17 changes: 15 additions & 2 deletions tests/unit/ValidationFieldArray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe('ValidationFieldArray', () => {
});
};

const focusTest = (name) => {
const emits = wrapper.emitted().focus;
expect(emits[emits.length - 1]).toContainEqual({ name });
};

it('should render array fields', async () => {
createComponent({
props: {
Expand Down Expand Up @@ -146,7 +151,8 @@ describe('ValidationFieldArray', () => {
await nextTick();
expect(wrapper.findAllComponents(BaseInput).length).toBe(3);

await wrapper.find('#append').trigger('click', { firstName: 'new name' });
await wrapper.find('#append').trigger('click');
focusTest('arrayField.1.firstName');

const baseInputWrappers = wrapper.findAllComponents(BaseInput);
expect(baseInputWrappers.length).toBe(4);
Expand Down Expand Up @@ -203,6 +209,8 @@ describe('ValidationFieldArray', () => {

await wrapper.find('#remove').trigger('click');

focusTest('arrayField.0.firstName');

expect(wrapper.findComponent(FormInfo).props().errors).toEqual({
'my.nested.value': [],
'my-input': [],
Expand Down Expand Up @@ -271,7 +279,8 @@ describe('ValidationFieldArray', () => {
]
});

await wrapper.find('#prepend').trigger('click', { firstName: 'new name' });
await wrapper.find('#prepend').trigger('click');
focusTest('arrayField.0.firstName');

expect(wrapper.findComponent(FormInfo).props().errors).toEqual({
'my.nested.value': [],
Expand Down Expand Up @@ -354,6 +363,7 @@ describe('ValidationFieldArray', () => {
]
});
expect(wrapper.findComponent(FormInfo).props().values.arrayField[1].type).toEqual(undefined);
focusTest('arrayField.1.firstName');
expect(wrapper.findAllComponents(BaseInput).at(3).props().modelValue).toBe('insert');
});

Expand Down Expand Up @@ -407,6 +417,7 @@ describe('ValidationFieldArray', () => {
const formInfoWrapper = wrapper.findComponent(FormInfo);

await wrapper.find('#swap').trigger('click');
focusTest('arrayField.2.firstName');

expect(formInfoWrapper.props().errors).toEqual({
'my.nested.value': [],
Expand Down Expand Up @@ -540,6 +551,8 @@ describe('ValidationFieldArray', () => {

await wrapper.find('#move').trigger('click');

focusTest('arrayField.2.firstName');

expect(formInfoWrapper.props().errors).toEqual({
'my.nested.value': [],
'my-input': [],
Expand Down
20 changes: 12 additions & 8 deletions tests/unit/ValidationForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
>
<ValidationField :name="`${arrayName}.${index}.id`" />
<ValidationField :name="`${arrayName}.${index}.type`" />
<ValidationField :name="`${arrayName}.${index}.firstName`">
<ValidationField
:name="`${arrayName}.${index}.firstName`"
@should-focus="$emit('focus', $event)"
>
<template
#default="{
modelValue,
Expand Down Expand Up @@ -132,42 +135,42 @@
<button
id="append"
type="button"
@click="append"
@click="append({ firstName: 'new name' }, { field: 'firstName' })"
>
Append
</button>
<button
id="prepend"
type="button"
@click="prepend"
@click="prepend({ firstName: 'new name' }, { field: 'firstName' })"
>
Prepend
</button>
<button
id="insert"
type="button"
@click="insert(1, { firstName: 'insert' })"
@click="insert(1, { firstName: 'insert' }, { field: 'firstName' })"
>
Insert
</button>
<button
id="swap"
type="button"
@click="swap(0, 2)"
@click="swap(0, 2, { field: 'firstName' })"
>
Swap
</button>
<button
id="move"
type="button"
@click="move(0, 2)"
@click="move(0, 2, { field: 'firstName' })"
>
Move
</button>
<button
id="remove"
type="button"
@click="remove(1)"
@click="remove(1, { field: 'firstName' })"
>
Remove
</button>
Expand Down Expand Up @@ -267,7 +270,8 @@ export default {
emits: {
submit: null,
dirty: null,
change: null
change: null,
focus: null
},
methods: {
onSubmit(values, options) {
Expand Down
Loading