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: ToggleButton usage #4429

Merged
merged 1 commit into from
Nov 15, 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
2 changes: 1 addition & 1 deletion packages/core/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const HvButton = fixedForwardRef(function HvButton<
tabIndex: focusableWhenDisabled ? 0 : -1,
"aria-disabled": true,
})}
{...(selected && { "aria-pressed": selected })}
{...(selected != null && { "aria-pressed": selected })}
{...others}
>
{startIcon && <span className={classes.startIcon}>{startIcon}</span>}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/TimePicker/TimePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export const Main: StoryObj<HvTimePickerProps> = {
label: "Time Picker",
description: "",
placeholder: "Select a date",
timeFormat: "24",
},
argTypes: {
classes: { control: { disable: true } },
timeFormat: { control: { disable: true } },
value: { control: { disable: true } },
defaultValue: { control: { disable: true } },
dropdownProps: { control: { disable: true } },
Expand Down
18 changes: 8 additions & 10 deletions packages/core/src/TimePicker/Unit/Unit.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { KeyboardEvent } from "react";
import { DateFieldState, DateSegment } from "@react-stately/datepicker";
import {
DropUpXS as AddTimeIcon,
DropDownXS as SubtractTimeIcon,
} from "@hitachivantara/uikit-react-icons";
import { theme } from "@hitachivantara/uikit-styles";

import { HvInput, HvInputProps } from "../../Input";
import { HvToggleButton } from "../../ToggleButton";
import { HvBaseInput, HvBaseInputProps } from "../../BaseInput";
import { HvButton } from "../../Button";
import { useClasses } from "./Unit.styles";

interface UnitProps {
Expand All @@ -16,7 +15,7 @@ interface UnitProps {
segment: DateSegment;
placeholder?: string;
/** Called when the value changes */
onChange?: HvInputProps["onChange"];
onChange?: HvBaseInputProps["onChange"];
/** Called when the up/add arrow is pressed */
onAdd?: () => void;
/** Called when the down/subtract arrow is pressed */
Expand All @@ -41,14 +40,13 @@ export const Unit = ({
{type !== "literal" && <AddTimeIcon onClick={onAdd} />}
{type === "literal" && <div className={classes.separator}>{text}</div>}
{type === "dayPeriod" && (
<HvToggleButton className={classes.periodToggle} onClick={onAdd}>
<HvButton icon className={classes.periodToggle} onClick={onAdd}>
{text}
</HvToggleButton>
</HvButton>
)}
{["hour", "minute", "second"].includes(type) && (
<HvInput
<HvBaseInput
id={id}
disableClear
style={{
...theme.typography.title3,
}}
Expand All @@ -59,13 +57,13 @@ export const Unit = ({
inputRoot: classes.inputRoot,
}}
onKeyDown={(event) => {
if ((event as KeyboardEvent).key === "Enter") {
if ("key" in event && event.key === "Enter") {
event.preventDefault();
event.stopPropagation();
}
}}
required
status={state.validationState}
invalid={state.isInvalid}
value={text.padStart(2, "0")}
onChange={onChange}
placeholder={placeholder}
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/ToggleButton/ToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ export const HvToggleButton = forwardRef<
Boolean(defaultSelected),
);

const onClickHandler = (event: React.MouseEvent<HTMLButtonElement>) => {
setIsSelected(!isSelected);
onClick?.(event, !isSelected);
};

return (
<HvButton
ref={ref}
icon
aria-pressed={isSelected}
onClick={onClickHandler}
selected={isSelected}
onClick={(event) => {
setIsSelected(!isSelected);
onClick?.(event, !isSelected);
}}
{...others}
>
{children || (!isSelected ? notSelectedIcon : selectedIcon)}
Expand Down