Skip to content

Commit b33dfa0

Browse files
committed
fix(forms): allow readOnly on FormInput and FormTextarea
1 parent dcf570e commit b33dfa0

7 files changed

+134
-29
lines changed

src/forms/FormAutocomplete.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ FormAutocomplete.defaultProps = {
186186
};
187187

188188
FormAutocomplete.propTypes = {
189+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
189190
filter: PropTypes.func,
190191
id: PropTypes.string,
191192
name: PropTypes.string.isRequired,
@@ -199,5 +200,4 @@ FormAutocomplete.propTypes = {
199200
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
200201
template: PropTypes.func,
201202
type: PropTypes.string,
202-
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
203203
};

src/forms/FormGroup.jsx

+84-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { FormSwitch } from './FormSwitch';
1010
import { FormTextarea } from './FormTextarea';
1111
import { FormValidationFeedback } from './FormValidationFeedback';
1212

13-
function FormGroup({ children, name, feedback, mockInvalidSibling, ...props }) {
13+
export function FormGroup({ children, name, feedback, mockInvalidSibling, ...props }) {
1414
return (
1515
<div className="form-group">
1616
<FormLabel {...props} />
@@ -40,6 +40,23 @@ export function FormGroupAutocomplete(props) {
4040
);
4141
}
4242

43+
FormGroupAutocomplete.propTypes = {
44+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
45+
filter: PropTypes.func,
46+
id: PropTypes.string,
47+
name: PropTypes.string.isRequired,
48+
onSearch: PropTypes.func,
49+
openOnFocus: PropTypes.bool,
50+
options: PropTypes.oneOfType([
51+
PropTypes.func,
52+
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
53+
]),
54+
placeholder: PropTypes.string,
55+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
56+
template: PropTypes.func,
57+
type: PropTypes.string,
58+
};
59+
4360
export function FormGroupCheckbox(props) {
4461
return (
4562
<FormGroup mockInvalidSibling={true} {...props}>
@@ -48,6 +65,15 @@ export function FormGroupCheckbox(props) {
4865
);
4966
}
5067

68+
FormGroupCheckbox.propTypes = {
69+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
70+
falseLabel: PropTypes.string,
71+
id: PropTypes.string.isRequired,
72+
name: PropTypes.string.isRequired,
73+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
74+
trueLabel: PropTypes.string,
75+
};
76+
5177
export function FormGroupInput(props) {
5278
return (
5379
<FormGroup {...props}>
@@ -56,6 +82,22 @@ export function FormGroupInput(props) {
5682
);
5783
}
5884

85+
FormGroupInput.propTypes = {
86+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
87+
id: PropTypes.string,
88+
max: PropTypes.string,
89+
maxLength: PropTypes.string,
90+
min: PropTypes.string,
91+
minLength: PropTypes.string,
92+
name: PropTypes.string.isRequired,
93+
pattern: PropTypes.string,
94+
placeholder: PropTypes.string,
95+
readOnly: PropTypes.bool,
96+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
97+
step: PropTypes.string,
98+
type: PropTypes.string,
99+
};
100+
59101
export function FormGroupRadio({ options, id, ...props }) {
60102
return (
61103
<FormGroup mockInvalidSibling={true} {...props}>
@@ -79,9 +121,12 @@ FormGroupRadio.defaultProps = {
79121
};
80122

81123
FormGroupRadio.propTypes = {
124+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
82125
id: PropTypes.string,
83-
options: PropTypes.arrayOf(PropTypes.object),
84126
inline: PropTypes.bool,
127+
name: PropTypes.string.isRequired,
128+
options: PropTypes.arrayOf(PropTypes.shape({ value: PropTypes.any.isRequired, label: PropTypes.string.isRequired })),
129+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
85130
};
86131

87132
export function FormGroupSelect(props) {
@@ -92,6 +137,24 @@ export function FormGroupSelect(props) {
92137
);
93138
}
94139

140+
FormGroupSelect.propTypes = {
141+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
142+
id: PropTypes.string,
143+
name: PropTypes.string.isRequired,
144+
options: PropTypes.oneOfType([
145+
PropTypes.func,
146+
PropTypes.arrayOf(
147+
PropTypes.oneOfType([
148+
PropTypes.string,
149+
PropTypes.shape({ value: PropTypes.any.isRequired, label: PropTypes.string.isRequired }),
150+
])
151+
),
152+
]),
153+
placeholder: PropTypes.string,
154+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
155+
trackBy: PropTypes.string,
156+
};
157+
95158
export function FormGroupSwitch(props) {
96159
return (
97160
<FormGroup mockInvalidSibling={true} {...props}>
@@ -100,10 +163,29 @@ export function FormGroupSwitch(props) {
100163
);
101164
}
102165

166+
FormGroupSwitch.propTypes = {
167+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
168+
falseLabel: PropTypes.string,
169+
id: PropTypes.string.isRequired,
170+
name: PropTypes.string.isRequired,
171+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
172+
trueLabel: PropTypes.string,
173+
};
174+
103175
export function FormGroupTextarea(props) {
104176
return (
105177
<FormGroup {...props}>
106178
<FormTextarea {...props} />
107179
</FormGroup>
108180
);
109181
}
182+
183+
FormGroupTextarea.propTypes = {
184+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
185+
id: PropTypes.string,
186+
name: PropTypes.string.isRequired,
187+
placeholder: PropTypes.string,
188+
readOnly: PropTypes.bool,
189+
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
190+
rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
191+
};

src/forms/FormInput.jsx

+10-16
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,19 @@ import PropTypes from 'prop-types';
44
import { useFormControl } from './helpers/useFormControl';
55
import { booleanOrFunction } from './helpers/form-helpers';
66

7-
export function FormInput({
8-
id,
9-
type,
10-
name,
11-
placeholder,
12-
required: _required,
13-
minLength,
14-
maxLength,
15-
min,
16-
max,
17-
pattern,
18-
step,
19-
disabled: _disabled,
20-
}) {
7+
export function FormInput({ type, name, required: _required, disabled: _disabled, ..._attrs }) {
218
const { getValue, handleOnChange, register, getFormData } = useFormControl(name, type);
229
const registerRef = useCallback(register, [register]);
2310
const disabled = booleanOrFunction(_disabled, getFormData());
2411
const required = booleanOrFunction(_required, getFormData());
2512

26-
const attrs = { required, name, id, placeholder, type, minLength, maxLength, min, max, pattern, step, disabled };
13+
const attrs = {
14+
..._attrs,
15+
disabled,
16+
name,
17+
required,
18+
type,
19+
};
2720

2821
if (type === 'datetime-local') {
2922
attrs.defaultValue = getValue();
@@ -39,6 +32,7 @@ FormInput.defaultProps = {
3932
};
4033

4134
FormInput.propTypes = {
35+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
4236
id: PropTypes.string,
4337
max: PropTypes.string,
4438
maxLength: PropTypes.string,
@@ -47,8 +41,8 @@ FormInput.propTypes = {
4741
name: PropTypes.string.isRequired,
4842
pattern: PropTypes.string,
4943
placeholder: PropTypes.string,
44+
readOnly: PropTypes.bool,
5045
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
5146
step: PropTypes.string,
5247
type: PropTypes.string,
53-
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
5448
};

src/forms/FormRadio.jsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33

44
import { useFormControl } from './helpers/useFormControl';
55
import { booleanOrFunction } from './helpers/form-helpers';
6+
import { formatClasses } from '../utils/attributes';
67

78
export function FormRadio({ id, name, required: _required, checkedValue, valueLabel, inline, disabled: _disabled }) {
89
const { getValue, handleOnChange, register, getFormData } = useFormControl(name, 'boolean');
@@ -12,7 +13,7 @@ export function FormRadio({ id, name, required: _required, checkedValue, valueLa
1213
const required = booleanOrFunction(_required, getFormData());
1314

1415
return (
15-
<div className={`custom-control custom-radio ${inline ? 'custom-control-inline' : ''}`}>
16+
<div className={formatClasses([`custom-control`, `custom-radio`, inline && 'custom-control-inline'])}>
1617
<input
1718
{...{ required, name, id, disabled }}
1819
type="radio"
@@ -35,10 +36,10 @@ FormRadio.defaultProps = {
3536

3637
FormRadio.propTypes = {
3738
checkedValue: PropTypes.any,
39+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
3840
id: PropTypes.string.isRequired,
3941
inline: PropTypes.bool,
4042
name: PropTypes.string.isRequired,
4143
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
4244
valueLabel: PropTypes.string,
43-
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
4445
};

src/forms/FormSelect.jsx

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,32 @@ import { getValueByPath } from '../utils/getters-setters';
55
import { normalizeOptions, booleanOrFunction } from './helpers/form-helpers';
66
import { useFormControl } from './helpers/useFormControl';
77

8-
export function FormSelect({ id, name, options, required: _required, placeholder, trackBy, disabled: _disabled }) {
8+
export function FormSelect({
9+
name,
10+
options,
11+
required: _required,
12+
placeholder,
13+
trackBy,
14+
disabled: _disabled,
15+
..._attrs
16+
}) {
917
const { getFormData, getValue, handleOnChange, register } = useFormControl(name);
1018
const registerRef = useCallback(register, [register]);
1119
const value = getValue();
1220
const normalizedOptions = normalizeOptions(options, getFormData());
1321
const disabled = booleanOrFunction(_disabled, getFormData());
1422
const required = booleanOrFunction(_required, getFormData());
1523

24+
const attrs = {
25+
..._attrs,
26+
disabled,
27+
name,
28+
required,
29+
};
30+
1631
return (
1732
<select
18-
{...{ required, name, id, disabled }}
33+
{...attrs}
1934
className="custom-select"
2035
onChange={handleOnChange}
2136
value={getSelectedOption(value, normalizedOptions, trackBy)}
@@ -29,16 +44,21 @@ export function FormSelect({ id, name, options, required: _required, placeholder
2944
}
3045

3146
FormSelect.propTypes = {
47+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
3248
id: PropTypes.string,
3349
name: PropTypes.string.isRequired,
3450
options: PropTypes.oneOfType([
3551
PropTypes.func,
36-
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
52+
PropTypes.arrayOf(
53+
PropTypes.oneOfType([
54+
PropTypes.string,
55+
PropTypes.shape({ value: PropTypes.any.isRequired, label: PropTypes.string.isRequired }),
56+
])
57+
),
3758
]),
3859
placeholder: PropTypes.string,
3960
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
4061
trackBy: PropTypes.string,
41-
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
4262
};
4363

4464
function renderOptions(options, trackBy) {

src/forms/FormSwitch.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export function FormSwitch({ id, name, required: _required, trueLabel, falseLabe
2929
}
3030

3131
FormSwitch.propTypes = {
32+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
3233
falseLabel: PropTypes.string,
3334
id: PropTypes.string.isRequired,
3435
name: PropTypes.string.isRequired,
3536
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
3637
trueLabel: PropTypes.string,
37-
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
3838
};

src/forms/FormTextarea.jsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ import PropTypes from 'prop-types';
44
import { useFormControl } from './helpers/useFormControl';
55
import { booleanOrFunction } from './helpers/form-helpers';
66

7-
export function FormTextarea({ id, name, required: _required, placeholder, rows, disabled: _disabled }) {
7+
export function FormTextarea({ name, required: _required, disabled: _disabled, ..._attrs }) {
88
const { getValue, handleOnChange, register, getFormData } = useFormControl(name);
99
const registerRef = useCallback(register, [register]);
1010
const disabled = booleanOrFunction(_disabled, getFormData());
1111
const required = booleanOrFunction(_required, getFormData());
1212

13+
const attrs = {
14+
..._attrs,
15+
disabled,
16+
name,
17+
required,
18+
};
19+
1320
return (
1421
<textarea
15-
{...{ required, name, id, placeholder, rows, disabled }}
22+
{...attrs}
1623
className="form-control"
1724
onChange={handleOnChange}
1825
value={getValue()}
@@ -26,10 +33,11 @@ FormTextarea.defaultProps = {
2633
};
2734

2835
FormTextarea.propTypes = {
36+
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
2937
id: PropTypes.string,
3038
name: PropTypes.string.isRequired,
3139
placeholder: PropTypes.string,
40+
readOnly: PropTypes.bool,
3241
required: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
3342
rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
34-
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
3543
};

0 commit comments

Comments
 (0)