This repository was archived by the owner on Jun 20, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +50
-23
lines changed
Expand file tree Collapse file tree 5 files changed +50
-23
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
33import classNames from 'classnames'
44import styled , { css } from 'styled-components'
55import handleRef from './internal/handleRef'
6- import getWithComponent from './internal/getWithComponent '
6+ import setWithComponent from './internal/setWithComponent '
77import * as defaultTheme from './style/defaultTheme'
88import { 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 */
9494export default Button
Original file line number Diff line number Diff 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+ ```
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import classNames from 'classnames'
33import PropTypes from 'prop-types'
44import styled from 'styled-components'
55import handleRef from './internal/handleRef'
6- import getWithComponent from './internal/getWithComponent '
6+ import setWithComponent from './internal/setWithComponent '
77import * as defaultTheme from './style/defaultTheme'
88import { 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 */
119119export default Input
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments