Skip to content

Commit

Permalink
feat(vue-input): add debounced-input event
Browse files Browse the repository at this point in the history
  • Loading branch information
devCrossNet committed Feb 11, 2022
1 parent 7338bcd commit 6e204a3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
21 changes: 21 additions & 0 deletions src/components/input-and-actions/VueInput/VueInput.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render, fireEvent } from '@testing-library/vue';
import flushPromises from 'flush-promises';
import { sleep } from '@/test/test-utils';
import VueInput from './VueInput.vue';

describe('VueInput.vue', () => {
Expand Down Expand Up @@ -66,6 +67,26 @@ describe('VueInput.vue', () => {
expect(actual).toBe(expected);
});

test('should emit debounced-input', async () => {
const { getByDisplayValue, emitted } = render(VueInput, {
propsData: {
label: 'this is the label',
value: 'this is the value',
name: 'name',
id: 'id',
debounce: 0,
},
});

await fireEvent.update(getByDisplayValue('this is the value'), 'this is the new value');
await sleep(10);

const actual = emitted()['debounced-input'][0][0];
const expected = 'this is the new value';

expect(actual).toBe(expected);
});

test('should display error state', async () => {
const { getByText, getByDisplayValue } = render(VueInput, {
propsData: {
Expand Down
21 changes: 16 additions & 5 deletions src/components/input-and-actions/VueInput/VueInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
v-bind="$attrs"
v-on="{
...$listeners,
input: (e) => {
$emit('input', e.target.value, e);
},
input: onInput,
}"
/>

Expand All @@ -76,12 +74,13 @@
</template>

<script lang="ts">
import debounce from 'lodash/debounce';
import { ValidationProvider } from 'vee-validate';
import { defineComponent } from '@vue/composition-api';
import { useIntersectionObserver } from '@/composables/use-intersection-observer';
import { getDomRef } from '@/composables/get-dom-ref';
import VueText from '@/components/typography/VueText/VueText.vue';
import { shirtSizeValidator } from '@/components/prop-validators';
import VueText from '@/components/typography/VueText/VueText.vue';
export default defineComponent({
name: 'VueInput',
Expand All @@ -108,9 +107,20 @@ export default defineComponent({
trailingIcon: { type: String, default: null },
size: { type: String, validator: shirtSizeValidator, default: 'md' },
sizeAttribute: { type: Number, default: null },
debounce: { type: Number, default: null },
},
setup(props) {
setup(props, { emit }) {
const input = getDomRef(null);
const debouncedInput = debounce((value: string) => emit('debounced-input', value), props.debounce || 0);
const onInput = (e: InputEvent) => {
const value = (e.target as HTMLInputElement).value;
emit('input', value, e);
if (props.debounce !== null) {
debouncedInput(value);
}
};
useIntersectionObserver(input, (entries: IntersectionObserverEntry[]) => {
if (props.autofocus) {
Expand All @@ -120,6 +130,7 @@ export default defineComponent({
return {
input,
onInput,
};
},
});
Expand Down

0 comments on commit 6e204a3

Please sign in to comment.