Skip to content

Commit

Permalink
feat: remove debounce feature and make it userland
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jul 20, 2020
1 parent 0b1d2bf commit b7263ce
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 59 deletions.
19 changes: 5 additions & 14 deletions packages/core/src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { computed, inject, h, defineComponent } from 'vue';
import { getConfig } from './config';
import { FormController } from './types';
import { useField } from './useField';
import { useRefsObjToComputed, debounce, normalizeChildren, isHTMLTag } from './utils';
import { useRefsObjToComputed, normalizeChildren, isHTMLTag } from './utils';

export const Field = defineComponent({
name: 'Field',
Expand Down Expand Up @@ -31,14 +31,6 @@ export const Field = defineComponent({
type: Boolean,
default: false,
},
debounce: {
type: Number,
default: 0,
},
customMessages: {
type: Object,
default: undefined,
},
},
setup(props, ctx) {
const fieldName = props.name;
Expand All @@ -58,17 +50,16 @@ export const Field = defineComponent({
}
);

const limitedHandleChange = debounce(handleChange, props.debounce);
const unwrappedMeta = useRefsObjToComputed(meta);

const slotProps = computed(() => {
return {
field: {
name: fieldName,
disabled: props.disabled,
onInput: limitedHandleChange,
onChange: limitedHandleChange,
'onUpdate:modelValue': limitedHandleChange,
onInput: handleChange,
onChange: handleChange,
'onUpdate:modelValue': handleChange,
onBlur: onBlur,
value: value.value,
},
Expand All @@ -78,7 +69,7 @@ export const Field = defineComponent({
errorMessage: errorMessage.value,
validate: validateField,
reset,
handleChange: limitedHandleChange,
handleChange,
};
});

Expand Down
20 changes: 0 additions & 20 deletions packages/core/src/utils/functions.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './assertions';
export * from './consts';
export * from './events';
export * from './functions';
export * from './refs';
export * from './rules';
export * from './vnode';
25 changes: 1 addition & 24 deletions packages/core/tests/Field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,6 @@ describe('<Field />', () => {
expect(errors[1].textContent).toBe('This field must be at least 3 characters');
});

test('validation can be debounced', async () => {
const wrapper = mountWithHoc({
template: `
<div>
<Field name="field" rules="required" :debounce="50" v-slot="{ field, errors }">
<input v-bind="field" type="text">
<p>{{ errors[0] }}</p>
</Field>
</div>
`,
});

const input = wrapper.$el.querySelector('input');
const error = wrapper.$el.querySelector('p');

setValue(input, '');
await flushPromises();
expect(error.textContent).toBe('');
await jest.advanceTimersByTime(50);
await flushPromises();
expect(error.textContent).toBe(REQUIRED_MESSAGE);
});

test('yup rules can be used', async () => {
const wrapper = mountWithHoc({
setup() {
Expand Down Expand Up @@ -402,7 +379,7 @@ describe('<Field />', () => {
const wrapper = mountWithHoc({
template: `
<div>
<Field name="field" rules="required|longRunning" :debounce="10" v-slot="{ field, errors }">
<Field name="field" rules="required|longRunning" v-slot="{ field, errors }">
<input v-bind="field" type="text">
<p>{{ errors[0] }}</p>
</Field>
Expand Down

0 comments on commit b7263ce

Please sign in to comment.