Skip to content

Commit e399de0

Browse files
committed
refactor(CFormCheck): split CFormCheck to CFormCheck and CFormSwitch
1 parent 9303781 commit e399de0

File tree

2 files changed

+114
-60
lines changed

2 files changed

+114
-60
lines changed

src/components/form/CFormCheck.tsx

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,40 @@ import React, { forwardRef, HTMLAttributes, ReactNode } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

5-
import { Colors, Shapes, colorPropType, shapePropType } from '../Types'
5+
import { Colors, Shapes } from '../Types'
66

7-
import { CFormControl } from './CFormControl'
87
import { CFormLabel } from './CFormLabel'
98

10-
export interface CFormCheckProps extends HTMLAttributes<HTMLInputElement> {
11-
button?: boolean
9+
export type ButtonObject = {
1210
/**
1311
* Sets the color context of the component to one of CoreUI’s themed colors. [docs]
1412
*
1513
* @type 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string
1614
*/
17-
buttonColor?: Colors
15+
color?: Colors
1816
/**
1917
* Select the shape of the component. [docs]
2018
*
2119
* @type 'rounded' | 'rounded-top' | 'rounded-end' | 'rounded-bottom' | 'rounded-start' | 'rounded-circle' | 'rounded-pill' | 'rounded-0' | 'rounded-1' | 'rounded-2' | 'rounded-3' | string
2220
*/
23-
buttonShape?: Shapes
21+
shape?: Shapes
2422
/**
2523
* Size the component small or large. [docs]
2624
*
2725
* @type 'sm' | 'lg'
2826
*/
29-
buttonSize?: 'sm' | 'lg'
27+
size?: 'sm' | 'lg'
3028
/**
3129
* Set the button variant to an outlined button or a ghost button. [docs]
3230
*/
33-
buttonVariant?: 'outline' | 'ghost'
31+
variant?: 'outline' | 'ghost'
32+
}
33+
34+
export interface CFormCheckProps extends HTMLAttributes<HTMLInputElement> {
35+
/**
36+
* Create button-like checkboxes and radio buttons
37+
*/
38+
button?: ButtonObject
3439
/**
3540
* A string of all className you want applied to the component. [docs]
3641
*/
@@ -51,16 +56,6 @@ export interface CFormCheckProps extends HTMLAttributes<HTMLInputElement> {
5156
* The element represents a caption for a component. [docs]
5257
*/
5358
label?: string | ReactNode
54-
/**
55-
* Size the component large or extra large. Works only with `switch` [docs]
56-
*
57-
* @type 'lg' | 'xl'
58-
*/
59-
size?: 'lg' | 'xl'
60-
/**
61-
* Render component as a toggle switch. [docs]
62-
*/
63-
switch?: boolean
6459
/**
6560
* Specifies the type of component. [docs]
6661
*
@@ -75,31 +70,10 @@ export interface CFormCheckProps extends HTMLAttributes<HTMLInputElement> {
7570
}
7671

7772
export const CFormCheck = forwardRef<HTMLInputElement, CFormCheckProps>(
78-
(
79-
{
80-
className,
81-
button,
82-
buttonColor = 'primary',
83-
buttonSize,
84-
buttonShape,
85-
buttonVariant,
86-
id,
87-
inline,
88-
invalid,
89-
label,
90-
size,
91-
switch: _switch,
92-
type = 'checkbox',
93-
valid,
94-
...rest
95-
},
96-
ref,
97-
) => {
73+
({ className, button, id, inline, invalid, label, type = 'checkbox', valid, ...rest }, ref) => {
9874
const _className = classNames(
9975
'form-check',
10076
{
101-
'form-switch': _switch,
102-
[`form-switch-${size}`]: size,
10377
'form-check-inline': inline,
10478
'is-invalid': invalid,
10579
'is-valid': valid,
@@ -115,35 +89,28 @@ export const CFormCheck = forwardRef<HTMLInputElement, CFormCheckProps>(
11589
button
11690
? classNames(
11791
'btn',
118-
buttonVariant ? `btn-${buttonVariant}-${buttonColor}` : `btn-${buttonColor}`,
92+
button.variant ? `btn-${button.variant}-${button.color}` : `btn-${button.color}`,
11993
{
120-
[`btn-${buttonSize}`]: buttonSize,
121-
buttonShape,
94+
[`btn-${button.size}`]: button.size,
12295
},
96+
`${button.shape}`,
12397
)
12498
: 'form-check-label',
12599
)
126100

127101
const formControl = () => {
128-
return (
129-
<CFormControl type={type} classNameParent={inputClassName} id={id} {...rest} ref={ref} />
130-
)
102+
return <input type={type} className={inputClassName} id={id} {...rest} ref={ref} />
131103
}
132104

133105
const formLabel = () => {
134106
return (
135-
<CFormLabel classNameParent={labelClassName} {...(id && { htmlFor: id })}>
107+
<CFormLabel customClassName={labelClassName} {...(id && { htmlFor: id })}>
136108
{label}
137109
</CFormLabel>
138110
)
139111
}
140112

141-
return _switch ? (
142-
<div className={_className}>
143-
{formControl()}
144-
{label && formLabel()}
145-
</div>
146-
) : button ? (
113+
return button ? (
147114
<>
148115
{formControl()}
149116
{label && formLabel()}
@@ -160,18 +127,12 @@ export const CFormCheck = forwardRef<HTMLInputElement, CFormCheckProps>(
160127
)
161128

162129
CFormCheck.propTypes = {
163-
button: PropTypes.bool,
164-
buttonColor: colorPropType,
165-
buttonShape: shapePropType,
166-
buttonSize: PropTypes.oneOf(['sm', 'lg']),
167-
buttonVariant: PropTypes.oneOf(['outline', 'ghost']),
130+
button: PropTypes.object,
168131
className: PropTypes.string,
169132
id: PropTypes.string,
170133
inline: PropTypes.bool,
171134
invalid: PropTypes.bool,
172135
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
173-
size: PropTypes.oneOf(['lg', 'xl']),
174-
switch: PropTypes.bool,
175136
type: PropTypes.oneOf(['checkbox', 'radio']),
176137
valid: PropTypes.bool,
177138
}

src/components/form/CFormSwitch.tsx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React, { forwardRef, HTMLAttributes, ReactNode } from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
5+
import { CFormLabel } from './CFormLabel'
6+
7+
export interface CFormSwitchProps extends HTMLAttributes<HTMLInputElement> {
8+
/**
9+
* A string of all className you want applied to the component. [docs]
10+
*/
11+
className?: string
12+
/**
13+
* The id global attribute defines an identifier (ID) that must be unique in the whole document. [docs]
14+
*/
15+
id?: string
16+
/**
17+
* Group checkboxes or radios on the same horizontal row by adding. [docs]
18+
*/
19+
inline?: boolean
20+
/**
21+
* Set component validation state to invalid. [docs]
22+
*/
23+
invalid?: boolean
24+
/**
25+
* The element represents a caption for a component. [docs]
26+
*/
27+
label?: string | ReactNode
28+
/**
29+
* Size the component large or extra large. Works only with `switch` [docs]
30+
*
31+
* @type 'lg' | 'xl'
32+
*/
33+
size?: 'lg' | 'xl'
34+
/**
35+
* Render component as a toggle switch. [docs]
36+
*/
37+
switch?: boolean
38+
/**
39+
* Specifies the type of component. [docs]
40+
*
41+
* @type checkbox' | 'radio'
42+
* @default 'checkbox'
43+
*/
44+
type?: 'checkbox' | 'radio'
45+
/**
46+
* Set component validation state to valid. [docs]
47+
*/
48+
valid?: boolean
49+
}
50+
51+
export const CFormSwitch = forwardRef<HTMLInputElement, CFormSwitchProps>(
52+
({ className, id, invalid, label, size, type = 'checkbox', valid, ...rest }, ref) => {
53+
const _className = classNames(
54+
'form-check form-switch',
55+
{
56+
[`form-switch-${size}`]: size,
57+
'is-invalid': invalid,
58+
'is-valid': valid,
59+
},
60+
className,
61+
)
62+
63+
const inputClassName = classNames('form-check-input', {
64+
'is-invalid': invalid,
65+
'is-valid': valid,
66+
})
67+
const labelClassName = classNames('form-check-label')
68+
69+
return (
70+
<div className={_className}>
71+
<input type={type} className={inputClassName} id={id} {...rest} ref={ref} />
72+
{label && (
73+
<CFormLabel customClassName={labelClassName} {...(id && { htmlFor: id })}>
74+
{label}
75+
</CFormLabel>
76+
)}
77+
</div>
78+
)
79+
},
80+
)
81+
82+
CFormSwitch.propTypes = {
83+
className: PropTypes.string,
84+
id: PropTypes.string,
85+
inline: PropTypes.bool,
86+
invalid: PropTypes.bool,
87+
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
88+
size: PropTypes.oneOf(['lg', 'xl']),
89+
type: PropTypes.oneOf(['checkbox', 'radio']),
90+
valid: PropTypes.bool,
91+
}
92+
93+
CFormSwitch.displayName = 'CFormSwitch'

0 commit comments

Comments
 (0)