Skip to content

Commit e1738ed

Browse files
maartenthn1k0
authored andcommitted
Improve checkbox and radio button styles (rjsf-team#403)
1 parent 8bceb45 commit e1738ed

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

src/components/widgets/CheckboxesWidget.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,33 @@ function CheckboxesWidget(props) {
2020
<div className="checkboxes" id={id}>{
2121
enumOptions.map((option, index) => {
2222
const checked = value.indexOf(option.value) !== -1;
23-
return (
24-
<div key={index} className={`checkbox${inline ? "-inline" : ""}`}>
23+
const disabledCls = disabled ? "disabled" : "";
24+
const checkbox = (
25+
<span>
26+
<input type="checkbox"
27+
id={`${id}_${index}`}
28+
checked={checked}
29+
disabled={disabled}
30+
autoFocus={autofocus && index === 0}
31+
onChange={(event) => {
32+
const all = enumOptions.map(({value}) => value);
33+
if (event.target.checked) {
34+
onChange(selectValue(option.value, value, all));
35+
} else {
36+
onChange(deselectValue(option.value, value));
37+
}
38+
}}/>
39+
{option.label}
40+
</span>
41+
);
42+
return inline ? (
43+
<label key={index} className={`checkbox-inline ${disabledCls}`}>
44+
{checkbox}
45+
</label>
46+
) : (
47+
<div key={index} className={`checkbox ${disabledCls}`}>
2548
<label>
26-
<input type="checkbox"
27-
id={`${id}_${index}`}
28-
checked={checked}
29-
disabled={disabled}
30-
autoFocus={autofocus && index === 0}
31-
onChange={(event) => {
32-
const all = enumOptions.map(({value}) => value);
33-
if (event.target.checked) {
34-
onChange(selectValue(option.value, value, all));
35-
} else {
36-
onChange(deselectValue(option.value, value));
37-
}
38-
}}/>
39-
<strong>{option.label}</strong>
49+
{checkbox}
4050
</label>
4151
</div>
4252
);

src/components/widgets/RadioWidget.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,28 @@ function RadioWidget({
1717
<div className="field-radio-group">{
1818
enumOptions.map((option, i) => {
1919
const checked = option.value === value;
20-
return (
21-
<div key={i} className={`radio${inline ? "-inline" : ""} ${disabled ? "disabled" : ""}`}>
20+
const disabledCls = disabled ? "disabled" : "";
21+
const radio = (
22+
<span>
23+
<input type="radio"
24+
name={name}
25+
value={option.value}
26+
checked={checked}
27+
disabled={disabled}
28+
autoFocus={autofocus && i === 0}
29+
onChange={_ => onChange(option.value)}/>
30+
{option.label}
31+
</span>
32+
);
33+
34+
return inline ? (
35+
<label key={i} className={`radio-inline ${disabledCls}`}>
36+
{radio}
37+
</label>
38+
) : (
39+
<div key={i} className={`radio ${disabledCls}`}>
2240
<label>
23-
<input type="radio"
24-
name={name}
25-
value={option.value}
26-
checked={checked}
27-
disabled={disabled}
28-
autoFocus={autofocus && i === 0}
29-
onChange={_ => onChange(option.value)}/>
30-
{option.label}
41+
{radio}
3142
</label>
3243
</div>
3344
);

0 commit comments

Comments
 (0)