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

Building Strongly Typed Polymorphic Components #18274

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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/react/src/components/ListItem/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import classnames from 'classnames';
import { usePrefix } from '../../internal/usePrefix';
import { Text } from '../Text';

type ListItemProps = ComponentProps<'li'>;
type ListItemProps = Omit<ComponentProps<'li'>, 'ref'>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this fix because of this typescript error:
Types of property 'ref' are incompatible.
This removes the ref property from ComponentProps<'li'>, preventing conflicts with the Text component.


export default function ListItem({
className,
Expand Down
104 changes: 56 additions & 48 deletions packages/react/src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,74 @@
*/

import PropTypes from 'prop-types';
import React, { ReactNode, useContext, useRef } from 'react';
import { PolymorphicProps } from '../../types/common';
import React, { ReactNode, useContext } from 'react';
import { TextDir } from './TextDirection';
import { TextDirectionContext } from './TextDirectionContext';
import { useMergeRefs } from '@floating-ui/react';
import {
PolymorphicComponentPropWithRef,
PolymorphicRef,
} from '../../internal/PolymorphicProps';

export interface TextBaseProps {
dir?: TextDir | undefined;
children?: ReactNode;
}

export type TextProps<T extends React.ElementType> = PolymorphicProps<
T,
TextBaseProps
>;
const Text = React.forwardRef(function Text<T extends React.ElementType>(
{ as, children, dir = 'auto', ...rest }: TextProps<T>,
ref: React.Ref<HTMLElement>
) {
// TODO: Update with context typing once its been converted to TS
const context = useContext<any>(TextDirectionContext);
const textProps: { dir?: TextDir } = {};
const BaseComponent = as ?? 'span';
const value = {
...context,
};

if (!context) {
textProps.dir = dir;
value.direction = dir;
} else {
const { direction: parentDirection, getTextDirection } = context;

if (getTextDirection && getTextDirection.current) {
const text = getTextFromChildren(children);
const override = getTextDirection.current(text);

if (parentDirection !== override) {
textProps.dir = override;
value.direction = override;
} else if (parentDirection === 'auto') {
textProps.dir = override;
}
} else if (parentDirection !== dir) {
export type TextProps<T extends React.ElementType> =
PolymorphicComponentPropWithRef<T, TextBaseProps>;

type TextComponent = <T extends React.ElementType = 'span'>(
props: TextProps<T>
) => React.ReactElement | any;

const Text: TextComponent = React.forwardRef(
<T extends React.ElementType = 'span'>(
{ as, children, dir = 'auto', ...rest }: TextProps<T>,
ref?: PolymorphicRef<T>
) => {
// TODO: Update with context typing once its been converted to TS
const context = useContext<any>(TextDirectionContext);
const textProps: { dir?: TextDir } = {};
const BaseComponent = as ?? 'span';
const value = {
...context,
};

if (!context) {
textProps.dir = dir;
value.direction = dir;
} else if (parentDirection === 'auto') {
textProps.dir = dir;
} else {
const { direction: parentDirection, getTextDirection } = context;

if (getTextDirection && getTextDirection.current) {
const text = getTextFromChildren(children);
const override = getTextDirection.current(text);

if (parentDirection !== override) {
textProps.dir = override;
value.direction = override;
} else if (parentDirection === 'auto') {
textProps.dir = override;

Check warning on line 56 in packages/react/src/components/Text/Text.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react/src/components/Text/Text.tsx#L56

Added line #L56 was not covered by tests
}
} else if (parentDirection !== dir) {
textProps.dir = dir;
value.direction = dir;

Check warning on line 60 in packages/react/src/components/Text/Text.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react/src/components/Text/Text.tsx#L59-L60

Added lines #L59 - L60 were not covered by tests
} else if (parentDirection === 'auto') {
textProps.dir = dir;
}
}
}

return (
<TextDirectionContext.Provider value={value}>
<BaseComponent ref={ref} {...rest} {...textProps}>
{children}
</BaseComponent>
</TextDirectionContext.Provider>
);
});
return (
<TextDirectionContext.Provider value={value}>
<BaseComponent ref={ref} {...rest} {...textProps}>
{children}
</BaseComponent>
</TextDirectionContext.Provider>
);
}
);

Text.propTypes = {
(Text as React.FC).propTypes = {
/**
* Provide a custom element type used to render the outermost node
*/
Expand Down
22 changes: 22 additions & 0 deletions packages/react/src/internal/PolymorphicProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type AsProp<C extends React.ElementType> = {
as?: C;
};

type PropsToOmit<C extends React.ElementType, P> = keyof (AsProp<C> & P);

// This can be used if there is NO need for "ref"
export type PolymorphicComponentProp<
C extends React.ElementType,
Props = {},
> = React.PropsWithChildren<Props & AsProp<C>> &
Omit<React.ComponentPropsWithoutRef<C>, PropsToOmit<C, Props>>;

// This is the type for the "ref" only
export type PolymorphicRef<C extends React.ElementType> =
React.ComponentPropsWithRef<C>['ref'];

// This is a new type utitlity with ref!
export type PolymorphicComponentPropWithRef<
C extends React.ElementType,
Props = {},
> = PolymorphicComponentProp<C, Props> & { ref?: PolymorphicRef<C> };
Loading