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

Commit 9fcdb26

Browse files
committed
fix: fix withComponent
1 parent 629ac11 commit 9fcdb26

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

src/Button.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44
import styled, { css } from 'styled-components'
55
import handleRef from './internal/handleRef'
6-
import getWithComponent from './internal/getWithComponent'
6+
import setWithComponent from './internal/setWithComponent'
77
import * as defaultTheme from './style/defaultTheme'
88
import { th, mixin } from './utils'
99

@@ -88,7 +88,7 @@ Button.defaultProps = {
8888
theme: defaultTheme,
8989
}
9090

91-
Button.withComponent = getWithComponent(Button, ButtonComponent)
91+
setWithComponent(Button, ButtonComponent)
9292

9393
/** @component */
9494
export default Button

src/Button.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,20 @@ Disable using `disabled` prop.
5656
```js
5757
<Button disabled>Disabled</Button>
5858
```
59+
60+
### Extend and use with other components
61+
62+
```js
63+
const BorderedButton = Button.extend`
64+
border: 2px solid black;
65+
`
66+
const BorderedLinkButton = BorderedButton.withComponent('a')
67+
;<div>
68+
<span style={{ margin: '5px' }}>
69+
<BorderedLinkButton variant="primary">Primary</BorderedLinkButton>
70+
</span>
71+
<span style={{ margin: '5px' }}>
72+
<BorderedLinkButton variant="secondary">Secondary</BorderedLinkButton>
73+
</span>
74+
</div>
75+
```

src/Input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import classNames from 'classnames'
33
import PropTypes from 'prop-types'
44
import styled from 'styled-components'
55
import handleRef from './internal/handleRef'
6-
import getWithComponent from './internal/getWithComponent'
6+
import setWithComponent from './internal/setWithComponent'
77
import * as defaultTheme from './style/defaultTheme'
88
import { th, mixin } from './utils'
99

@@ -113,7 +113,7 @@ Input.defaultProps = {
113113
theme: defaultTheme,
114114
}
115115

116-
Input.withComponent = getWithComponent(Input, InputComponent)
116+
setWithComponent(Input, InputComponent)
117117

118118
/** @component */
119119
export default Input

src/internal/getWithComponent.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/internal/setWithComponent.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable no-param-reassign */
2+
import React from 'react'
3+
4+
const setWithComponent = (StyledComponent, BaseComponent) => {
5+
const originalExtend = StyledComponent.extend.bind(this)
6+
Object.defineProperty(StyledComponent, 'extend', {
7+
get() {
8+
return (...args) => {
9+
const NewStyledComponent = originalExtend(...args)
10+
setWithComponent(NewStyledComponent, BaseComponent)
11+
return NewStyledComponent
12+
}
13+
},
14+
})
15+
16+
const originalWithComponent = StyledComponent.withComponent.bind(
17+
StyledComponent,
18+
)
19+
StyledComponent.withComponent = component => {
20+
const NewStyledComponent = originalWithComponent(props => (
21+
<BaseComponent component={component} {...props} />
22+
))
23+
24+
setWithComponent(NewStyledComponent, BaseComponent)
25+
return NewStyledComponent
26+
}
27+
}
28+
29+
export default setWithComponent

0 commit comments

Comments
 (0)