diff --git a/packages/main/cypress/specs/StepInput.cy.tsx b/packages/main/cypress/specs/StepInput.cy.tsx index f535a9447245..d589a5a2624b 100644 --- a/packages/main/cypress/specs/StepInput.cy.tsx +++ b/packages/main/cypress/specs/StepInput.cy.tsx @@ -646,12 +646,32 @@ describe("StepInput thousand separator formatting", () => { ); cy.get("[ui5-step-input]") + .as("stepInput"); + + cy.get("@stepInput") .ui5StepInputGetInnerInput() .should($input => { const val = $input.val() as string; const num = Number(val.replace(/[^\d]/g, "")); expect(num).to.equal(12345); }); + + cy.get("@stepInput") + .realClick({ "clickCount": 2 }) + .should("be.focused"); + + cy.realType("1,0000"); + cy.realPress("Enter"); + + cy.get("@stepInput") + .ui5StepInputGetInnerInput() + .should($input => { + const val = $input.val() as string; + expect(val).to.equal("10,000"); + }); + + cy.get("@stepInput") + .should("have.prop", "value", 10000); }); it("should update input value when language is changed", () => { diff --git a/packages/main/src/StepInput.ts b/packages/main/src/StepInput.ts index 79922074d79c..89b58e949447 100644 --- a/packages/main/src/StepInput.ts +++ b/packages/main/src/StepInput.ts @@ -308,6 +308,10 @@ class StepInput extends UI5Element implements IFormInputElement { _languageChanged?: boolean = false; + _delimiter?: string; + + _groupSeparator?: string; + @i18n("@ui5/webcomponents") static i18nBundle: I18nBundle; @@ -422,6 +426,8 @@ class StepInput extends UI5Element implements IFormInputElement { this._languageChangeHandler = () => { this._formatter = undefined; this._languageChanged = true; + this._delimiter = undefined; + this._groupSeparator = undefined; return Promise.resolve(); }; @@ -445,6 +451,24 @@ class StepInput extends UI5Element implements IFormInputElement { return this._formatter; } + get delimiter() { + if (!this._delimiter) { + const localeData = getCachedLocaleDataInstance(getLocale()); + this._delimiter = localeData.getNumberSymbol("decimal") || "."; + } + + return this._delimiter; + } + + get groupSeparator() { + if (!this._groupSeparator) { + const localeData = getCachedLocaleDataInstance(getLocale()); + this._groupSeparator = localeData.getNumberSymbol("group") || ","; + } + + return this._groupSeparator; + } + get input(): Input { return this.shadowRoot!.querySelector("[ui5-input]")!; } @@ -603,9 +627,7 @@ class StepInput extends UI5Element implements IFormInputElement { } get _isValueWithCorrectPrecision() { - const localeData = getCachedLocaleDataInstance(getLocale()); - // gets either "." or "," as delimiter which is based on locale, and splits the number by it - const delimiter = localeData.getNumberSymbol("decimal") || "."; + const delimiter = this.delimiter; // check if the value will be displayed with correct precision // _displayValue has special formatting logic if (this.valuePrecision === 0 && !this.input?.value.includes(delimiter) && ((this.value === 0) || (Number.isInteger(this.value)))) { @@ -620,7 +642,8 @@ class StepInput extends UI5Element implements IFormInputElement { _onInputChange() { this._setDefaultInputValueIfNeeded(); - const inputValue = this._parseNumber(this.input.value); + const updatedValue = this._removeGroupSeparators(this.input.value); + const inputValue = this._parseNumber(updatedValue); if (this._isValueChanged(inputValue)) { this._updateValueAndValidate(Number.isNaN(inputValue) ? this.min || 0 : inputValue); this.innerInput.value = this.input.value; @@ -730,7 +753,14 @@ class StepInput extends UI5Element implements IFormInputElement { } _getValueOnkeyDown(e: KeyboardEvent, inputValue: string, cursorPosition?: number) { - return `${inputValue.substring(0, cursorPosition)}${e.key}${inputValue.substring(cursorPosition!)}`; + const typedValue = `${inputValue.substring(0, cursorPosition)}${e.key}${inputValue.substring(cursorPosition!)}`; + const updatedValue = this._removeGroupSeparators(typedValue); + return updatedValue; + } + + _removeGroupSeparators(value: string) { + const groupSeparator = this.groupSeparator; + return value.replaceAll(groupSeparator, ""); } _isInputValueValid(typedValue: string, parsedValue: number) {