Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Fix: don't clear decimals when switching French -> English (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelidlessness authored Dec 22, 2022
1 parent 20d76ac commit c681c24
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/widget/number-input/number-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Widget from '../../js/widget';
* @abstract
* @extends {Widget<HTMLInputElement>}
*/
export default class NumberInput extends Widget {
class NumberInput extends Widget {
/**
* @abstract
*/
Expand Down Expand Up @@ -123,13 +123,22 @@ export default class NumberInput extends Widget {
this.question = question;
this.message = message;

this.reformatValue();
this.setReformattedValue(input.valueAsNumber);
this.setValidity();

const languageChanged = () => {
// Important: this value may become invalid if it isn't accessed
// before setting `lang`. This repros in Firefox if:
//
// 1. Your default language is English
// 2. Set a decimal value
// 3. Switch to French
// 4. Switch back to English
const { valueAsNumber } = input;

characterPattern = this.characterPattern;
question.setAttribute('lang', this.language);
this.reformatValue();
this.setReformattedValue(valueAsNumber);
this.setValidity();
};

Expand Down Expand Up @@ -160,16 +169,20 @@ export default class NumberInput extends Widget {
});
}

reformatValue() {
/**
* @param {number} value
*/
setReformattedValue(value) {
const { element, pattern } = this;
const { valueAsNumber } = element;

element.pattern = pattern.source;
element.removeAttribute('pattern');
element.value = '';

if (!Number.isNaN(valueAsNumber)) {
element.value = valueAsNumber;
if (!Number.isNaN(value)) {
element.value = value;
}

element.setAttribute('pattern', pattern);
}

setValidity() {
Expand All @@ -182,3 +195,5 @@ export default class NumberInput extends Widget {
this.isValid = isValid;
}
}

export default NumberInput;

0 comments on commit c681c24

Please sign in to comment.