Skip to content

Commit

Permalink
refactor(time-input): simplify isInvalid logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chirokas committed Jul 7, 2024
1 parent f2d04ef commit 7cea350
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
26 changes: 26 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
8 changes: 3 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 All @@ -115,6 +114,7 @@ export function useTimeInput<T extends TimeValue>(originalProps: UseTimeInputPro
minValue,
maxValue,
validationBehavior,
isInvalid: isInvalidProp,
shouldForceLeadingZeros,
});

Expand All @@ -126,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

0 comments on commit 7cea350

Please sign in to comment.