Skip to content

Commit

Permalink
Added form readonly prop (rjsf-team#2552)
Browse files Browse the repository at this point in the history
Updated the playground to add the `Readonly whole form` flag
Added the `form-readonly` section in the documentation
Updated the tests to verify the `read-only` status
  • Loading branch information
heath-freenome committed Sep 24, 2021
1 parent 1332748 commit d0119f6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/api-reference/form-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ render((

If you just want to disable some of the fields, see the `ui:disabled` parameter in `uiSchema`.

## readonly

It's possible to make the whole form read-only by setting the `readonly` prop. The `readonly` prop is then forwarded down to each field of the form.

```jsx
const schema = {
type: "string"
};

render((
<Form schema={schema}
readonly />
), document.getElementById("app"));
```

If you just want to make some of the fields read-only, see the `ui:readonly` parameter in `uiSchema`.

## enctype

The value of this prop will be passed to the `enctype` [HTML attribute on the form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-enctype).
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default class Form extends Component {
noValidate: false,
liveValidate: false,
disabled: false,
readonly: false,
noHtml5Validate: false,
ErrorList: DefaultErrorList,
omitExtraData: false,
Expand Down Expand Up @@ -438,6 +439,7 @@ export default class Form extends Component {
acceptcharset,
noHtml5Validate,
disabled,
readonly,
formContext,
} = this.props;

Expand Down Expand Up @@ -484,6 +486,7 @@ export default class Form extends Component {
onFocus={this.onFocus}
registry={registry}
disabled={disabled}
readonly={readonly}
/>
{children ? (
children
Expand All @@ -504,6 +507,8 @@ if (process.env.NODE_ENV !== "production") {
schema: PropTypes.object.isRequired,
uiSchema: PropTypes.object,
formData: PropTypes.any,
disabled: PropTypes.bool,
readonly: PropTypes.bool,
widgets: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.func, PropTypes.object])
),
Expand Down
27 changes: 27 additions & 0 deletions packages/core/test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,33 @@ describeRepeated("Form common", createFormComponent => {
});
});

describe("Form readonly prop", () => {
const schema = {
type: "object",
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
};
const formData = { foo: "foo", bar: "bar" };

it("should enable all items", () => {
const { node } = createFormComponent({ schema, formData });

expect(node.querySelectorAll("input:disabled")).to.have.length.of(0);
});

it("should readonly all items", () => {
const { node } = createFormComponent({
schema,
formData,
readonly: true,
});

expect(node.querySelectorAll("input:read-only")).to.have.length.of(2);
});
});

describe("Attributes", () => {
const formProps = {
schema: {},
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const liveSettingsSchema = {
properties: {
validate: { type: "boolean", title: "Live validation" },
disable: { type: "boolean", title: "Disable whole form" },
readonly: { type: "boolean", title: "Readonly whole form" },
omitExtraData: { type: "boolean", title: "Omit extra data" },
liveOmit: { type: "boolean", title: "Live omit" },
},
Expand Down Expand Up @@ -355,6 +356,7 @@ class Playground extends Component {
liveSettings: {
validate: false,
disable: false,
readonly: false,
omitExtraData: false,
liveOmit: false,
},
Expand Down Expand Up @@ -599,6 +601,7 @@ class Playground extends Component {
{...templateProps}
liveValidate={liveSettings.validate}
disabled={liveSettings.disable}
readonly={liveSettings.readonly}
omitExtraData={liveSettings.omitExtraData}
liveOmit={liveSettings.liveOmit}
schema={schema}
Expand Down

0 comments on commit d0119f6

Please sign in to comment.