Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 77 additions & 6 deletions packages/main/cypress/specs/StepInput.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,77 @@ describe("StepInput property propagation", () => {
});
});

describe("Number expressions with constants", () => {
it("should work properly with number expressions which contains Euler`s constant", () => {
cy.mount(
<StepInput value={5}></StepInput>
);

cy.get("[ui5-step-input]")
.as("stepInput");

cy.get<StepInput>("@stepInput")
.ui5StepInputAttachHandler("ui5-change", "change");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber("1e3");

cy.get("@change")
.should("have.been.calledOnce");

cy.get<StepInput>("@stepInput")
.should("have.prop", "value", 1000);
});

it("should work properly with number expressions which contains Euler`s constant and decimals", () => {
cy.mount(
<StepInput value={5} valuePrecision={2}></StepInput>
);

cy.get("[ui5-step-input]")
.as("stepInput");

cy.get<StepInput>("@stepInput")
.ui5StepInputAttachHandler("ui5-change", "change");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber("1.5e3");

cy.get("@change")
.should("have.been.calledOnce");

cy.get<StepInput>("@stepInput")
.should("have.prop", "value", 1500);

cy.get<StepInput>("@stepInput")
.should("have.prop", "valueState", "None");
});

it("should set default value when value is out of bounds", () => {
cy.mount(
<StepInput value={5} max={20}></StepInput>
);

cy.get("[ui5-step-input]")
.as("stepInput");

cy.get<StepInput>("@stepInput")
.ui5StepInputAttachHandler("ui5-change", "change");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber("1e9999");

cy.get("@change")
.should("have.been.calledOnce");

cy.get<StepInput>("@stepInput")
.should("have.prop", "value", 0);

cy.get<StepInput>("@stepInput")
.should("have.prop", "valueState", "None");
});
});

