Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
epicfaace committed Jan 21, 2019
2 parents 9fdab86 + 3f35d78 commit 8db0f3b
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -202,6 +202,7 @@ This component follows [JSON Schema](http://json-schema.org/documentation.html)
- Custom errors messages with transformErrors: <https://jsfiddle.net/revolunet/5r3swnr4/>
- 2 columns form with CSS and FieldTemplate: <https://jsfiddle.net/n1k0/bw0ffnz4/1/>
- Validate and submit form from external control: <https://jsfiddle.net/spacebaboon/g5a1re63/>
- Custom component for Help text with `ui:help`: <https://codesandbox.io/s/14pqx97xl7/>

## Contributing

Expand Down
4 changes: 4 additions & 0 deletions src/components/widgets/CheckboxWidget.js
Expand Up @@ -12,6 +12,8 @@ function CheckboxWidget(props) {
readonly,
label,
autofocus,
onBlur,
onFocus,
onChange,
} = props;
return (
Expand All @@ -28,6 +30,8 @@ function CheckboxWidget(props) {
disabled={disabled || readonly}
autoFocus={autofocus}
onChange={event => onChange(event.target.checked)}
onBlur={onBlur && (event => onBlur(id, event.target.checked))}
onFocus={onFocus && (event => onFocus(id, event.target.checked))}
/>
<span>{label}</span>
</label>
Expand Down
4 changes: 4 additions & 0 deletions src/components/widgets/RadioWidget.js
Expand Up @@ -9,6 +9,8 @@ function RadioWidget(props) {
disabled,
readonly,
autofocus,
onBlur,
onFocus,
onChange,
id,
} = props;
Expand Down Expand Up @@ -36,6 +38,8 @@ function RadioWidget(props) {
disabled={disabled || itemDisabled || readonly}
autoFocus={autofocus && i === 0}
onChange={_ => onChange(option.value)}
onBlur={onBlur && (event => onBlur(id, event.target.value))}
onFocus={onFocus && (event => onFocus(id, event.target.value))}
/>
<span>{option.label}</span>
</span>
Expand Down
132 changes: 132 additions & 0 deletions test/BooleanField_test.js
Expand Up @@ -245,6 +245,50 @@ describe("BooleanField", () => {
expect(node.querySelectorAll(".radio-inline")).to.have.length.of(2);
});

it("should handle a focus event for radio widgets", () => {
const onFocus = sandbox.spy();
const { node } = createFormComponent({
schema: {
type: "boolean",
default: false,
},
uiSchema: {
"ui:widget": "radio",
},
onFocus,
});

const element = node.querySelector(".field-radio-group");
Simulate.focus(node.querySelector("input"), {
target: {
value: false,
},
});
expect(onFocus.calledWith(element.id, false)).to.be.true;
});

it("should handle a blur event for radio widgets", () => {
const onBlur = sandbox.spy();
const { node } = createFormComponent({
schema: {
type: "boolean",
default: false,
},
uiSchema: {
"ui:widget": "radio",
},
onBlur,
});

const element = node.querySelector(".field-radio-group");
Simulate.blur(node.querySelector("input"), {
target: {
value: false,
},
});
expect(onBlur.calledWith(element.id, false)).to.be.true;
});

it("should support enumNames for select", () => {
const { node } = createFormComponent({
schema: {
Expand All @@ -262,6 +306,50 @@ describe("BooleanField", () => {
expect(labels).eql(["", "Yes", "No"]);
});

it("should handle a focus event with checkbox", () => {
const onFocus = sandbox.spy();
const { node } = createFormComponent({
schema: {
type: "boolean",
default: false,
},
uiSchema: {
"ui:widget": "select",
},
onFocus,
});

const element = node.querySelector("select");
Simulate.focus(element, {
target: {
value: false,
},
});
expect(onFocus.calledWith(element.id, false)).to.be.true;
});

it("should handle a blur event with select", () => {
const onBlur = sandbox.spy();
const { node } = createFormComponent({
schema: {
type: "boolean",
default: false,
},
uiSchema: {
"ui:widget": "select",
},
onBlur,
});

const element = node.querySelector("select");
Simulate.blur(element, {
target: {
value: false,
},
});
expect(onBlur.calledWith(element.id, false)).to.be.true;
});

it("should render the widget with the expected id", () => {
const { node } = createFormComponent({
schema: {
Expand All @@ -285,6 +373,50 @@ describe("BooleanField", () => {
expect(node.querySelector("#custom")).to.exist;
});

it("should handle a focus event with checkbox", () => {
const onFocus = sandbox.spy();
const { node } = createFormComponent({
schema: {
type: "boolean",
default: false,
},
uiSchema: {
"ui:widget": "checkbox",
},
onFocus,
});

const element = node.querySelector("input");
Simulate.focus(element, {
target: {
checked: false,
},
});
expect(onFocus.calledWith(element.id, false)).to.be.true;
});

it("should handle a blur event with checkbox", () => {
const onBlur = sandbox.spy();
const { node } = createFormComponent({
schema: {
type: "boolean",
default: false,
},
uiSchema: {
"ui:widget": "checkbox",
},
onBlur,
});

const element = node.querySelector("input");
Simulate.blur(element, {
target: {
checked: false,
},
});
expect(onBlur.calledWith(element.id, false)).to.be.true;
});

describe("Label", () => {
const Widget = props => <div id={`label-${props.label}`} />;

Expand Down

0 comments on commit 8db0f3b

Please sign in to comment.