Skip to content

Commit

Permalink
fix: on-enter key submit validation [WTEL-4384]
Browse files Browse the repository at this point in the history
  • Loading branch information
dlohvinov committed Apr 17, 2024
1 parent 543a20d commit f75ad7d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
12 changes: 7 additions & 5 deletions src/components/auth/login/steps/the-login-first-step.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
v-model.trim="domain"
:label="$t('auth.domain')"
:v="v$.domain"
@keyup.enter="emit('next')"
/>

<div class="auth-form-actions">
Expand All @@ -27,6 +26,7 @@ import { required } from '@vuelidate/validators';
import { computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import domainValidator from '@webitel/ui-sdk/src/validators/domainValidator';
import { useNextOnEnter } from '../../../../composables/useNextOnEnter.js';
const props = defineProps({
isSubmitting: {
Expand Down Expand Up @@ -55,11 +55,13 @@ const v$ = useVuelidate(
{ $autoDirty: true },
);
async function setProp(payload) {
return store.dispatch('auth/SET_PROPERTY', payload);
}
onMounted(() => {
v$.value.$touch();
});
useNextOnEnter(() => !v$.value.$invalid && emit('next'));
async function setProp(payload) {
return store.dispatch('auth/SET_PROPERTY', payload);
}
</script>
5 changes: 3 additions & 2 deletions src/components/auth/login/steps/the-login-second-step.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
v-model.trim="username"
:label="$t('vocabulary.login')"
:v="v$.username"
@keyup.enter="emit('next')"
/>

<wt-input
Expand All @@ -21,7 +20,6 @@
:v="v$.password"
type="password"
has-show-password
@keyup.enter="emit('next')"
/>

<wt-checkbox
Expand Down Expand Up @@ -56,6 +54,7 @@ import { required } from '@vuelidate/validators';
import { computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import LoginProviders from '../providers/the-login-providers.vue';
import { useNextOnEnter } from '../../../../composables/useNextOnEnter.js';
const emit = defineEmits(['back', 'next']);
Expand Down Expand Up @@ -90,6 +89,8 @@ const v$ = useVuelidate(
onMounted(() => { v$.value.$touch() });
useNextOnEnter(() => !v$.value.$invalid && emit('next'));
async function setProp(payload) {
return store.dispatch('auth/SET_PROPERTY', payload);
}
Expand Down
14 changes: 0 additions & 14 deletions src/components/auth/login/the-login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,9 @@ export default {
this.$emit('submit');
}
},
handleGlobalKeypress(event) {
if (event.key === 'Enter') {
if (event.ctrlKey) {
this.$emit('submit');
}
this.goNextStep();
}
},
},
mounted() {
window.addEventListener('keypress', this.handleGlobalKeypress);
},
unmounted() {
window.removeEventListener('keypress', this.handleGlobalKeypress);
this.resetState();
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
v-model.trim="domain"
:label="$t('auth.domain')"
:v="v$.domain"
@keyup.enter="emit('next')"
/>

<div class="auth-form-actions">
Expand All @@ -30,6 +29,7 @@ import { required } from '@vuelidate/validators';
import { computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import domainValidator from '@webitel/ui-sdk/src/validators/domainValidator';
import { useNextOnEnter } from '../../../../composables/useNextOnEnter.js';
const props = defineProps({
isSubmitting: {
Expand Down Expand Up @@ -58,6 +58,8 @@ const v$ = useVuelidate(
{ $autoDirty: true },
);
useNextOnEnter(() => !v$.value.$invalid && emit('next'));
async function setProp(payload) {
return store.dispatch('auth/SET_PROPERTY', payload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
v-model.trim="username"
:label="$t('vocabulary.login')"
:v="v$.username"
@keyup.enter="emit('next')"
/>

<wt-input
Expand Down Expand Up @@ -53,6 +52,7 @@ import { useVuelidate } from '@vuelidate/core';
import { required, sameAs } from '@vuelidate/validators';
import { computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import { useNextOnEnter } from '../../../../composables/useNextOnEnter.js';
const emit = defineEmits(['back', 'next']);
Expand Down Expand Up @@ -91,6 +91,8 @@ const v$ = useVuelidate(
{ $autoDirty: true },
);
useNextOnEnter(() => !v$.value.$invalid && emit('next'));
async function setProp(payload) {
return store.dispatch('auth/SET_PROPERTY', payload);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
:label="$t('auth.key')"
:v="v$.certificate"
@input="setProp({ prop: 'certificate', value: $event })"
@keyup.enter="emit('next')"
/>

<div class="auth-form-actions">
Expand All @@ -29,6 +28,7 @@ import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';
import { computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import { useNextOnEnter } from '../../../../composables/useNextOnEnter.js';
const emit = defineEmits(['back', 'next']);
Expand All @@ -46,9 +46,11 @@ const v$ = useVuelidate(
{ $autoDirty: true },
);
useNextOnEnter(() => !v$.value.$invalid && emit('next'));
async function setProp(payload) {
return store.dispatch('auth/SET_PROPERTY', payload);
};
}
onMounted(() => { v$.value.$touch() });
</script>
11 changes: 0 additions & 11 deletions src/components/auth/register/the-register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,9 @@ export default {
this.$emit('submit');
}
},
handleGlobalKeypress(event) {
if (event.key === 'Enter') {
this.goNextStep();
}
},
},
mounted() {
window.addEventListener('keypress', this.handleGlobalKeypress);
},
unmounted() {
window.removeEventListener('keypress', this.handleGlobalKeypress);
this.resetState();
},
};
Expand Down
17 changes: 17 additions & 0 deletions src/composables/useNextOnEnter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { onMounted, onUnmounted } from 'vue';

export const useNextOnEnter = (callback) => {
const handleKeyDown = (e) => {
if (e.key === 'Enter') {
callback();
}
};

onMounted(() => {
window.addEventListener('keydown', handleKeyDown);
});

onUnmounted(() => {
window.removeEventListener('keydown', handleKeyDown);
});
};

0 comments on commit f75ad7d

Please sign in to comment.