Skip to content

fix(input, input-number): increment/decrement to the min/max when value is below/above #6207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions src/components/input-number/input-number.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,38 @@ describe("calcite-input-number", () => {
expect(await input.getProperty("value")).toBe(`${finalNudgedValue * 0.01}`);
});

it("decrements to max when value is higher", async () => {
await page.setContent(html`<calcite-input-number max="10" value="20"></calcite-input-number>`);

const element = await page.find("calcite-input-number");
const numberHorizontalItemDown = await page.find(
"calcite-input-number >>> .number-button-item[data-adjustment='down']"
);

await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("10");
await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("9");
});

it("increments to min when value is lower", async () => {
await page.setContent(html`<calcite-input-number min="20" value="11"></calcite-input-number>`);

const element = await page.find("calcite-input-number");
const numberHorizontalItemDown = await page.find(
"calcite-input-number >>> .number-button-item[data-adjustment='down']"
);

await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("20");
await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("20");
});

it("correctly increments and decrements value by one when any is set for step", async () => {
await page.setContent(html`<calcite-input-number step="any" value="5.5"></calcite-input-number>`);

Expand Down
7 changes: 4 additions & 3 deletions src/components/input-number/input-number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,10 @@ export class InputNumber
const adjustment = direction === "up" ? 1 : -1;
const nudgedValue = inputVal + inputStep * adjustment;
const finalValue =
(typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
(typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
? inputVal
typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin
? inputMin
: typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax
? inputMax
: nudgedValue;

const inputValPlaces = decimalPlaces(inputVal);
Expand Down
28 changes: 28 additions & 0 deletions src/components/input/input.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,34 @@ describe("calcite-input", () => {
expect(await element.getProperty("value")).toBe("6");
});

it("decrements to max when value is higher", async () => {
await page.setContent(html`<calcite-input type="number" max="10" value="20"></calcite-input>`);

const element = await page.find("calcite-input");
const numberHorizontalItemDown = await page.find("calcite-input >>> .number-button-item[data-adjustment='down']");

await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("10");
await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("9");
});

it("increments to min when value is lower", async () => {
await page.setContent(html`<calcite-input type="number" min="20" value="11"></calcite-input>`);

const element = await page.find("calcite-input");
const numberHorizontalItemDown = await page.find("calcite-input >>> .number-button-item[data-adjustment='down']");

await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("20");
await numberHorizontalItemDown.click();
await page.waitForChanges();
expect(await element.getProperty("value")).toBe("20");
});

it("correctly stops decrementing value when min is set", async () => {
await page.setContent(html`<calcite-input type="number" min="10" value="11"></calcite-input>`);

Expand Down
7 changes: 4 additions & 3 deletions src/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,10 @@ export class Input
const adjustment = direction === "up" ? 1 : -1;
const nudgedValue = inputVal + inputStep * adjustment;
const finalValue =
(typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
(typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
? inputVal
typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin
? inputMin
: typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax
? inputMax
: nudgedValue;

const inputValPlaces = decimalPlaces(inputVal);
Expand Down