Skip to content

Commit 531265b

Browse files
authored
Merge pull request #432 from facultyai/disabled-checkbox-radio
Add option to disable RadioButton and Checkbox
2 parents 6e982d4 + 8ce396e commit 531265b

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/components/input/Checkbox.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {omit} from 'ramda';
66
* Creates a single checkbox input. Use the `checked` prop in your callbacks.
77
*/
88
const Checkbox = props => {
9-
const {checked, loading_state, setProps, ...otherProps} = props;
9+
const {checked, loading_state, disabled, setProps, ...otherProps} = props;
1010
const [checkedState, setCheckedState] = useState(checked || false);
1111

1212
useEffect(() => {
@@ -23,12 +23,15 @@ const Checkbox = props => {
2323
['setProps', 'persistence', 'persistence_type', 'persisted_props'],
2424
otherProps
2525
)}
26+
disabled={disabled}
2627
onChange={() => {
27-
setCheckedState(!checkedState);
28-
if (setProps) {
29-
setProps({
30-
checked: !checkedState
31-
});
28+
if (!disabled) {
29+
setCheckedState(!checkedState);
30+
if (setProps) {
31+
setProps({
32+
checked: !checkedState
33+
});
34+
}
3235
}
3336
}}
3437
data-dash-is-loading={
@@ -123,7 +126,12 @@ Checkbox.propTypes = {
123126
* local: window.localStorage, data is kept after the browser quit.
124127
* session: window.sessionStorage, data is cleared once the browser quit.
125128
*/
126-
persistence_type: PropTypes.oneOf(['local', 'session', 'memory'])
129+
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
130+
131+
/**
132+
* Disabled the Checkbox
133+
*/
134+
disabled: PropTypes.bool
127135
};
128136

129137
export default Checkbox;

src/components/input/RadioButton.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, {useEffect, useState} from 'react';
21
import PropTypes from 'prop-types';
32
import {omit} from 'ramda';
3+
import React, {useEffect, useState} from 'react';
44

55
/**
66
* Creates a single radio button. Use the `checked` prop in your callbacks.
77
*/
88
const RadioButton = props => {
9-
const {checked, loading_state, setProps, ...otherProps} = props;
9+
const {checked, loading_state, disabled, setProps, ...otherProps} = props;
1010
const [checkedState, setCheckedState] = useState(checked || false);
1111

1212
useEffect(() => {
@@ -23,12 +23,15 @@ const RadioButton = props => {
2323
['setProps', 'persistence', 'persistence_type', 'persisted_props'],
2424
otherProps
2525
)}
26+
disabled={disabled}
2627
onClick={() => {
27-
setCheckedState(!checkedState);
28-
if (setProps) {
29-
setProps({
30-
checked: !checkedState
31-
});
28+
if (!disabled) {
29+
setCheckedState(!checkedState);
30+
if (setProps) {
31+
setProps({
32+
checked: !checkedState
33+
});
34+
}
3235
}
3336
}}
3437
onChange={() => {}}
@@ -124,7 +127,12 @@ RadioButton.propTypes = {
124127
* local: window.localStorage, data is kept after the browser quit.
125128
* session: window.sessionStorage, data is cleared once the browser quit.
126129
*/
127-
persistence_type: PropTypes.oneOf(['local', 'session', 'memory'])
130+
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
131+
132+
/**
133+
* Disable the RadioButton.
134+
*/
135+
disabled: PropTypes.bool
128136
};
129137

130138
export default RadioButton;

0 commit comments

Comments
 (0)