Skip to content

Commit

Permalink
Fixed issue with oneOf selector can be modified in readonly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
abdalla-rko committed Jan 23, 2025
1 parent 2b5c907 commit 980bc39
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/utils

- switch `lodash.isEqualWith` to `fast-equals.createCustomEqual` providing `areFunctionsEqual` assuming any functions are equal.
- Fixed issue with oneOf selector can be modified in readonly mode, fixing [#4460](https://github.com/rjsf-team/react-jsonschema-form/issues/4460)

# 5.24.1

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/components/fields/MultiSchemaField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
autofocus={autofocus}
label={title ?? name}
hideLabel={!displayLabel}
readonly={this.props.readonly}
/>
</div>
{optionSchema && <_SchemaField {...this.props} schema={optionSchema} uiSchema={optionUiSchema} />}
Expand Down
74 changes: 74 additions & 0 deletions packages/core/test/oneOf.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,80 @@ describe('oneOf', () => {
});
});

it('should select oneOf dropdown be disabled when the schema is readOnly', () => {
const schema = {
title: 'Example Schema',
type: 'object',
readOnly: true,
properties: {
contactPreference: {
oneOf: [
{
$ref: '#/definitions/phoneContact',
},
{
$ref: '#/definitions/emailContact',
},
],
},
},
required: ['contactPreference'],
definitions: {
phoneContact: {
type: 'object',
properties: {
contactMethod: {
type: 'string',
enum: ['phone'],
},
phoneNumber: {
type: 'string',
pattern: '^[0-9]{10}$',
},
},
required: ['contactMethod', 'phoneNumber'],
},
emailContact: {
type: 'object',
properties: {
contactMethod: {
type: 'string',
enum: ['email'],
},
emailAddress: {
type: 'string',
format: 'email',
},
},
required: ['contactMethod', 'emailAddress'],
},
},
};

const { node } = createFormComponent({
schema,
formData: {
contactPreference: {
contactMethod: 'phone',
phoneNumber: '1231231231',
},
},
});

const $select = node.querySelector('select#root_contactPreference__oneof_select');

expect($select.value).eql('0');
expect($select).to.have.property('disabled', true);

act(() => {
fireEvent.change($select, {
target: { value: $select.options[1].value },
});
});

expect($select.value).eql('0');
});

describe('Arrays', () => {
it('should correctly render mixed types for oneOf inside array items', () => {
const schema = {
Expand Down

0 comments on commit 980bc39

Please sign in to comment.