Skip to content

Commit 582f3dd

Browse files
committed
feat(forms): allow booleans as select option values
1 parent 32e059f commit 582f3dd

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

demo/FormExamples.jsx

+11
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ export function FormExamples() {
188188
required
189189
/>
190190
</div>
191+
<div className="col-4">
192+
<FormGroupSelect
193+
name="selectBooleanField"
194+
label="Select boolean"
195+
options={[
196+
{ label: 'Yes', value: true },
197+
{ label: 'No', value: false },
198+
]}
199+
required
200+
/>
201+
</div>
191202
<div className="col-4">
192203
<FormGroupSelect
193204
name="selectField3"

demo/demo.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ReactDOM.render(
1717
<StatefulTabs
1818
vertical={true}
1919
onlyRenderActiveTab={true}
20-
initialTab={7}
20+
initialTab={2}
2121
tabs={[
2222
{
2323
title: 'Dialog',

src/forms/helpers/form-helpers.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { getValueByPath } from '../../utils/getters-setters';
3-
import { isFunction } from '../../utils/types';
3+
import { isFunction, isUndefined } from '../../utils/types';
44

55
export const FormContext = React.createContext(null);
66

@@ -45,8 +45,8 @@ export function normalizeOptions(options, formData, extraData) {
4545
}
4646

4747
return _options.map((option) => ({
48-
value: option.value || option,
49-
label: option.label || serializeValue(option),
48+
value: isUndefined(option.value) ? option : option.value,
49+
label: isUndefined(option.label) ? serializeValue(option) : option.label,
5050
}));
5151
}
5252

src/forms/helpers/useFormControl.js

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function useFormControl(name, type) {
2323
setValue,
2424
handleOnChange: ({ target }, _type) => {
2525
const value = getTargetValue(target);
26+
2627
const decodedValue = decode(value, type || _type);
2728

2829
setValue(decodedValue);
@@ -68,6 +69,10 @@ function decode(value, type) {
6869
return parseFloat(value);
6970
}
7071

72+
if (type === 'boolean') {
73+
return value === 'true';
74+
}
75+
7176
return value;
7277
}
7378

0 commit comments

Comments
 (0)