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

Commit 1252d4f

Browse files
committed
feat: support withComponent on Input, Button
1 parent 6de6735 commit 1252d4f

File tree

5 files changed

+39
-43
lines changed

5 files changed

+39
-43
lines changed

β€Žsrc/Button.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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'
67
import * as defaultTheme from './style/defaultTheme'
78
import { th, mixin } from './utils'
89

@@ -17,8 +18,15 @@ const variants = [
1718
'dark',
1819
]
1920

20-
const ButtonComponent = ({ className, size, theme, variant, ...props }) => (
21-
<button
21+
const ButtonComponent = ({
22+
className,
23+
component: Component = 'button',
24+
size,
25+
theme,
26+
variant,
27+
...props
28+
}) => (
29+
<Component
2230
{...props}
2331
className={classNames(
2432
'sui-button',
@@ -80,5 +88,7 @@ Button.defaultProps = {
8088
theme: defaultTheme,
8189
}
8290

91+
Button.withComponent = getWithComponent(Button, ButtonComponent)
92+
8393
/** @component */
8494
export default Button

β€Žsrc/Input.jsβ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ 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'
67
import * as defaultTheme from './style/defaultTheme'
78
import { th, mixin } from './utils'
89

910
const InputComponent = ({
11+
component: Component = 'input',
1012
className,
1113
control,
1214
size,
1315
theme,
1416
valid,
1517
...props
1618
}) => (
17-
<input
19+
<Component
1820
{...props}
1921
className={classNames(
2022
'sui-input',
@@ -111,5 +113,7 @@ Input.defaultProps = {
111113
theme: defaultTheme,
112114
}
113115

116+
Input.withComponent = getWithComponent(Input, InputComponent)
117+
114118
/** @component */
115119
export default Input

β€Žsrc/Textarea.jsβ€Ž

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,5 @@
1-
import React from 'react'
2-
import PropTypes from 'prop-types'
3-
import classNames from 'classnames'
4-
import * as defaultTheme from './style/defaultTheme'
51
import Input from './Input'
62

7-
const TextareaComponent = ({
8-
className,
9-
control,
10-
size,
11-
theme,
12-
valid,
13-
...props
14-
}) => (
15-
<textarea
16-
{...props}
17-
className={classNames(
18-
'sui-input',
19-
{
20-
'sui-control': control,
21-
'sui-is-valid': valid === true,
22-
'sui-is-invalid': valid === false,
23-
[`sui-input-${size}`]: size,
24-
},
25-
className,
26-
)}
27-
/>
28-
)
3+
const Textarea = Input.withComponent('textarea')
294

30-
const Textarea = Input.withComponent(TextareaComponent)
31-
32-
Input.propTypes = {
33-
control: PropTypes.bool,
34-
size: PropTypes.oneOf(['sm', 'lg']),
35-
theme: PropTypes.object,
36-
valid: PropTypes.bool,
37-
}
38-
39-
Input.defaultProps = {
40-
theme: defaultTheme,
41-
}
42-
43-
/** @component */
445
export default Textarea

β€Žsrc/Textarea.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
See [`Input`](#input).
2+
13
### Sizes
24

35
Set heights using `size` prop like `"sm"` or `"lg"`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
3+
const getWithComponent = (StyledComponent, BaseComponent) => {
4+
const originalWithComponent = StyledComponent.withComponent.bind(
5+
StyledComponent,
6+
)
7+
8+
const withComponent = component => {
9+
const NewStyledComponent = originalWithComponent(props => (
10+
<BaseComponent component={component} {...props} />
11+
))
12+
NewStyledComponent.withComponent = withComponent
13+
return NewStyledComponent
14+
}
15+
16+
return withComponent
17+
}
18+
19+
export default getWithComponent

0 commit comments

Comments
Β (0)