Skip to content

Commit b258439

Browse files
committed
Added propTypes to widget components.
1 parent 4a88c72 commit b258439

File tree

9 files changed

+107
-41
lines changed

9 files changed

+107
-41
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
type: "boolean",
23
label: "foo",
34
onChange: console.log.bind(console)
45
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module.exports = {
2-
schema: {
3-
type: "string",
4-
title: "foo",
5-
default: "b"
6-
},
7-
options: ["a", "b"]
2+
type: "string",
3+
label: "foo",
4+
defaultValue: "b",
5+
options: ["a", "b"],
6+
onChange: console.log.bind(console)
87
};

src/components/fields/BooleanField.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import CheckboxField from "./../widgets/CheckboxWidget";
66
export default function BooleanField({schema, formData, required, onChange}) {
77
const {title, description} = schema;
88
const commonProps = {
9+
type: schema.type,
910
onChange,
1011
label: title,
1112
placeholder: description,
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import React from "react";
22

3-
import TextField from "./../widgets/TextWidget";
4-
import SelectField from "./../widgets/SelectWidget";
3+
import { defaultFieldValue } from "../../utils";
4+
import TextWidget from "./../widgets/TextWidget";
5+
import SelectWidget from "./../widgets/SelectWidget";
56

67

78
export default function StringField({schema, formData, required, onChange}) {
9+
const {type, title, description} = schema;
810
const commonProps = {
9-
schema,
11+
type: type,
12+
label: title,
13+
placeholder: description,
1014
onChange,
11-
label: schema.title,
12-
formData: formData,
15+
value: formData,
1316
required: required,
17+
defaultValue: defaultFieldValue(formData, schema),
1418
};
1519
if (Array.isArray(schema.enum)) {
16-
return <SelectField options={schema.enum} {...commonProps} />;
20+
// XXX uiSchema: Could also be a list of radio buttons
21+
return <SelectWidget options={schema.enum} {...commonProps} />;
1722
}
18-
return <TextField placeholder={schema.description} {...commonProps} />;
23+
// XXX uiSchema: Could also be a textarea for longer texts
24+
return <TextWidget {...commonProps} />;
1925
}

src/components/widgets/CheckboxWidget.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { PropTypes } from "react";
33
import Wrapper from "./../widgets/Wrapper";
44

55
function CheckboxWidget({
6+
type,
67
onChange,
78
label,
89
defaultValue,
@@ -11,7 +12,7 @@ function CheckboxWidget({
1112
placeholder
1213
}) {
1314
return (
14-
<Wrapper label={label} required={required} type="boolean">
15+
<Wrapper label={label} required={required} type={type}>
1516
<input type="checkbox"
1617
title={placeholder}
1718
checked={value}
@@ -23,6 +24,7 @@ function CheckboxWidget({
2324
}
2425

2526
CheckboxWidget.propTypes = {
27+
type: PropTypes.string.isRequired,
2628
onChange: PropTypes.func,
2729
label: PropTypes.string,
2830
defaultValue: PropTypes.bool,
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
import React from "react";
1+
import React, { PropTypes } from "react";
22

33
import Wrapper from "./../widgets/Wrapper";
44

55

6-
export default function RadioWidget({schema, formData, options, onChange}) {
6+
function RadioWidget({
7+
type,
8+
options,
9+
label,
10+
placeholder,
11+
value,
12+
defaultValue,
13+
required,
14+
onChange
15+
}) {
716
// Generating a unique field name to identify this set of radio buttons
817
const name = Math.random().toString();
918
return (
10-
<Wrapper type={schema.type} label={schema.title}>
19+
<Wrapper type={type} label={label}>
1120
{
1221
options.map((option, i) => {
13-
const checked = formData ? option === formData :
14-
option === schema.default;
22+
const checked = value ? option === value :
23+
option === defaultValue;
1524
return (
1625
<div key={i}>
1726
<label>
1827
<input type="radio"
1928
name={name}
2029
value={option}
2130
checked={checked}
31+
placeholder={placeholder}
2232
onChange={_ => onChange(option)} />
2333
{option}
2434
</label>
@@ -29,3 +39,16 @@ export default function RadioWidget({schema, formData, options, onChange}) {
2939
</Wrapper>
3040
);
3141
}
42+
43+
RadioWidget.propTypes = {
44+
type: PropTypes.string.isRequired,
45+
options: PropTypes.array.isRequired,
46+
label: PropTypes.string,
47+
placeholder: PropTypes.string,
48+
value: PropTypes.any,
49+
defaultValue: PropTypes.any,
50+
required: PropTypes.bool,
51+
onChange: PropTypes.func,
52+
};
53+
54+
export default RadioWidget;
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
import React from "react";
1+
import React, { PropTypes } from "react";
22

3-
import { defaultFieldValue } from "../../utils";
43
import Wrapper from "./../widgets/Wrapper";
54

65

7-
export default function SelectWidget({
8-
schema,
9-
formData,
6+
function SelectWidget({
7+
type,
108
options,
11-
required,
129
label,
10+
placeholder,
11+
value,
12+
defaultValue,
13+
required,
1314
onChange
1415
}) {
1516
return (
16-
<Wrapper label={label} required={required}>
17+
<Wrapper label={label} required={required} type={type}>
1718
<select
18-
title={schema.description}
19-
value={defaultFieldValue(formData, schema)}
20-
defaultValue={schema.default}
19+
title={placeholder}
20+
value={value}
21+
defaultValue={defaultValue}
2122
onChange={(event) => onChange(event.target.value)}>{
2223
options.map((option, i) => {
2324
return <option key={i}>{option}</option>;
@@ -26,3 +27,16 @@ export default function SelectWidget({
2627
</Wrapper>
2728
);
2829
}
30+
31+
SelectWidget.propTypes = {
32+
type: PropTypes.string.isRequired,
33+
options: PropTypes.array.isRequired,
34+
label: PropTypes.string,
35+
placeholder: PropTypes.string,
36+
value: PropTypes.any,
37+
defaultValue: PropTypes.any,
38+
required: PropTypes.bool,
39+
onChange: PropTypes.func,
40+
};
41+
42+
export default SelectWidget;
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1-
import React from "react";
1+
import React, { PropTypes } from "react";
22

3-
import { defaultFieldValue } from "../../utils";
43
import Wrapper from "./../widgets/Wrapper";
54

65

7-
export default function TextWidget({
8-
schema,
9-
formData,
6+
function TextWidget({
7+
type,
108
label,
11-
required,
129
placeholder,
10+
value,
11+
defaultValue,
12+
required,
1313
onChange
1414
}) {
1515
return (
16-
<Wrapper label={label} required={required}
17-
type={schema.type}>
16+
<Wrapper label={label} required={required} type={type}>
1817
<input type="text"
19-
value={defaultFieldValue(formData, schema)}
20-
defaultValue={schema.default}
18+
value={value}
19+
defaultValue={defaultValue}
2120
placeholder={placeholder}
2221
required={required}
2322
onChange={(event) => onChange(event.target.value)} />
2423
</Wrapper>
2524
);
2625
}
26+
27+
TextWidget.propTypes = {
28+
type: PropTypes.string.isRequired,
29+
label: PropTypes.string,
30+
placeholder: PropTypes.string,
31+
value: PropTypes.string,
32+
defaultValue: PropTypes.string,
33+
required: PropTypes.bool,
34+
onChange: PropTypes.func,
35+
};
36+
37+
export default TextWidget;

src/components/widgets/Wrapper.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { PropTypes } from "react";
22

33
const REQUIRED_FIELD_SYMBOL = "*";
44

@@ -12,7 +12,7 @@ function getLabel(label, required) {
1212
return label;
1313
}
1414

15-
export default function Wrapper({type, children, label, required}) {
15+
function Wrapper({type, children, label, required}) {
1616
return (
1717
<div className={`field field-${type}`}>
1818
<label>
@@ -22,3 +22,12 @@ export default function Wrapper({type, children, label, required}) {
2222
</div>
2323
);
2424
}
25+
26+
Wrapper.propTypes = {
27+
type: PropTypes.string.isRequired,
28+
label: PropTypes.string,
29+
required: PropTypes.bool,
30+
children: React.PropTypes.node.isRequired,
31+
};
32+
33+
export default Wrapper;

0 commit comments

Comments
 (0)