Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fraincs committed Sep 20, 2024
1 parent 3f4a075 commit 6901b5e
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 58 deletions.
2 changes: 1 addition & 1 deletion packages/ActionMenu/src/ActionMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('ActionMenu', () => {
label: 'Add Item',
value: 'add',
onClick: (e) => {
e.preventDefault();
e?.preventDefault();
selected = true;
}
},
Expand Down
19 changes: 5 additions & 14 deletions packages/ActionMenu/src/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ActionMenuOption extends Omit<Option, "type"> {
/** Whether or not the action menu should close when an option is selected */
closeOnSelect?: boolean | ((option: OptionType) => boolean);
/** Callback when an option is selected */
onClick?: (() => void) | React.MouseEventHandler<HTMLButtonElement>;
onClick?: (e?: React.SyntheticEvent) => void;
}

export interface ActionMenuProps extends React.ComponentProps<"div"> {
Expand Down Expand Up @@ -119,21 +119,12 @@ const ActionMenu: React.FunctionComponent<ActionMenuProps> = ({
return closeOnSelect;
};

const selectOption = (option: OptionType): void => {
const selectOption = (option: OptionType, e?: React.SyntheticEvent): void => {
const actionMenuOption = options.find(
actionMenuOption => actionMenuOption.value === option.value
);
const onOptionSelect = actionMenuOption?.onClick;
if (onOptionSelect) {
if (onOptionSelect.length > 0) {
// eslint-disable-next-line max-len
const syntheticEvent = new MouseEvent("click") as unknown as React.MouseEvent<HTMLButtonElement>;
onOptionSelect(syntheticEvent);
} else {
// Call the function without arguments if it doesn't expect any
(onOptionSelect as () => void)();
}
}

actionMenuOption?.onClick?.(e);

if (closeMenuOnSelect(option)) {
toggleMenu(false);
Expand Down Expand Up @@ -194,7 +185,7 @@ const ActionMenu: React.FunctionComponent<ActionMenuProps> = ({
keyboardEvent.preventDefault();
keyboardEvent.stopPropagation();
if (currentFocusedOption) {
selectOption(currentFocusedOption);
selectOption(currentFocusedOption, keyboardEvent);
}
if ((!currentFocusedOption && showMenu) || !showMenu) {
toggleMenu(!showMenu);
Expand Down
2 changes: 1 addition & 1 deletion packages/Alert/src/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type Appearance = "card" | "inline" | "horizontal";

export interface AlertButton {
label: React.ReactNode;
onClick: (() => void) | React.MouseEventHandler<HTMLButtonElement>;
onClick?: (e?: React.SyntheticEvent) => void;
}

export interface AlertProps extends Omit<React.ComponentProps<"div">, "title"> {
Expand Down
2 changes: 1 addition & 1 deletion packages/Button/src/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface ButtonOwnProps {
/** Display only the icon in mobile */
showOnlyIconOnMobile?: boolean;
/** Callback when clicked */
onClick?: (() => void) | React.MouseEventHandler<HTMLButtonElement>;
onClick?: (e?: React.SyntheticEvent) => void;
/** Optional prop to specify the type of the Button */
type?: "button" | "reset" | "submit";
/** Add a data-intercom-target with unique id to link a
Expand Down
2 changes: 1 addition & 1 deletion packages/Filter/src/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface FilterProps extends React.ComponentProps<"button"> {
/** True if the filter should be disabled */
disabled?: boolean;
/** Add an event for when the filter is clicked */
onClick?: (() => void) | React.MouseEventHandler<HTMLButtonElement>;
onClick?: (e?: React.SyntheticEvent) => void;
/** True if the tag is selected */
selected?: boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/IconButton/src/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface IconButtonProps extends Omit<ButtonOwnProps, "size"> {
icon: React.ReactNode;
/** Callback function that will be called
* when the user clicks on the button */
onClick?: (() => void) | React.MouseEventHandler<HTMLButtonElement>;
onClick?: (e?: React.SyntheticEvent) => void;
/** True if the control is disabled and shows a disabled state.
* The user cannot click on the button */
disabled?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/List/src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface ListProps extends React.ComponentPropsWithRef<"ul"> {
disableTabbing?: boolean;
/** The option that is currently being focused or hovered */
focusedOption?: OptionType | null;
/** True for a compact appearance
/** True for a compact appearance
* (Corresponds to "small" in Figma for compact, and "medium" for non-compact) */
isCompact?: boolean;
/** Whether or not the list is loading */
Expand All @@ -25,7 +25,7 @@ export interface ListProps extends React.ComponentPropsWithRef<"ul"> {
/** Called when an option becomes focused or hovered */
onOptionFocus?: (option: OptionType) => void;
/** Called when an option is selected */
onOptionChange?: (option: OptionType) => void;
onOptionChange?: (option: OptionType, e?: React.SyntheticEvent) => void;
/** Called when the mouse moves outside of the option
* or the option loses focus */
onOptionBlur?: (option: OptionType) => void;
Expand Down
8 changes: 4 additions & 4 deletions packages/List/src/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface ListItemProps extends React.ComponentProps<"li"> {
/** Called when an option becomes focused or hovered */
onOptionFocus?: (option: OptionType) => void;
/** Called when an option is selected */
onOptionChange?: (option: OptionType) => void;
onOptionChange?: (option: OptionType, e?: React.SyntheticEvent) => void;
/** Called when the mouse moves outside of the option
* or the option loses focus */
onOptionBlur?: (option: OptionType) => void;
Expand Down Expand Up @@ -127,9 +127,9 @@ const ListItem: React.FunctionComponent<ListItemProps> = ({
}
};

const handleOptionChange = (item: OptionType): void => {
const handleOptionChange = (item: OptionType, e?: React.SyntheticEvent): void => {
if (!isOptionDisabled() && onOptionChange) {
onOptionChange(item);
onOptionChange(item, e);
}
};

Expand Down Expand Up @@ -218,7 +218,7 @@ const ListItem: React.FunctionComponent<ListItemProps> = ({
!(target as HTMLElement).closest("button")
) {
if (option) {
handleOptionChange(option);
handleOptionChange(option, e);
}
}
}}
Expand Down
25 changes: 0 additions & 25 deletions packages/Stepper/src/Stepper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,6 @@ describe('Stepper', () => {
}
});

test('It should accept React.MouseEventHandler<HTMLButtonElement> as onClick prop', () => {
const mockOnClick: React.MouseEventHandler<HTMLButtonElement> = jest.fn();

const steps = [
{ title: 'Step 1', onClick: mockOnClick },
{ title: 'Step 2' },
{ title: 'Step 3' },
];

const currentStep = 1;
const {container} = setup({steps: steps, currentStep: currentStep, clickableNextSteps: false});

const props: StepperProps = {
steps: steps,
currentStep: 0,
};

render(<Stepper {...props} />);

const stepElements = container.querySelectorAll('.ids-step');
fireEvent.click(stepElements[0]);

expect(mockOnClick).toHaveBeenCalled();
});

test('Enables steps after the current step if clickableNextSteps is true', () => {
const currentStep = 1;

Expand Down
11 changes: 3 additions & 8 deletions packages/Stepper/src/Stepper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "./stepper.scss";

export interface Step {
/** The callback function that is called when the step is clicked */
onClick?: ((index: number) => void) | React.MouseEventHandler<HTMLButtonElement>;
onClick?: ((index: number, e?: PressEvent) => void);
/** The title for the step */
title: string;
}
Expand Down Expand Up @@ -55,13 +55,8 @@ const Stepper: React.FunctionComponent<StepperProps> = ({
isComplete={isComplete}
isCurrent={isCurrent}
disabled={disabled}
onPress={(event: PressEvent) => {
if (typeof step.onClick === "function") {
(step.onClick as (index: number) => void)(index);
} else if (step.onClick) {
// eslint-disable-next-line max-len
(step.onClick as React.MouseEventHandler<HTMLButtonElement>)(event as unknown as React.MouseEvent<HTMLButtonElement>);
}
onPress={(e: PressEvent) => {
step.onClick?.(index, e);
}}
/>
{index < steps.length - 1 && (
Expand Down

0 comments on commit 6901b5e

Please sign in to comment.