Skip to content

Commit

Permalink
fix reassign forwardProp
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Apr 30, 2024
1 parent 8735a94 commit a5f2b38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/pigment-css-react/src/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ export default function styled(tag, componentMeta = {}) {
getVariantClasses(inProps, variants),
);

if (shouldUseAs && !shouldForwardProp) {
// Reassign `shouldForwardProp` based on the incoming `as` prop
if (isHtmlTag(Component)) {
finalShouldForwardProp = isPropValid;
} else if (slot === 'Root' || slot === 'root') {
finalShouldForwardProp = rootShouldForwardProp;
} else {
finalShouldForwardProp = slotShouldForwardProp;
}
}

const newProps = {};
// eslint-disable-next-line no-restricted-syntax
for (const key in props) {
Expand Down
14 changes: 14 additions & 0 deletions packages/pigment-css-react/tests/styled/runtime-styled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ describe('props filtering', () => {
expect(container.firstChild).to.have.style('--foo', '300px');
});

it('use component forward prop if provided `as` is a component', () => {
const StyledDiv = styled('div')({
classes: ['root'],
});

function Component({ TagComponent = 'span', ...props }) {
return <TagComponent {...props} />;
}

const { container } = render(<StyledDiv as={Component} TagComponent="button" disabled />);

expect(container.firstChild).to.have.tagName('button');
});

it('should forward `as` prop', () => {
// The components below is a simplified version of the `NativeSelect` component from Material UI.

Expand Down

0 comments on commit a5f2b38

Please sign in to comment.