Skip to content

Commit

Permalink
fix: Pass disabled prop to FieldTemplate. (rjsf-team#741)
Browse files Browse the repository at this point in the history
If you want to change how your FieldTemplate is rendered based on
ui:disabled, you currently have to check `uiSchema["ui:disabled"]`,
which is unlike the pattern for `readonly` and other known `ui:*`
settings.
  • Loading branch information
theengineear authored and glasserc committed Nov 9, 2017
1 parent fdc5566 commit f4583f9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ The following props are passed to a custom field template component:
- `hidden`: A boolean value stating if the field should be hidden.
- `required`: A boolean value stating if the field is required.
- `readonly`: A boolean value stating if the field is read-only.
- `disabled`: A boolean value stating if the field is disabled.
- `displayLabel`: A boolean value stating if the label should be rendered or not. This is useful for nested fields in arrays where you don't want to clutter the UI.
- `fields`: An array containing all Form's fields including your [custom fields](#custom-field-components) and the built-in fields.
- `schema`: The schema object for this field.
Expand Down
1 change: 1 addition & 0 deletions src/components/fields/SchemaField.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ function SchemaFieldRender(props) {
label,
hidden,
required,
disabled,
readonly,
displayLabel,
classNames,
Expand Down
40 changes: 40 additions & 0 deletions test/FieldTemplate_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from "react";

import { expect } from "chai";
import { createFormComponent, createSandbox } from "./test_utils";

describe("FieldTemplate", () => {
let sandbox;

beforeEach(() => {
sandbox = createSandbox();
});

afterEach(() => {
sandbox.restore();
});

describe("Custom FieldTemplate for disabled property", () => {
function FieldTemplate(props) {
return <div className={props.disabled ? "disabled" : "foo"} />;
}

it("should render with disabled when ui:disabled is truthy", () => {
const { node } = createFormComponent({
schema: { type: "string" },
uiSchema: { "ui:disabled": true },
FieldTemplate,
});
expect(node.querySelectorAll(".disabled")).to.have.length.of(1);
});

it("should render with disabled when ui:disabled is falsey", () => {
const { node } = createFormComponent({
schema: { type: "string" },
uiSchema: { "ui:disabled": false },
FieldTemplate,
});
expect(node.querySelectorAll(".disabled")).to.have.length.of(0);
});
});
});

0 comments on commit f4583f9

Please sign in to comment.