Skip to content

Commit a8881f3

Browse files
committed
widgets: add number and boolean widgets
1 parent accec32 commit a8881f3

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

src/lib/forms/SelectField.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ export class SelectField extends Component {
4949
options,
5050
onChange,
5151
onAddItem,
52+
multiple,
5253
...uiProps
5354
} = cmpProps;
54-
const value = getIn(values, fieldPath, defaultValue);
55-
const initialValue = getIn(initialValues, fieldPath, "");
55+
const _defaultValue = multiple ? [] : "";
56+
const value = getIn(values, fieldPath, defaultValue || _defaultValue);
57+
const initialValue = getIn(initialValues, fieldPath, _defaultValue);
5658
return (
5759
<Form.Dropdown
5860
fluid
@@ -84,6 +86,7 @@ export class SelectField extends Component {
8486
}}
8587
options={options}
8688
value={value}
89+
multiple={multiple}
8790
{...uiProps}
8891
/>
8992
);
@@ -112,6 +115,7 @@ SelectField.propTypes = {
112115
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
113116
onChange: PropTypes.func,
114117
onAddItem: PropTypes.func,
118+
multiple: PropTypes.bool,
115119
};
116120

117121
SelectField.defaultProps = {
@@ -121,4 +125,5 @@ SelectField.defaultProps = {
121125
label: "",
122126
onChange: undefined,
123127
onAddItem: undefined,
128+
multiple: false,
124129
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// This file is part of React-Invenio-Forms
2+
// Copyright (C) 2020 CERN.
3+
// Copyright (C) 2020 Northwestern University.
4+
//
5+
// React-Invenio-Forms is free software; you can redistribute it and/or modify it
6+
// under the terms of the MIT License; see LICENSE file for more details.
7+
8+
import PropTypes from "prop-types";
9+
import React from "react";
10+
import { Form } from "semantic-ui-react";
11+
import { FieldLabel } from "../../FieldLabel";
12+
import { RadioField } from "../../RadioField";
13+
import { useField } from "formik";
14+
15+
export default function BooleanInput({
16+
description,
17+
icon,
18+
falseLabel,
19+
fieldPath,
20+
label,
21+
trueLabel,
22+
required,
23+
}) {
24+
const [meta] = useField(fieldPath);
25+
console.log(meta);
26+
return (
27+
<>
28+
<Form.Group inline className="mb-0" required>
29+
<Form.Field required={required} error={meta.error}>
30+
<FieldLabel htmlFor={fieldPath} icon={icon} label={label} />
31+
</Form.Field>
32+
<RadioField
33+
fieldPath={fieldPath}
34+
label={trueLabel}
35+
checked={meta.value === true}
36+
value
37+
optimized
38+
/>
39+
<RadioField
40+
fieldPath={fieldPath}
41+
label={falseLabel}
42+
checked={meta.value === false}
43+
value={false}
44+
optimized
45+
/>
46+
</Form.Group>
47+
{description && <label className="helptext">{description}</label>}
48+
</>
49+
);
50+
}
51+
52+
BooleanInput.propTypes = {
53+
fieldPath: PropTypes.string.isRequired,
54+
label: PropTypes.string.isRequired,
55+
trueLabel: PropTypes.string.isRequired,
56+
falseLabel: PropTypes.string.isRequired,
57+
description: PropTypes.string.isRequired,
58+
icon: PropTypes.string,
59+
required: PropTypes.bool,
60+
};
61+
62+
BooleanInput.defaultProps = {
63+
icon: undefined,
64+
required: false,
65+
};

src/lib/forms/widgets/text/Input.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import { TextField } from "../../TextField";
66

77
export default class Input extends Component {
88
render() {
9-
const { fieldPath, required, label, icon, placeholder, description, disabled } =
10-
this.props;
9+
const {
10+
fieldPath,
11+
required,
12+
label,
13+
icon,
14+
placeholder,
15+
description,
16+
disabled,
17+
type,
18+
} = this.props;
1119

1220
return (
1321
<TextField
@@ -18,6 +26,7 @@ export default class Input extends Component {
1826
disabled={disabled}
1927
label={<FieldLabel htmlFor={fieldPath} icon={icon} label={label} />}
2028
placeholder={placeholder}
29+
type={type}
2130
/>
2231
);
2332
}
@@ -31,10 +40,12 @@ Input.propTypes = {
3140
icon: PropTypes.string,
3241
required: PropTypes.bool,
3342
disabled: PropTypes.bool,
43+
type: PropTypes.string,
3444
};
3545

3646
Input.defaultProps = {
3747
icon: undefined,
3848
required: false,
3949
disabled: false,
50+
type: "input",
4051
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from "react";
2+
import Input from "./Input";
3+
4+
const NumberInput = (props) => <Input {...props} type="number" />;
5+
export default NumberInput;

src/lib/forms/widgets/text/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ export { default as RichInput } from "./RichInput";
22
export { default as TextArea } from "./TextArea";
33
export { default as Input } from "./Input";
44
export { default as MultiInput } from "./MultiInput";
5+
export { default as NumberInput } from "./NumberInput";
6+
export { default as BooleanInput } from "./BooleanInput";

0 commit comments

Comments
 (0)