Skip to content

Commit

Permalink
[#4267] Auto select Objects API group if only one available
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jun 18, 2024
1 parent 39c46dc commit 9a244d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types';
import React, {useContext} from 'react';
import React, {useContext, useEffect} from 'react';
import {useIntl} from 'react-intl';

import {CustomFieldTemplate} from 'components/admin/RJSFWrapper';
Expand Down Expand Up @@ -32,6 +32,14 @@ const LegacyConfigFields = ({index, name, schema, formData, onChange}) => {
return rawErrors ? getErrorMarkup(rawErrors) : null;
};

useEffect(() => {
if (schema.properties.objectsApiGroup.enum.length === 1) {
onChange({
target: {name: 'objectsApiGroup', value: schema.properties.objectsApiGroup.enum[0]},
});
}
}, []);

return (
<>
<CustomFieldTemplate
Expand All @@ -44,17 +52,17 @@ const LegacyConfigFields = ({index, name, schema, formData, onChange}) => {
description: 'Objects API group selection',
defaultMessage: 'Which Objects API group to use.',
})}
rawErrors={null}
errors={null}
rawErrors={getFieldErrors(name, index, validationErrors, 'objectsApiGroup')}
errors={buildErrorsComponent('objectsApiGroup')}
displayLabel
required
>
<Select
id="root_objectsApiGroup"
name="objectsApiGroup"
choices={getChoicesFromSchema(
schema?.properties?.objectsApiGroup?.enum,
schema?.properties?.objectsApiGroup?.enumNames
schema.properties.objectsApiGroup.enum,
schema.properties.objectsApiGroup.enumNames
)}
value={objectsApiGroup}
onChange={onChange}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types';
import React, {useContext} from 'react';
import React, {useContext, useEffect} from 'react';
import {FormattedMessage, useIntl} from 'react-intl';

import {CustomFieldTemplate} from 'components/admin/RJSFWrapper';
Expand Down Expand Up @@ -49,6 +49,14 @@ const V2ConfigFields = ({index, name, schema, formData, onChange}) => {
);
}

useEffect(() => {
if (schema.properties.objectsApiGroup.enum.length === 1) {
onChange({
target: {name: 'objectsApiGroup', value: schema.properties.objectsApiGroup.enum[0]},
});
}
}, []);

return (
<>
<CustomFieldTemplate
Expand All @@ -61,8 +69,8 @@ const V2ConfigFields = ({index, name, schema, formData, onChange}) => {
description: 'Objects API group selection',
defaultMessage: 'Which Objects API group to use.',
})}
rawErrors={null}
errors={null}
rawErrors={getFieldErrors(name, index, validationErrors, 'objectsApiGroup')}
errors={buildErrorsComponent('objectsApiGroup')}
displayLabel
>
<Select
Expand Down

0 comments on commit 9a244d8

Please sign in to comment.