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

Commit

Permalink
fix(sc): fix withComponent usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Oct 19, 2018
1 parent 27acc39 commit 3f882c5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/shared/core/styled-engine/emotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ function patchStyledComponent(StyledComponent) {
StyledComponent.withComponent = (component, options) => {
// eslint-disable-next-line no-underscore-dangle
const BaseComponent = StyledComponent.__emotion_base
return Object.assign(
patchStyledComponent(
return patchStyledComponent(
Object.assign(
withComponent(
props => <BaseComponent as={component} {...props} />,
options,
),
StyledComponent,
),
StyledComponent,
)
}

Expand Down
37 changes: 31 additions & 6 deletions packages/shared/core/styled-engine/util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
function patchStyledComponent(StyledComponent, defaultAs) {
const { render } = StyledComponent
StyledComponent.withComponent = component =>
patchStyledComponent({ ...StyledComponent }, component)
StyledComponent.render = ({ as = defaultAs, ...props }, ref) =>
/* eslint-disable no-underscore-dangle */
import React from 'react'

function patchStyledComponent(StyledComponent) {
if (!StyledComponent.target.__smoothUIComponent) {
return StyledComponent
}
const {
render,
withComponent,
defaultProps,
propTypes,
componentStyle,
} = StyledComponent
StyledComponent.withComponent = (component, ...args) => {
const Target = StyledComponent.target
const NewTarget = props => <Target as={component} {...props} />
// eslint-disable-next-line no-underscore-dangle
NewTarget.__smoothUIComponent = true
return patchStyledComponent(
Object.assign(withComponent(NewTarget, ...args), {
defaultProps,
propTypes,
componentStyle,
}),
)
}
StyledComponent.render = ({ as, ...props }, ref) =>
render({ forwardedAs: as, ...props }, ref)
return StyledComponent
}
Expand All @@ -21,5 +44,7 @@ function wrapCreateStyledComponent(createStyledComponent) {
}

export function wrapStyled(styled) {
return component => wrapCreateStyledComponent(styled(component))
const newStyled = component => wrapCreateStyledComponent(styled(component))
Object.assign(newStyled, styled)
return newStyled
}
3 changes: 3 additions & 0 deletions packages/shared/core/utils/createComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ function createComponent(getConfig) {
const RefComponent = React.forwardRef(forwardRef)
RefComponent.displayName = InnerComponent.displayName

// eslint-disable-next-line no-underscore-dangle
RefComponent.__smoothUIComponent = true

const StyledComponent = styled(RefComponent)`
${style};
${applySystem && applySystem(system)};
Expand Down

0 comments on commit 3f882c5

Please sign in to comment.