Skip to content

Commit

Permalink
fix: fix withComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Feb 20, 2018
1 parent 629ac11 commit 9fcdb26
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import classNames from 'classnames'
import styled, { css } from 'styled-components'
import handleRef from './internal/handleRef'
import getWithComponent from './internal/getWithComponent'
import setWithComponent from './internal/setWithComponent'
import * as defaultTheme from './style/defaultTheme'
import { th, mixin } from './utils'

Expand Down Expand Up @@ -88,7 +88,7 @@ Button.defaultProps = {
theme: defaultTheme,
}

Button.withComponent = getWithComponent(Button, ButtonComponent)
setWithComponent(Button, ButtonComponent)

/** @component */
export default Button
17 changes: 17 additions & 0 deletions src/Button.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,20 @@ Disable using `disabled` prop.
```js
<Button disabled>Disabled</Button>
```

### Extend and use with other components

```js
const BorderedButton = Button.extend`
border: 2px solid black;
`
const BorderedLinkButton = BorderedButton.withComponent('a')
;<div>
<span style={{ margin: '5px' }}>
<BorderedLinkButton variant="primary">Primary</BorderedLinkButton>
</span>
<span style={{ margin: '5px' }}>
<BorderedLinkButton variant="secondary">Secondary</BorderedLinkButton>
</span>
</div>
```
4 changes: 2 additions & 2 deletions src/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import handleRef from './internal/handleRef'
import getWithComponent from './internal/getWithComponent'
import setWithComponent from './internal/setWithComponent'
import * as defaultTheme from './style/defaultTheme'
import { th, mixin } from './utils'

Expand Down Expand Up @@ -113,7 +113,7 @@ Input.defaultProps = {
theme: defaultTheme,
}

Input.withComponent = getWithComponent(Input, InputComponent)
setWithComponent(Input, InputComponent)

/** @component */
export default Input
19 changes: 0 additions & 19 deletions src/internal/getWithComponent.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/internal/setWithComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable no-param-reassign */
import React from 'react'

const setWithComponent = (StyledComponent, BaseComponent) => {
const originalExtend = StyledComponent.extend.bind(this)
Object.defineProperty(StyledComponent, 'extend', {
get() {
return (...args) => {
const NewStyledComponent = originalExtend(...args)
setWithComponent(NewStyledComponent, BaseComponent)
return NewStyledComponent
}
},
})

const originalWithComponent = StyledComponent.withComponent.bind(
StyledComponent,
)
StyledComponent.withComponent = component => {
const NewStyledComponent = originalWithComponent(props => (
<BaseComponent component={component} {...props} />
))

setWithComponent(NewStyledComponent, BaseComponent)
return NewStyledComponent
}
}

export default setWithComponent

0 comments on commit 9fcdb26

Please sign in to comment.