Add disabled attribute in Checkbox, Toggle and RadioButton

Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
Ajay Bura 2021-12-31 17:38:25 +05:30
parent 387f6bcad4
commit a0399b7f5e
6 changed files with 25 additions and 5 deletions

View file

@ -2,7 +2,9 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import './Checkbox.scss'; import './Checkbox.scss';
function Checkbox({ variant, isActive, onToggle }) { function Checkbox({
variant, isActive, onToggle, disabled,
}) {
const className = `checkbox checkbox-${variant}${isActive ? ' checkbox--active' : ''}`; const className = `checkbox checkbox-${variant}${isActive ? ' checkbox--active' : ''}`;
if (onToggle === null) return <span className={className} />; if (onToggle === null) return <span className={className} />;
return ( return (
@ -11,6 +13,7 @@ function Checkbox({ variant, isActive, onToggle }) {
onClick={() => onToggle(!isActive)} onClick={() => onToggle(!isActive)}
className={className} className={className}
type="button" type="button"
disabled={disabled}
/> />
); );
} }
@ -19,12 +22,14 @@ Checkbox.defaultProps = {
variant: 'primary', variant: 'primary',
isActive: false, isActive: false,
onToggle: null, onToggle: null,
disabled: false,
}; };
Checkbox.propTypes = { Checkbox.propTypes = {
variant: PropTypes.oneOf(['primary', 'positive', 'caution', 'danger']), variant: PropTypes.oneOf(['primary', 'positive', 'caution', 'danger']),
isActive: PropTypes.bool, isActive: PropTypes.bool,
onToggle: PropTypes.func, onToggle: PropTypes.func,
disabled: PropTypes.bool,
}; };
export default Checkbox; export default Checkbox;

View file

@ -1,4 +1,5 @@
@use '../../partials/flex'; @use '../../partials/flex';
@use './state';
.checkbox { .checkbox {
width: 20px; width: 20px;
@ -8,6 +9,7 @@
background-color: var(--bg-surface-border); background-color: var(--bg-surface-border);
box-shadow: var(--bs-surface-border); box-shadow: var(--bs-surface-border);
cursor: pointer; cursor: pointer;
@include state.disabled;
@extend .cp-fx__row--c-c; @extend .cp-fx__row--c-c;
&--active { &--active {

View file

@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import './RadioButton.scss'; import './RadioButton.scss';
function RadioButton({ isActive, onToggle }) { function RadioButton({ isActive, onToggle, disabled }) {
if (onToggle === null) return <span className={`radio-btn${isActive ? ' radio-btn--active' : ''}`} />; if (onToggle === null) return <span className={`radio-btn${isActive ? ' radio-btn--active' : ''}`} />;
return ( return (
// eslint-disable-next-line jsx-a11y/control-has-associated-label // eslint-disable-next-line jsx-a11y/control-has-associated-label
@ -10,6 +10,7 @@ function RadioButton({ isActive, onToggle }) {
onClick={() => onToggle(!isActive)} onClick={() => onToggle(!isActive)}
className={`radio-btn${isActive ? ' radio-btn--active' : ''}`} className={`radio-btn${isActive ? ' radio-btn--active' : ''}`}
type="button" type="button"
disabled={disabled}
/> />
); );
} }
@ -17,11 +18,13 @@ function RadioButton({ isActive, onToggle }) {
RadioButton.defaultProps = { RadioButton.defaultProps = {
isActive: false, isActive: false,
onToggle: null, onToggle: null,
disabled: false,
}; };
RadioButton.propTypes = { RadioButton.propTypes = {
isActive: PropTypes.bool, isActive: PropTypes.bool,
onToggle: PropTypes.func, onToggle: PropTypes.func,
disabled: PropTypes.bool,
}; };
export default RadioButton; export default RadioButton;

View file

@ -1,4 +1,5 @@
@use '../../partials/flex'; @use '../../partials/flex';
@use './state';
.radio-btn { .radio-btn {
@extend .cp-fx__row--c-c; @extend .cp-fx__row--c-c;
@ -8,6 +9,7 @@
background-color: var(--bg-surface-border); background-color: var(--bg-surface-border);
border: 2px solid var(--bg-surface-border); border: 2px solid var(--bg-surface-border);
cursor: pointer; cursor: pointer;
@include state.disabled;
&::before { &::before {
content: ''; content: '';

View file

@ -2,24 +2,30 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import './Toggle.scss'; import './Toggle.scss';
function Toggle({ isActive, onToggle }) { function Toggle({ isActive, onToggle, disabled }) {
const className = `toggle${isActive ? ' toggle--active' : ''}`;
if (onToggle === null) return <span className={className} />;
return ( return (
// eslint-disable-next-line jsx-a11y/control-has-associated-label // eslint-disable-next-line jsx-a11y/control-has-associated-label
<button <button
onClick={() => onToggle(!isActive)} onClick={() => onToggle(!isActive)}
className={`toggle${isActive ? ' toggle--active' : ''}`} className={className}
type="button" type="button"
disabled={disabled}
/> />
); );
} }
Toggle.defaultProps = { Toggle.defaultProps = {
isActive: false, isActive: false,
disabled: false,
onToggle: null,
}; };
Toggle.propTypes = { Toggle.propTypes = {
isActive: PropTypes.bool, isActive: PropTypes.bool,
onToggle: PropTypes.func.isRequired, onToggle: PropTypes.func,
disabled: PropTypes.bool,
}; };
export default Toggle; export default Toggle;

View file

@ -1,4 +1,5 @@
@use '../../partials/dir'; @use '../../partials/dir';
@use './state';
.toggle { .toggle {
width: 44px; width: 44px;
@ -10,6 +11,7 @@
box-shadow: var(--bs-surface-border); box-shadow: var(--bs-surface-border);
cursor: pointer; cursor: pointer;
background-color: var(--bg-surface-low); background-color: var(--bg-surface-low);
@include state.disabled;
transition: background 200ms ease-in-out; transition: background 200ms ease-in-out;