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

chore: export type EventHandler/EventData from react-utilities for typing callback props #30322

Merged
merged 4 commits into from
Jan 17, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Add new type EventHandler/EventData for callback props",
"packageName": "@fluentui/react-utilities",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export function createPriorityQueue<T>(compare: PriorityQueueCompareFn<T>): Prio
// @internal
export function elementContains(parent: Node | null, child: Node | null): boolean;

// @public
export type EventData<Type extends string, TEvent> = {
type: undefined;
event: React_2.SyntheticEvent | Event;
} | {
type: Type;
event: TEvent;
};

// @public
export type EventHandler<TData extends EventData<string, unknown>> = (ev: React_2.SyntheticEvent | Event, data: TData) => void;

// @public
export type ExtractSlotProps<S> = Exclude<S, SlotShorthandValue | null | undefined>;

Expand Down
31 changes: 31 additions & 0 deletions packages/react-components/react-utilities/src/compose/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,34 @@ export type SlotComponentType<Props extends UnknownSlotProps> = Props & {
| React.ComponentType<Props>
| (Props extends AsIntrinsicElement<infer As> ? As : keyof JSX.IntrinsicElements);
};

/**
* Data type for event handlers. It makes data a discriminated union, where each object requires `event` and `type` property.
* - `event` is the specific event type
* - `type` is a string literal. It serves as a clear identifier of the event type that reflects the component's state when the event occurred.
* For example, the Tree component's `onNavigation` event handler has different `type` for different key presses: `{ event: React.KeyboardEvent<HTMLElement>; type: typeof ArrowRight } | { event: React.KeyboardEvent<HTMLElement>; type: typeof ArrowLeft }`.
* Developers can use the `type` property to identify and filter events of interest.
* See RFC event-handlers-event-type.md for more details.
*
* Example usage:
* type OnOpenChangeData = (
* | EventData\<'click', React.MouseEvent\<MyComponentElement\>\>
* | EventData\<'keydown', React.KeyboardEvent\<MyComponentElement\>\>
* ) & \{ open: boolean; \};
*/
export type EventData<Type extends string, TEvent> =
| { type: undefined; event: React.SyntheticEvent | Event }
| { type: Type; event: TEvent };

/**
* Type for props that are event handlers.
* See RFC event-handlers-event-type.md for more details.
*
* Example usage:
* type OnSomeEventData = EventData\<'click', React.MouseEvent\<MyComponentElement\>\> & \{ open: boolean; \};
* type SomeProps = \{ onSomeEvent?: EventHandler\<OnSomeEventData\>; \};
*/
export type EventHandler<TData extends EventData<string, unknown>> = (
ev: React.SyntheticEvent | Event,
data: TData,
) => void;
2 changes: 2 additions & 0 deletions packages/react-components/react-utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type {
SlotComponentType,
SlotOptions,
InferredElementRefType,
EventData,
EventHandler,
} from './compose/index';

export {
Expand Down
Loading