Skip to content

Commit

Permalink
refactor(toolkit): update polymorphism types
Browse files Browse the repository at this point in the history
- Update `InferOtherProps` to use `React.ComponentPropsWithRef` instead of `React.ComponentPropsWithoutRef` to include ref prop
- Remove the separate `RefProp` and `PolymorphicPropsWithRef` types as the ref prop is now included in `PolymorphicProps`
  • Loading branch information
Ryan-Zayne committed Nov 8, 2024
1 parent 85d7c0e commit cc6c4d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .changeset/strong-fireants-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@zayne-labs/toolkit": patch
---

refactor(toolkit): update polymorphism types

- Update `InferOtherProps` to use `React.ComponentPropsWithRef` instead of `React.ComponentPropsWithoutRef` to include ref prop
- Remove the separate `RefProp` and `PolymorphicPropsWithRef` types as the ref prop is now included in `PolymorphicProps`
12 changes: 1 addition & 11 deletions packages/toolkit/src/react/types/polymorphism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type PropsWithOptionalAs<TElement extends React.ElementType, TProps> = "as" exte

// == Get all other primitive element props by Omitting the result of MergedProps from React.ComponentPropsWithoutRef
type InferOtherProps<TElement extends React.ElementType, TProps> = Omit<
React.ComponentPropsWithoutRef<TElement>,
React.ComponentPropsWithRef<TElement>,
// == Removing children and className as well to give components control over these props
"children" | "className" | keyof PropsWithOptionalAs<TElement, TProps>
>;
Expand All @@ -17,13 +17,3 @@ export type PolymorphicProps<
TElement extends React.ElementType,
TProps extends Record<string, unknown> = AsProp<TElement>,
> = InferOtherProps<TElement, TProps> & PropsWithOptionalAs<TElement, TProps>;

type RefProp<TElement extends React.ElementType> = {
ref?: React.ComponentPropsWithRef<TElement>["ref"];
};

// == For components with the Ref Prop
export type PolymorphicPropsWithRef<
TElement extends React.ElementType,
TProps extends Record<string, unknown> = AsProp<TElement>,
> = PolymorphicProps<TElement, TProps> & RefProp<TElement>;

0 comments on commit cc6c4d6

Please sign in to comment.