Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added form readonly prop (#2553) #2554

Merged
merged 5 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/core
- Fix for clearing errors after updating and submitting form (https://github.com/rjsf-team/react-jsonschema-form/pull/2536)
- bootstrap-4 TextWidget wrappers now pull from registry, add rootSchema to Registry, fix FieldProps.onFocus type to match WidgetProps (https://github.com/rjsf-team/react-jsonschema-form/pull/2519)
- Added global `readonly` flag to the `Form` (https://github.com/rjsf-team/react-jsonschema-form/pull/2554)

## @rjsf/bootstrap-4
- bootstrap-4 TextWidget wrappers now pull from registry, add rootSchema to Registry, fix FieldProps.onFocus type to match WidgetProps (https://github.com/rjsf-team/react-jsonschema-form/pull/2519)
Expand All @@ -30,6 +31,7 @@ should change the heading of the (upcoming) version to include a major version b

## Dev / docs / playground
- Several dependency updates
- Added global `readonly` flag to the `Form` (https://github.com/rjsf-team/react-jsonschema-form/pull/2554)

# v3.1.0

Expand Down
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 />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's better to call this "readOnly", for consistency with the readOnly react prop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that all the fields and widgets use readonly, I'd rather keep it consistent within the project. Perhaps we can deprecate readonly in favor of readOnly in the future?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sounds good. I'd note that we did already deprecate autocomplete in favor of autoComplete (see https://github.com/rjsf-team/react-jsonschema-form/blob/master/docs/api-reference/form-props.md#autocomplete), but I guess we don't have an analogous situation there (because autoComplete is not passed down as props to fields / widgets)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update types?

Done!

), 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
1 change: 1 addition & 0 deletions packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare module '@rjsf/core' {
className?: string;
customFormats?: { [k: string]: string | RegExp | ((data: string) => boolean) };
disabled?: boolean;
readonly?: boolean;
enctype?: string;
extraErrors?: any;
ErrorList?: React.StatelessComponent<ErrorListProps>;
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: "object", properties: { baz: { type: "string" } } },
},
};
const formData = { foo: "foo", bar: { baz: "baz" } };

it("should not have any readonly items", () => {
const { node } = createFormComponent({ schema, formData });

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

it("should readonly all items", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test just to ensure it works on nested objects / arrays?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure... I originally followed the pattern for the disabled tests above. Should those also be updated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the one test to use a nested object

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2021-09-24 at 4 53 46 PM

The playground works with arrays

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