From e78170a3ed5a41a7ca2379ee531fe72c00cd9ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Beauvais?= Date: Fri, 13 Sep 2024 14:50:41 -0700 Subject: [PATCH] 16700 Fixed validation in LimitedRestorationPanel component (#258) * - fixed input handling and validation * - updated unit tests * - don't call validate before component is rendered (fixes unit test warnings) --------- Co-authored-by: Severin Beauvais --- .../LimitedRestorationPanel.vue | 47 +++++++------------ tests/unit/LimitedRestorationPanel.spec.ts | 35 +++++++------- 2 files changed, 35 insertions(+), 47 deletions(-) diff --git a/src/components/limited-restoration-panel/LimitedRestorationPanel.vue b/src/components/limited-restoration-panel/LimitedRestorationPanel.vue index bf568efa..c83bb680 100644 --- a/src/components/limited-restoration-panel/LimitedRestorationPanel.vue +++ b/src/components/limited-restoration-panel/LimitedRestorationPanel.vue @@ -45,7 +45,6 @@ hide-spin-buttons :rules="monthsRules" :disabled="(radioValue !== 'customMonths')" - @input="onMonthsInput($event)" /> month(s) @@ -84,9 +83,9 @@ export default class LimitedRestorationPanel extends Vue { /** * Called when the component is mounted. - * Sets the initial radio button and months input (if applicable). */ mounted (): void { + // set the initial radio button and months input (if applicable) if ([24, 18, 12, 6].includes(this.months)) { this.radioValue = this.months.toString() } else { @@ -96,49 +95,38 @@ export default class LimitedRestorationPanel extends Vue { } /** - * Called when months input has changed. + * Called when radio value or input value have changed. */ - async onMonthsInput (input: string): Promise { - // ignore non-inputs - if (input !== null) { - // wait for component updates + @Watch('radioValue') + @Watch('inputValue') + private async onValueChanged (): Promise { + if (this.radioValue === 'customMonths') { + // wait for component updates then validate the input field await this.$nextTick() - // validate the input - const valid = this.$refs.monthsRef.validate() - // emit validity + const valid = this.$refs.monthsRef?.validate() + + // emit months and validity + this.emitMonths(valid ? +this.inputValue : null) // emit null if invalid this.emitValid(valid) - // if valid, emit new value - if (valid) this.emitMonths(Number(this.inputValue)) - } - } + } else { + // reset input field validation + this.$refs.monthsRef.resetValidation() - /** - * Called when radio button has changed. - */ - @Watch('radioValue') - private onRadioValueChanged () { - if (this.radioValue !== 'customMonths') { - // reset months text field when another radio button is selected - this.$refs.monthsRef.reset() // emit months and validity - this.emitMonths(Number(this.radioValue)) + this.emitMonths(+this.radioValue) this.emitValid(true) - } else if (!this.inputValue) { - // as there is no input value, this component is invalid - // otherwise, validation will be done in onMonthsInput() - this.emitValid(false) } } /** - * Emits the numbed of months selected. + * Emits the number of months selected. */ @Emit('months') // eslint-disable-next-line @typescript-eslint/no-unused-vars private emitMonths (months: number): void {} /** - * Emits whether the number of months selected is valid. + * Emits the component validity. */ @Emit('valid') // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -148,6 +136,7 @@ export default class LimitedRestorationPanel extends Vue {