describe("Validation inside form", () => {
it("has correct validity for patternMissmatch", () => {
cy.mount(
Expand All @@ -835,7 +906,7 @@ describe("Validation inside form", () => {
.as("stepInput");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber(2.34);
.ui5StepInputTypeNumber("2.34");

cy.get("#submitBtn")
.realClick();
Expand All @@ -855,7 +926,7 @@ describe("Validation inside form", () => {
.should("exist", "StepInput without formatted value should have :invalid CSS class");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber(2.345);
.ui5StepInputTypeNumber("2.345");

cy.get("@stepInput")
.ui5AssertValidityState({
Expand Down Expand Up @@ -886,7 +957,7 @@ describe("Validation inside form", () => {
.as("stepInput");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber(2);
.ui5StepInputTypeNumber("2");

cy.get("#submitBtn")
.realClick();
Expand All @@ -906,7 +977,7 @@ describe("Validation inside form", () => {
.should("exist", "StepInput with value lower than min should have :invalid CSS class");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber(4);
.ui5StepInputTypeNumber("4");

cy.get("@stepInput")
.ui5AssertValidityState({
Expand Down Expand Up @@ -938,7 +1009,7 @@ describe("Validation inside form", () => {
.as("stepInput");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber(4);
.ui5StepInputTypeNumber("4");

cy.get("#submitBtn")
.realClick();
Expand All @@ -958,7 +1029,7 @@ describe("Validation inside form", () => {
.should("exist", "StepInput without value lower than min should have :invalid CSS class");

cy.get<StepInput>("@stepInput")
.ui5StepInputTypeNumber(2);
.ui5StepInputTypeNumber("2");

cy.get("@stepInput")
.ui5AssertValidityState({
Expand Down
6 changes: 3 additions & 3 deletions packages/main/cypress/support/commands/StepInput.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Cypress.Commands.add("ui5StepInputCheckInnerInputProperty", { prevSubject: true
});
});

Cypress.Commands.add("ui5StepInputTypeNumber", { prevSubject: true }, (subject, value: number) => {
Cypress.Commands.add("ui5StepInputTypeNumber", { prevSubject: true }, (subject, value: string) => {
cy.wrap(subject)
.as("stepInput")
.should("be.visible");
Expand All @@ -79,7 +79,7 @@ Cypress.Commands.add("ui5StepInputTypeNumber", { prevSubject: true }, (subject,
.shadow()
.find("input")
.clear()
.realType(value.toString())
.realType(value)
.realPress("Enter");
});

Expand Down Expand Up @@ -120,7 +120,7 @@ declare global {
ui5StepInputAttachHandler(eventName: string, stubName: string): Chainable<void>
ui5StepInputGetInnerInput(): Chainable<JQuery<HTMLElement>>
ui5StepInputCheckInnerInputProperty(propName: string, expectedValue: any, shouldBePropagated?: boolean): Chainable<void>
ui5StepInputTypeNumber(value: number): Chainable<void>
ui5StepInputTypeNumber(value: string): Chainable<void>
ui5StepInputScrollToChangeValue(expectedValue: number, decreaseValue: boolean): Chainable<void>
}
}
Expand Down
45 changes: 39 additions & 6 deletions packages/main/src/StepInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,13 @@ class StepInput extends UI5Element implements IFormInputElement {
}

_updateValueState() {
const isWithinRange = (this.min === undefined || this._parseNumber(this.input.value) >= this.min)
&& (this.max === undefined || this._parseNumber(this.input.value) <= this.max);
let parsedValue = this._parseNumber(this.input.value);
if (this._isInfinity(parsedValue) && this._isScientificNotation(this.input.value)) {
parsedValue = this._defaultValue;
}

const isWithinRange = (this.min === undefined || parsedValue >= this.min)
&& (this.max === undefined || parsedValue <= this.max);
const isValueWithCorrectPrecision = this._isValueWithCorrectPrecision;
const previousValueState = this.valueState;
const isValid = isWithinRange && isValueWithCorrectPrecision;
Expand Down Expand Up @@ -627,6 +632,10 @@ class StepInput extends UI5Element implements IFormInputElement {
}

get _isValueWithCorrectPrecision() {
if (this._isScientificNotation(this.input?.value)) {
return true;
}

const delimiter = this.delimiter;
// check if the value will be displayed with correct precision
// _displayValue has special formatting logic
Expand All @@ -640,19 +649,32 @@ class StepInput extends UI5Element implements IFormInputElement {
return decimalPartLength === this.valuePrecision;
}

_isInfinity(value: number) {
return Math.abs(value) === Infinity;
}

_onInputChange() {
this._setDefaultInputValueIfNeeded();
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);
let inputValue = this._parseNumber(updatedValue);
const isInfinity = this._isInfinity(inputValue);
if (isInfinity) {
inputValue = this._defaultValue;
}

if (this._isValueChanged(inputValue) || isInfinity) {
this._updateValueAndValidate(Number.isNaN(inputValue) ? this._defaultValue : inputValue);
this.innerInput.value = this.input.value;
}
}

get _defaultValue() {
return this.min !== undefined ? this.min : 0;
}

_setDefaultInputValueIfNeeded() {
if (this.input.value === "") {
const defaultValue = this._formatNumber(this.min || 0);
const defaultValue = this._formatNumber(this._defaultValue);
this.input.value = defaultValue;
this.innerInput.value = defaultValue; // we need to update inner input value as well, to avoid empty input scenario
}
Expand Down Expand Up @@ -763,7 +785,18 @@ class StepInput extends UI5Element implements IFormInputElement {
return value.replaceAll(groupSeparator, "");
}

_isScientificNotation(value: string) {
const sEscapedDecimalSep = this.delimiter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const oScientificNotationRegex = new RegExp(`^[+-]?\\d+(${sEscapedDecimalSep}\\d*)?[eE]([+-]?\\d+)?$`);

return oScientificNotationRegex.test(value);
}

_isInputValueValid(typedValue: string, parsedValue: number) {
if (this._isScientificNotation(typedValue)) {
return true;
}

return !Number.isNaN(parsedValue) && !/, {2,}/.test(typedValue);
}

Expand Down
Loading