Skip to content

Commit 2cc5126

Browse files
authored
Bug: The oneOf selector can be modified in readonly mode (#4463)
* Fixed issue with oneOf selector can be modified in readonly mode * update based on feedback
1 parent 5a3537e commit 2cc5126

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b
2020
## @rjsf/utils
2121

2222
- switch `lodash.isEqualWith` to `fast-equals.createCustomEqual` providing `areFunctionsEqual` assuming any functions are equal.
23+
- Fixed issue with oneOf selector can be modified in readonly mode, fixing [#4460](https://github.com/rjsf-team/react-jsonschema-form/issues/4460)
2324
- Fixed issue with fields inside an array can't be set to empty when a default is set, fixing [#4456](https://github.com/rjsf-team/react-jsonschema-form/issues/4456)
2425
- Fixed issue with file accept attribute, fixing [#4404](https://github.com/rjsf-team/react-jsonschema-form/issues/4404).
2526

packages/core/src/components/fields/MultiSchemaField.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
149149
formContext,
150150
onBlur,
151151
onFocus,
152+
readonly,
152153
registry,
153154
schema,
154155
uiSchema,
@@ -237,6 +238,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
237238
autofocus={autofocus}
238239
label={title ?? name}
239240
hideLabel={!displayLabel}
241+
readonly={readonly}
240242
/>
241243
</div>
242244
{optionSchema && <_SchemaField {...this.props} schema={optionSchema} uiSchema={optionUiSchema} />}

packages/core/test/oneOf.test.jsx

+74
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,80 @@ describe('oneOf', () => {
891891
});
892892
});
893893

894+
it('should select oneOf dropdown be disabled when the schema is readOnly', () => {
895+
const schema = {
896+
title: 'Example Schema',
897+
type: 'object',
898+
readOnly: true,
899+
properties: {
900+
contactPreference: {
901+
oneOf: [
902+
{
903+
$ref: '#/definitions/phoneContact',
904+
},
905+
{
906+
$ref: '#/definitions/emailContact',
907+
},
908+
],
909+
},
910+
},
911+
required: ['contactPreference'],
912+
definitions: {
913+
phoneContact: {
914+
type: 'object',
915+
properties: {
916+
contactMethod: {
917+
type: 'string',
918+
enum: ['phone'],
919+
},
920+
phoneNumber: {
921+
type: 'string',
922+
pattern: '^[0-9]{10}$',
923+
},
924+
},
925+
required: ['contactMethod', 'phoneNumber'],
926+
},
927+
emailContact: {
928+
type: 'object',
929+
properties: {
930+
contactMethod: {
931+
type: 'string',
932+
enum: ['email'],
933+
},
934+
emailAddress: {
935+
type: 'string',
936+
format: 'email',
937+
},
938+
},
939+
required: ['contactMethod', 'emailAddress'],
940+
},
941+
},
942+
};
943+
944+
const { node } = createFormComponent({
945+
schema,
946+
formData: {
947+
contactPreference: {
948+
contactMethod: 'phone',
949+
phoneNumber: '1231231231',
950+
},
951+
},
952+
});
953+
954+
const $select = node.querySelector('select#root_contactPreference__oneof_select');
955+
956+
expect($select.value).eql('0');
957+
expect($select).to.have.property('disabled', true);
958+
959+
act(() => {
960+
fireEvent.change($select, {
961+
target: { value: $select.options[1].value },
962+
});
963+
});
964+
965+
expect($select.value).eql('0');
966+
});
967+
894968
describe('Arrays', () => {
895969
it('should correctly render mixed types for oneOf inside array items', () => {
896970
const schema = {

0 commit comments

Comments
 (0)