Skip to content

Commit 9643c7a

Browse files
authored
feat(layer): add polymorphic props in Layer component (#18831)
* feat(layer): add polymorphic props in Layer component * test: add test case * test: remove test case to merge
1 parent 895837d commit 9643c7a

File tree

1 file changed

+39
-44
lines changed

1 file changed

+39
-44
lines changed

packages/react/src/components/Layer/index.tsx

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import cx from 'classnames';
1111
import { usePrefix } from '../../internal/usePrefix';
1212
import { LayerContext } from './LayerContext';
1313
import { LayerLevel, MAX_LEVEL, MIN_LEVEL, levels } from './LayerLevel';
14-
import { PolymorphicProps } from '../../types/common';
14+
import {
15+
PolymorphicComponentPropWithRef,
16+
PolymorphicRef,
17+
} from '../../internal/PolymorphicProps';
1518

1619
/**
1720
* A custom hook that will return information about the current layer. A common
@@ -43,53 +46,45 @@ export interface LayerBaseProps {
4346
level?: LayerLevel;
4447
}
4548

46-
export type LayerProps<T extends React.ElementType> = PolymorphicProps<
47-
T,
48-
LayerBaseProps
49-
>;
49+
export type LayerProps<T extends React.ElementType> =
50+
PolymorphicComponentPropWithRef<T, LayerBaseProps>;
5051

51-
export interface LayerComponent {
52-
<T extends React.ElementType>(
53-
props: LayerProps<T>,
54-
context?: any
55-
): React.ReactElement<any, any> | null;
56-
}
52+
const Layer = React.forwardRef(
53+
<T extends React.ElementType = 'div'>(
54+
{
55+
as,
56+
className: customClassName,
57+
children,
58+
level: overrideLevel,
59+
...rest
60+
}: LayerProps<T>,
61+
ref?: PolymorphicRef<T>
62+
) => {
63+
const contextLevel = React.useContext(LayerContext);
64+
const level = overrideLevel ?? contextLevel;
65+
const prefix = usePrefix();
66+
const className = cx(`${prefix}--layer-${levels[level]}`, customClassName);
67+
// The level should be between MIN_LEVEL and MAX_LEVEL
68+
const value = Math.max(
69+
MIN_LEVEL,
70+
Math.min(level + 1, MAX_LEVEL)
71+
) as LayerLevel;
5772

58-
const LayerRenderFunction = React.forwardRef(function Layer<
59-
T extends React.ElementType,
60-
>(
61-
{
62-
as = 'div' as T,
63-
className: customClassName,
64-
children,
65-
level: overrideLevel,
66-
...rest
67-
}: LayerProps<T>,
68-
ref: React.Ref<unknown>
69-
) {
70-
const contextLevel = React.useContext(LayerContext);
71-
const level = overrideLevel ?? contextLevel;
72-
const prefix = usePrefix();
73-
const className = cx(`${prefix}--layer-${levels[level]}`, customClassName);
74-
// The level should be between MIN_LEVEL and MAX_LEVEL
75-
const value = Math.max(
76-
MIN_LEVEL,
77-
Math.min(level + 1, MAX_LEVEL)
78-
) as LayerLevel;
73+
const BaseComponent = as || 'div';
7974

80-
const BaseComponent = as as React.ElementType;
75+
return (
76+
<LayerContext.Provider value={value}>
77+
<BaseComponent ref={ref} {...rest} className={className}>
78+
{children}
79+
</BaseComponent>
80+
</LayerContext.Provider>
81+
);
82+
}
83+
);
8184

82-
return (
83-
<LayerContext.Provider value={value}>
84-
<BaseComponent ref={ref} {...rest} className={className}>
85-
{children}
86-
</BaseComponent>
87-
</LayerContext.Provider>
88-
);
89-
});
85+
Layer.displayName = 'Layer';
9086

91-
LayerRenderFunction.displayName = 'Layer';
92-
LayerRenderFunction.propTypes = {
87+
Layer.propTypes = {
9388
/**
9489
* Specify a custom component or element to be rendered as the top-level
9590
* element in the component
@@ -117,4 +112,4 @@ LayerRenderFunction.propTypes = {
117112
level: PropTypes.oneOf([0, 1, 2]),
118113
};
119114

120-
export const Layer = LayerRenderFunction as LayerComponent;
115+
export { Layer };

0 commit comments

Comments
 (0)