Skip to content
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

fix(time-input): minValue and maxValue validation #3426

Merged
merged 3 commits into from
Jul 12, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/good-feet-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/date-input": patch
---

Fix minValue and maxValue validation (#3424)
76 changes: 76 additions & 0 deletions packages/components/date-input/__tests__/time-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ describe("TimeInput", () => {
}
}
});

it("should support error message (with isInvalid)", function () {
const {getAllByRole, getByRole} = render(
<TimeInput isInvalid errorMessage="Error message" label="Time" />,
);

const group = getByRole("group");

expect(group).toHaveAttribute("aria-describedby");

if (group) {
const descById = group.getAttribute("aria-describedby");
const description = descById && document.getElementById(descById);

expect(description).toHaveTextContent("Error message");

const segments = getAllByRole("spinbutton");

for (const segment of segments) {
expect(segment).toHaveAttribute(
"aria-describedby",
group.getAttribute("aria-describedby"),
);
}
}
});
});

describe("Events", function () {
Expand Down Expand Up @@ -354,4 +380,54 @@ describe("TimeInput", () => {
expect(input).toHaveValue("08:30:00");
});
});

describe(`Validation (validationBehavior="aria")`, () => {
it("should display errorMessage when timeValue is less than the minimum (controlled)", () => {
render(<TimeInput label="Time" minValue={new Time(9)} value={new Time(8)} />);

expect(document.querySelector("[data-slot=error-message]")).toBeVisible();
});

it("should display errorMessage when timeValue is less than the minimum (uncontrolled)", async () => {
const {getAllByRole} = render(
<TimeInput defaultValue={new Time(9)} label="Time" minValue={new Time(9)} name="time" />,
);

const input = document.querySelector("input[name=time]");
const segments = getAllByRole("spinbutton");

await user.tab();
expect(input).toHaveValue("09:00:00");
expect(segments[0]).toHaveFocus();
expect(document.querySelector("[data-slot=error-message]")).toBeNull();

await user.keyboard("[ArrowDown]");
expect(input).toHaveValue("08:00:00");
expect(document.querySelector("[data-slot=error-message]")).toBeVisible();
});

it("should display errorMessage when timeValue is greater than the maximum (controlled)", () => {
render(<TimeInput label="Time" maxValue={new Time(17)} value={new Time(18)} />);

expect(document.querySelector("[data-slot=error-message]")).toBeVisible();
});

it("should display errorMessage when timeValue is greater than the maximum (uncontrolled)", async () => {
const {getAllByRole} = render(
<TimeInput defaultValue={new Time(17)} label="Time" maxValue={new Time(17)} name="time" />,
);

const input = document.querySelector("input[name=time]");
const segments = getAllByRole("spinbutton");

await user.tab();
expect(input).toHaveValue("17:00:00");
expect(segments[0]).toHaveFocus();
expect(document.querySelector("[data-slot=error-message]")).toBeNull();

await user.keyboard("[ArrowUp]");
expect(input).toHaveValue("18:00:00");
expect(document.querySelector("[data-slot=error-message]")).toBeVisible();
});
});
});
7 changes: 2 additions & 5 deletions packages/components/date-input/src/use-time-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export function useTimeInput<T extends TimeValue>(originalProps: UseTimeInputPro
endContent,
className,
classNames,
validationState,
groupProps = {},
labelProps: labelPropsProp,
fieldProps: fieldPropsProp,
Expand All @@ -97,7 +96,7 @@ export function useTimeInput<T extends TimeValue>(originalProps: UseTimeInputPro
shouldForceLeadingZeros = true,
minValue,
maxValue,
isInvalid: isInvalidProp = validationState ? validationState === "invalid" : false,
isInvalid: isInvalidProp,
errorMessage,
} = props;

Expand Down Expand Up @@ -127,13 +126,11 @@ export function useTimeInput<T extends TimeValue>(originalProps: UseTimeInputPro
validationDetails,
descriptionProps,
errorMessageProps,
isInvalid: ariaIsInvalid,
isInvalid,
} = useAriaTimeField({...originalProps, label, validationBehavior, inputRef}, state, domRef);

const baseStyles = clsx(classNames?.base, className);

const isInvalid = isInvalidProp || ariaIsInvalid;

const labelPlacement = useMemo<DateInputVariantProps["labelPlacement"]>(() => {
if (
(!originalProps.labelPlacement || originalProps.labelPlacement === "inside") &&
Expand Down