Skip to content

Commit

Permalink
Merge pull request #81 from mozilla-services/boolean-labels
Browse files Browse the repository at this point in the history
[Experiment] Ref #80: Add support for enumNames to boolean widgets.
  • Loading branch information
n1k0 committed Mar 22, 2016
2 parents 2fcaa40 + 7e34b67 commit ee9b0cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ Here's a list of supported alternative widgets for different JSONSchema data typ
* `select`: a select box with `true` and `false` as options;
* by default, a checkbox is used

> Note: To set the labels for a boolean field, instead of using `true` and `false` you can set `enumNames` in your schema. Note that `enumNames` belongs in your `schema`, not the `uiSchema`, and the order is always `[true, false]`.
#### For `string` fields

* `textarea`: a `textarea` element;
Expand Down
10 changes: 9 additions & 1 deletion src/components/fields/BooleanField.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import React, { PropTypes } from "react";
import { defaultFieldValue, getAlternativeWidget, optionsList } from "../../utils";
import CheckboxWidget from "./../widgets/CheckboxWidget";


function buildOptions(schema) {
return optionsList(Object.assign({
enumNames: ["true", "false"],
enum: [true, false]
}, {enumNames: schema.enumNames}));
}

function BooleanField(props) {
const {schema, name, uiSchema, formData, widgets, required, onChange} = props;
const {title, description} = schema;
Expand All @@ -18,7 +26,7 @@ function BooleanField(props) {
};
if (widget) {
const Widget = getAlternativeWidget(schema.type, widget, widgets);
return <Widget options={optionsList({enum: [true, false]})} {... commonProps} />;
return <Widget options={buildOptions(schema)} {... commonProps} />;
}
return <CheckboxWidget {...commonProps} />;
}
Expand Down
22 changes: 22 additions & 0 deletions test/BooleanField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,26 @@ describe("BooleanField", () => {
expect(node.querySelector(".field input").checked)
.eql(true);
});

it("should support enumNames for radio widgets", () => {
const {node} = createFormComponent({schema: {
type: "boolean",
enumNames: ["Yes", "No"],
}, formData: true, uiSchema: {"ui:widget": "radio"}});

const labels = [].map.call(node.querySelectorAll(".field-radio-group label"),
label => label.textContent);
expect(labels).eql(["Yes", "No"]);
});

it("should support enumNames for select", () => {
const {node} = createFormComponent({schema: {
type: "boolean",
enumNames: ["Yes", "No"],
}, formData: true, uiSchema: {"ui:widget": "select"}});

const labels = [].map.call(node.querySelectorAll(".field option"),
label => label.textContent);
expect(labels).eql(["Yes", "No"]);
});
});

0 comments on commit ee9b0cb

Please sign in to comment.