Added ui:enumDisabled to RadioWidget.js (#1028)

* Added ui:enumDisabled to RadioWidget.js

* cs-format

* Added a test and fixed the disabled state
This commit is contained in:
Jonathan Grimmtjärn
2018-09-27 17:33:16 +02:00
committed by Ethan Glasser-Camp
parent 9813483ab1
commit 7c252de331
2 changed files with 39 additions and 3 deletions

View File

@@ -13,14 +13,17 @@ function RadioWidget(props) {
} = props;
// Generating a unique field name to identify this set of radio buttons
const name = Math.random().toString();
const { enumOptions, inline } = options;
const { enumOptions, enumDisabled, inline } = options;
// checked={checked} has been moved above name={name}, As mentioned in #349;
// this is a temporary fix for radio button rendering bug in React, facebook/react#7630.
return (
<div className="field-radio-group">
{enumOptions.map((option, i) => {
const checked = option.value === value;
const disabledCls = disabled || readonly ? "disabled" : "";
const itemDisabled =
enumDisabled && enumDisabled.indexOf(option.value) != -1;
const disabledCls =
disabled || itemDisabled || readonly ? "disabled" : "";
const radio = (
<span>
<input
@@ -29,7 +32,7 @@ function RadioWidget(props) {
name={name}
required={required}
value={option.value}
disabled={disabled || readonly}
disabled={disabled || itemDisabled || readonly}
autoFocus={autofocus && i === 0}
onChange={_ => onChange(option.value)}
/>

View File

@@ -2,6 +2,7 @@ import { expect } from "chai";
import React from "react";
import { Simulate } from "react-addons-test-utils";
import SelectWidget from "../src/components/widgets/SelectWidget";
import RadioWidget from "../src/components/widgets/RadioWidget";
import { createFormComponent, createSandbox } from "./test_utils";
describe("uiSchema", () => {
@@ -439,6 +440,38 @@ describe("uiSchema", () => {
);
});
});
describe("enum fields disabled radio options", () => {
const schema = {
type: "object",
properties: {
field: {
type: "string",
enum: ["foo", "bar"],
},
},
};
const uiSchema = {
field: {
"ui:widget": RadioWidget,
"ui:options": {
className: "custom",
},
"ui:enumDisabled": ["foo"],
},
};
it("should have atleast one radio option disabled", () => {
const { node } = createFormComponent({ schema, uiSchema });
const disabledOptionsLen = uiSchema.field["ui:enumDisabled"].length;
expect(node.querySelectorAll("input:disabled")).to.have.length.of(
disabledOptionsLen
);
expect(node.querySelectorAll("input:enabled")).to.have.length.of(
// Two options, one disabled, plus the placeholder
2 - disabledOptionsLen
);
});
});
});
describe("ui:help", () => {