Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 3f882c5

Browse files
committed
fix(sc): fix withComponent usage
1 parent 27acc39 commit 3f882c5

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

packages/shared/core/styled-engine/emotion.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ function patchStyledComponent(StyledComponent) {
6363
StyledComponent.withComponent = (component, options) => {
6464
// eslint-disable-next-line no-underscore-dangle
6565
const BaseComponent = StyledComponent.__emotion_base
66-
return Object.assign(
67-
patchStyledComponent(
66+
return patchStyledComponent(
67+
Object.assign(
6868
withComponent(
6969
props => <BaseComponent as={component} {...props} />,
7070
options,
7171
),
72+
StyledComponent,
7273
),
73-
StyledComponent,
7474
)
7575
}
7676

packages/shared/core/styled-engine/util.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1-
function patchStyledComponent(StyledComponent, defaultAs) {
2-
const { render } = StyledComponent
3-
StyledComponent.withComponent = component =>
4-
patchStyledComponent({ ...StyledComponent }, component)
5-
StyledComponent.render = ({ as = defaultAs, ...props }, ref) =>
1+
/* eslint-disable no-underscore-dangle */
2+
import React from 'react'
3+
4+
function patchStyledComponent(StyledComponent) {
5+
if (!StyledComponent.target.__smoothUIComponent) {
6+
return StyledComponent
7+
}
8+
const {
9+
render,
10+
withComponent,
11+
defaultProps,
12+
propTypes,
13+
componentStyle,
14+
} = StyledComponent
15+
StyledComponent.withComponent = (component, ...args) => {
16+
const Target = StyledComponent.target
17+
const NewTarget = props => <Target as={component} {...props} />
18+
// eslint-disable-next-line no-underscore-dangle
19+
NewTarget.__smoothUIComponent = true
20+
return patchStyledComponent(
21+
Object.assign(withComponent(NewTarget, ...args), {
22+
defaultProps,
23+
propTypes,
24+
componentStyle,
25+
}),
26+
)
27+
}
28+
StyledComponent.render = ({ as, ...props }, ref) =>
629
render({ forwardedAs: as, ...props }, ref)
730
return StyledComponent
831
}
@@ -21,5 +44,7 @@ function wrapCreateStyledComponent(createStyledComponent) {
2144
}
2245

2346
export function wrapStyled(styled) {
24-
return component => wrapCreateStyledComponent(styled(component))
47+
const newStyled = component => wrapCreateStyledComponent(styled(component))
48+
Object.assign(newStyled, styled)
49+
return newStyled
2550
}

packages/shared/core/utils/createComponent.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ function createComponent(getConfig) {
7171
const RefComponent = React.forwardRef(forwardRef)
7272
RefComponent.displayName = InnerComponent.displayName
7373

74+
// eslint-disable-next-line no-underscore-dangle
75+
RefComponent.__smoothUIComponent = true
76+
7477
const StyledComponent = styled(RefComponent)`
7578
${style};
7679
${applySystem && applySystem(system)};

0 commit comments

Comments
 (0)