Skip to content

Commit d9eb215

Browse files
authored
fix(input, input-number): increment/decrement to the min/max when value is below/above (#6207)
**Related Issue:** #6201 ## Summary Follows the native input's behavior by incrementing/decrementing to the `min`/`max` property when `value` is below/above
1 parent 2d8fb41 commit d9eb215

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

src/components/input-number/input-number.e2e.ts

+32
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,38 @@ describe("calcite-input-number", () => {
333333
expect(await input.getProperty("value")).toBe(`${finalNudgedValue * 0.01}`);
334334
});
335335

336+
it("decrements to max when value is higher", async () => {
337+
await page.setContent(html`<calcite-input-number max="10" value="20"></calcite-input-number>`);
338+
339+
const element = await page.find("calcite-input-number");
340+
const numberHorizontalItemDown = await page.find(
341+
"calcite-input-number >>> .number-button-item[data-adjustment='down']"
342+
);
343+
344+
await numberHorizontalItemDown.click();
345+
await page.waitForChanges();
346+
expect(await element.getProperty("value")).toBe("10");
347+
await numberHorizontalItemDown.click();
348+
await page.waitForChanges();
349+
expect(await element.getProperty("value")).toBe("9");
350+
});
351+
352+
it("increments to min when value is lower", async () => {
353+
await page.setContent(html`<calcite-input-number min="20" value="11"></calcite-input-number>`);
354+
355+
const element = await page.find("calcite-input-number");
356+
const numberHorizontalItemDown = await page.find(
357+
"calcite-input-number >>> .number-button-item[data-adjustment='down']"
358+
);
359+
360+
await numberHorizontalItemDown.click();
361+
await page.waitForChanges();
362+
expect(await element.getProperty("value")).toBe("20");
363+
await numberHorizontalItemDown.click();
364+
await page.waitForChanges();
365+
expect(await element.getProperty("value")).toBe("20");
366+
});
367+
336368
it("correctly increments and decrements value by one when any is set for step", async () => {
337369
await page.setContent(html`<calcite-input-number step="any" value="5.5"></calcite-input-number>`);
338370

src/components/input-number/input-number.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,10 @@ export class InputNumber
526526
const adjustment = direction === "up" ? 1 : -1;
527527
const nudgedValue = inputVal + inputStep * adjustment;
528528
const finalValue =
529-
(typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
530-
(typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
531-
? inputVal
529+
typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin
530+
? inputMin
531+
: typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax
532+
? inputMax
532533
: nudgedValue;
533534

534535
const inputValPlaces = decimalPlaces(inputVal);

src/components/input/input.e2e.ts

+28
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,34 @@ describe("calcite-input", () => {
383383
expect(await element.getProperty("value")).toBe("6");
384384
});
385385

386+
it("decrements to max when value is higher", async () => {
387+
await page.setContent(html`<calcite-input type="number" max="10" value="20"></calcite-input>`);
388+
389+
const element = await page.find("calcite-input");
390+
const numberHorizontalItemDown = await page.find("calcite-input >>> .number-button-item[data-adjustment='down']");
391+
392+
await numberHorizontalItemDown.click();
393+
await page.waitForChanges();
394+
expect(await element.getProperty("value")).toBe("10");
395+
await numberHorizontalItemDown.click();
396+
await page.waitForChanges();
397+
expect(await element.getProperty("value")).toBe("9");
398+
});
399+
400+
it("increments to min when value is lower", async () => {
401+
await page.setContent(html`<calcite-input type="number" min="20" value="11"></calcite-input>`);
402+
403+
const element = await page.find("calcite-input");
404+
const numberHorizontalItemDown = await page.find("calcite-input >>> .number-button-item[data-adjustment='down']");
405+
406+
await numberHorizontalItemDown.click();
407+
await page.waitForChanges();
408+
expect(await element.getProperty("value")).toBe("20");
409+
await numberHorizontalItemDown.click();
410+
await page.waitForChanges();
411+
expect(await element.getProperty("value")).toBe("20");
412+
});
413+
386414
it("correctly stops decrementing value when min is set", async () => {
387415
await page.setContent(html`<calcite-input type="number" min="10" value="11"></calcite-input>`);
388416

src/components/input/input.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,10 @@ export class Input
610610
const adjustment = direction === "up" ? 1 : -1;
611611
const nudgedValue = inputVal + inputStep * adjustment;
612612
const finalValue =
613-
(typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
614-
(typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
615-
? inputVal
613+
typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin
614+
? inputMin
615+
: typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax
616+
? inputMax
616617
: nudgedValue;
617618

618619
const inputValPlaces = decimalPlaces(inputVal);

0 commit comments

Comments
 (0)