Skip to content

Commit d5929a1

Browse files
committed
Added support for uiSchema and alternative widgets.
1 parent 6bf63b6 commit d5929a1

34 files changed

+590
-56
lines changed

README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ react-jsonschema-form
33

44
[![Build Status](https://travis-ci.org/mozilla-services/react-jsonschema-form.svg)](https://travis-ci.org/mozilla-services/react-jsonschema-form)
55

6-
A simple [React](http://facebook.github.io/react/) component capable of building
7-
HTML forms out of a [JSON schema](http://jsonschema.net/).
6+
A simple [React](http://facebook.github.io/react/) component capable of building HTML forms out of a [JSON schema](http://jsonschema.net/).
87

98
Requires React 0.14+.
109

@@ -60,15 +59,46 @@ render((
6059
), document.getElementById("app"));
6160
```
6261

62+
### Alternative widgets
63+
64+
JSONSchema is limited for describing how a given data type should be rendered as an input component, that's why this lib introduces the concept of *UI schema*. A UI schema is basically an object literal describing which UI widget should be used to render a certain field
65+
66+
Example:
67+
68+
```js
69+
const uiSchema =  {
70+
done: {
71+
widget: "radio" // could also be "select"
72+
}
73+
};
74+
75+
render((
76+
<Form schema={schema}
77+
uiSchema={uiSchema}
78+
formData={formData} />
79+
), document.getElementById("app"));
80+
```
81+
82+
Here's a list of supported alternative widgets for different JSONSchema data types:
83+
84+
#### `boolean`
85+
86+
* `radio`: a radio button group with `true` and `false` as selectable values;
87+
* `select`: a select box with `true` and `false` as options;
88+
* by default, a checkbox is used
89+
90+
#### `string`:
91+
92+
* `textarea`: a `textarea` element
93+
* by default, a regular `input[type=text]` element is used.
94+
6395
## Development server
6496

6597
```
6698
$ npm start
6799
```
68100

69-
A [Cosmos development server](https://github.com/skidding/cosmos) showcasing
70-
components with hot reload enabled is available at
71-
[localhost:8080](http://localhost:8080).
101+
A [Cosmos development server](https://github.com/skidding/cosmos) showcasing components with hot reload enabled is available at [localhost:8080](http://localhost:8080).
72102

73103
## Tests
74104

fixtures/Form/nested-default.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
schema: {
3+
title: "Todo Tasks",
4+
description: "Tasks collection.",
5+
type: "object",
6+
additionalProperties: false,
7+
required: [
8+
"title", "tasks"
9+
],
10+
default: {
11+
title: "Default task list",
12+
tasks: [
13+
{title: "A default task", done: true},
14+
{title: "Another default task", done: false},
15+
]
16+
},
17+
properties: {
18+
title: {
19+
type: "string",
20+
title: "Tasks list title",
21+
},
22+
tasks: {
23+
type: "array",
24+
title: "Tasks list",
25+
items: {
26+
type: "object",
27+
properties: {
28+
done: {
29+
type: "boolean",
30+
title: "Done?",
31+
description: "Is that task done already?"
32+
},
33+
title: {
34+
type: "string",
35+
title: "Title",
36+
description: "The task title.",
37+
minLength: 1
38+
}
39+
}
40+
}
41+
}
42+
}
43+
},
44+
onSubmit: console.log.bind(console, "submit"),
45+
onError: console.log.bind(console, "errors")
46+
};

fixtures/Form/nested-uiSchema.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module.exports = {
2+
schema: {
3+
title: "Todo Tasks",
4+
description: "Tasks collection.",
5+
type: "object",
6+
additionalProperties: false,
7+
required: [
8+
"title", "tasks"
9+
],
10+
properties: {
11+
title: {
12+
type: "string",
13+
title: "Tasks list title",
14+
},
15+
tasks: {
16+
type: "array",
17+
title: "Tasks list",
18+
items: {
19+
type: "object",
20+
properties: {
21+
type: {
22+
type: "string",
23+
title: "Category",
24+
enum: ["coding", "sleeping"]
25+
},
26+
done: {
27+
type: "boolean",
28+
title: "Done?",
29+
description: "Is that task done already?"
30+
},
31+
title: {
32+
type: "string",
33+
title: "Title",
34+
description: "The task title.",
35+
minLength: 1
36+
}
37+
}
38+
}
39+
}
40+
}
41+
},
42+
uiSchema: {
43+
title: {
44+
widget: "textarea",
45+
},
46+
tasks: {
47+
items: {
48+
type: {
49+
widget: "radio"
50+
},
51+
done: {
52+
widget: "radio",
53+
},
54+
title: {
55+
widget: "textarea"
56+
}
57+
}
58+
}
59+
},
60+
onSubmit: console.log.bind(console, "submit"),
61+
onError: console.log.bind(console, "errors")
62+
};

fixtures/fields/ArrayField/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ module.exports = {
77
title: "item"
88
}
99
},
10-
onChange: console.log.bind(console)
10+
onChange: console.log.bind(console, "change")
1111
};

fixtures/fields/ArrayField/with-default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ module.exports = {
88
title: "item"
99
}
1010
},
11-
onChange: console.log.bind(console)
11+
onChange: console.log.bind(console, "change")
1212
};

fixtures/fields/ArrayField/with-formData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ module.exports = {
88
}
99
},
1010
formData: ["item1", "item2"],
11-
onChange: console.log.bind(console)
11+
onChange: console.log.bind(console, "change")
1212
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
schema: {
3+
type: "array",
4+
title: "title",
5+
items: {
6+
type: "string",
7+
title: "item"
8+
}
9+
},
10+
uiSchema: {
11+
items: {
12+
widget: "textarea"
13+
}
14+
},
15+
formData: ["item1", "item2"],
16+
onChange: console.log.bind(console, "change")
17+
};

fixtures/fields/BooleanField/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module.exports = {
33
type: "boolean",
44
title: "My boolean",
55
},
6-
onChange: console.log.bind(console)
6+
onChange: console.log.bind(console, "change")
77
};

fixtures/fields/BooleanField/with-default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ module.exports = {
44
title: "My boolean",
55
default: true,
66
},
7-
onChange: console.log.bind(console)
7+
onChange: console.log.bind(console, "change")
88
};

fixtures/fields/BooleanField/with-formData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ module.exports = {
44
title: "My boolean",
55
},
66
formData: true,
7-
onChange: console.log.bind(console)
7+
onChange: console.log.bind(console, "change")
88
};

0 commit comments

Comments
 (0)