Skip to content

Commit 4f7e74b

Browse files
committed
fix: CSwitch component fixes:
- delete props which are not controlled HTML attributes - rename 'onChange' to 'onCheckedUpdate' - fix wrong classes - pass attributes and ref to input - fix PropTypes and default values - delete obsolete props
1 parent 5066958 commit 4f7e74b

File tree

1 file changed

+39
-128
lines changed

1 file changed

+39
-128
lines changed

src/CSwitch.js

+39-128
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,66 @@
1-
import React, {useEffect, useState, useRef} from 'react';
2-
import PropTypes from 'prop-types';
3-
import classNames from 'classnames';
1+
import React, { useEffect, useState } from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
44

55
//component - CoreUI / CSwitch
66

7-
const CSwitch = props=>{
7+
const CSwitch = props => {
88

99
let {
1010
className,
1111
//
1212
innerRef,
13-
type,
1413
size,
15-
disabled,
1614
color,
1715
labelOn,
1816
labelOff,
1917
variant,
2018
shape,
21-
22-
name,
23-
required,
24-
value,
19+
checked,
20+
onCheckedUpdate,
2521
...attributes
26-
//label,
27-
//outline,
28-
} = props;
29-
30-
const fields = useRef({firstRender: true}).current;
31-
const [checked, setChecked] = useState(props.defaultChecked || props.checked);
32-
//const [selected, setSelected] = useState([]);
33-
34-
//effect - update
35-
useEffect(() => {
36-
if (fields.firstRender) return;
37-
setChecked(props.checked);
38-
},
39-
[props.checked]);
22+
} = props
4023

41-
useEffect(() => {
42-
fields.firstRender = false;
43-
},
44-
[]);
24+
const [isChecked, setIsChecked] = useState(checked)
25+
useEffect(() => setIsChecked(checked), [checked])
4526

46-
//events
47-
const onChange = e=>{
48-
const target = e.target;
49-
setChecked(target.checked);
50-
if (props.onChange) {
51-
props.onChange(e);
52-
}
27+
const change = e => {
28+
setIsChecked(e.target.checked)
29+
onCheckedUpdate && onCheckedUpdate(e.target.checked)
5330
}
5431

5532
//render
56-
57-
delete attributes.checked;
58-
delete attributes.defaultChecked;
59-
delete attributes.onChange;
60-
61-
/*
62-
const outlineString = this.outline ? '-outline' : ''
63-
const outlinedAltString = this.outline === 'alt' ? '-alt' : ''
64-
return [
65-
'c-switch form-check-label',
66-
`c-switch${outlineString}-${this.color}${outlinedAltString}`,
67-
{
68-
[`c-switch-${this.size}`]: this.size,
69-
[`c-switch-${this.shape}`]: this.shape,
70-
'c-switch-label': this.labelOn || this.labelOff
71-
}
72-
]
73-
*/
74-
/*
75-
let outline='';
76-
switch (variant) {
77-
case '3d':
78-
79-
break;
80-
case 'opposite':
81-
variant="alt";
82-
break;
83-
case '3d-opposite':
84-
85-
break;
86-
}
87-
*/
88-
89-
//console.log(variant);
90-
91-
const variant2 = ['opposite', 'outline'].indexOf(variant)>-1 ? `-${variant}` : ''
92-
let classes = classNames(
93-
className,
33+
const classes = classNames(
9434
'c-switch form-check-label',
95-
labelOn||labelOff ? 'c-switch-label' : false,
96-
size ? `c-switch-${size}` : '',
97-
shape ? `c-switch-${shape}` : '',
98-
color ? `c-switch${variant2}-${color}` : '',
99-
variant=='3d' ? 'c-switch-3d' : ''
100-
);
35+
(labelOn || labelOff) && 'c-switch-label',
36+
size && `c-switch-${size}`,
37+
shape && `c-switch-${shape}`,
38+
color && `c-switch${variant ? `-${variant}` : ''}-${color}`,
39+
className
40+
)
10141

10242
const inputClasses = classNames(
10343
'c-switch-input',
104-
'c-form-check-input',
105-
);
106-
107-
const sliderClasses = classNames(
108-
'c-switch-slider'
109-
);
110-
111-
//classes = "c-switch form-check-label c-switch-label c-switch-opposite c-switch-primary c-switch-lg"
44+
'c-form-check-input'
45+
)
11246

11347
return (
114-
<label {...attributes} className={classes} ref={innerRef}>
115-
<input className={inputClasses} type={type} onChange={onChange} checked={checked} name={name} required={required} disabled={disabled} value={value} />
116-
<span className={sliderClasses} data-checked={labelOn} data-unchecked={labelOff}></span>
48+
<label className={classes}>
49+
<input
50+
className={inputClasses}
51+
type="checkbox"
52+
onChange={change}
53+
checked={isChecked}
54+
{...attributes}
55+
ref={innerRef}
56+
/>
57+
<span
58+
className="c-switch-slider"
59+
data-checked={labelOn}
60+
data-unchecked={labelOff}
61+
></span>
11762
</label>
118-
);
119-
63+
)
12064
}
12165

12266
CSwitch.propTypes = {
@@ -126,45 +70,12 @@ CSwitch.propTypes = {
12670
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
12771
size: PropTypes.oneOf(['', 'lg', 'sm']),
12872
shape: PropTypes.oneOf(['', 'pill', 'square']),
129-
variant: PropTypes.oneOf(['', '3d', '3d-opposite', 'opposite', 'outline']),
73+
variant: PropTypes.oneOf(['', '3d', 'opposite', 'outline']),
13074
color: PropTypes.string,
13175
checked: PropTypes.bool,
13276
labelOn: PropTypes.string,
13377
labelOff: PropTypes.string,
134-
type: PropTypes.oneOf(['checkbox', 'radio']),
135-
136-
value: PropTypes.string,
137-
defaultChecked: PropTypes.bool,
138-
name: PropTypes.string,
139-
disabled: PropTypes.bool,
140-
form: PropTypes.any,//?
141-
required: PropTypes.bool,
142-
onChange: PropTypes.func
143-
144-
//label: PropTypes.bool,
145-
/*
146-
outline: PropTypes.oneOfType([
147-
PropTypes.bool,
148-
PropTypes.string,
149-
PropTypes.oneOf(['', 'alt'])
150-
]),
151-
*/
152-
153-
};
154-
155-
CSwitch.defaultProps = {
156-
size: '',
157-
checked: false,
158-
type: 'checkbox',
159-
variant: '',
160-
161-
defaultChecked: false,
162-
disabled: false,
163-
required: false,
164-
//labelOn: 'On',
165-
//labelOff: 'Off',
166-
//label: false,
167-
//outline: false,
78+
onCheckedUpdate: PropTypes.func
16879
};
16980

170-
export default CSwitch;
81+
export default CSwitch

0 commit comments

Comments
 (0)