Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.

Commit b099b08

Browse files
PIERMÉ Jean-LouPIERMÉ Jean-Lou
authored andcommitted
refactor(action,button): remove hocs
The HOCs not only disturb the bundler's process to understand which code does what, it also makes typing a pain. Moreover, the HOC used here removed the react event and replaced it with only the id of the HTML element. This version makes typing slightly simpler, and gives user the complete event while maintaining existing behaviour.
1 parent bb84255 commit b099b08

3 files changed

Lines changed: 49 additions & 22 deletions

File tree

packages/action/src/Action.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/action/src/Action.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { ComponentProps, EventHandler, SyntheticEvent } from 'react';
2+
3+
import ActionCore from './ActionCore';
4+
5+
interface MyEvent extends React.SyntheticEvent {
6+
id: string;
7+
}
8+
9+
export type ActionProps = Omit<ComponentProps<typeof ActionCore>, 'onClick'> & {
10+
onClick: React.EventHandler<MyEvent>;
11+
};
12+
13+
function useWithEventId<T extends SyntheticEvent>(fn: EventHandler<MyEvent>) {
14+
return (event: T) => {
15+
fn({ ...event, id: event.currentTarget.id });
16+
};
17+
}
18+
19+
const Action = ({ onClick, ...rest }: ActionProps) => {
20+
const onClickCustom = useWithEventId(onClick);
21+
22+
return <ActionCore {...rest} onClick={onClickCustom} />;
23+
};
24+
25+
export default Action;

packages/button/src/Button.tsx

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
import {
2-
withClickId,
3-
WithClickIdProps,
4-
compose,
5-
identity,
6-
} from '@axa-fr/react-toolkit-core';
7-
import { ComponentPropsWithoutRef } from 'react';
1+
import React, { ComponentPropsWithoutRef } from 'react';
82

93
import ButtonCore from './ButtonCore';
104

11-
type ButtonCoreProps = ComponentPropsWithoutRef<typeof ButtonCore>;
12-
export type ButtonProps = WithClickIdProps<ButtonCoreProps, 'onClick'>;
5+
interface MyEvent extends React.MouseEvent<HTMLButtonElement> {
6+
id: string;
7+
}
138

14-
const Button = compose(
15-
identity<ButtonCoreProps>(),
16-
withClickId({ event: ['onClick'] })
17-
)(ButtonCore);
9+
export type ButtonProps = Omit<
10+
ComponentPropsWithoutRef<typeof ButtonCore>,
11+
'onClick'
12+
> & {
13+
onClick: React.EventHandler<MyEvent>;
14+
};
15+
16+
const Button = (props: ButtonProps) => {
17+
const customOnClick = (
18+
e: React.MouseEvent<HTMLButtonElement, MouseEvent>,
19+
onClick: React.EventHandler<MyEvent>
20+
) => {
21+
return onClick({ ...e, id: props.id });
22+
};
23+
24+
return (
25+
<ButtonCore {...props} onClick={(e) => customOnClick(e, props.onClick)} />
26+
);
27+
};
1828

1929
Button.displayName = 'Button';
2030

2131
export default Button;
32+
export { ButtonCore };

0 commit comments

Comments
 (0)