mirror of
https://github.com/ansible/awx.git
synced 2026-03-18 09:27:31 -02:30
Revert "Gets rid of custom form reset function in favor of formik's resetForm in credential form"
This reverts commit 26fa94dc6f1f1c1306c32ba571eb2899e516b0a0.
This commit is contained in:
@@ -12,9 +12,14 @@ import OrganizationLookup from '../../../components/Lookup/OrganizationLookup';
|
|||||||
import { FormColumnLayout } from '../../../components/FormLayout';
|
import { FormColumnLayout } from '../../../components/FormLayout';
|
||||||
import TypeInputsSubForm from './TypeInputsSubForm';
|
import TypeInputsSubForm from './TypeInputsSubForm';
|
||||||
|
|
||||||
function CredentialFormFields({ i18n, credentialTypes, formik }) {
|
function CredentialFormFields({
|
||||||
|
i18n,
|
||||||
|
credentialTypes,
|
||||||
|
formik,
|
||||||
|
initialValues,
|
||||||
|
}) {
|
||||||
const [orgField, orgMeta, orgHelpers] = useField('organization');
|
const [orgField, orgMeta, orgHelpers] = useField('organization');
|
||||||
const [credTypeField, credTypeMeta] = useField({
|
const [credTypeField, credTypeMeta, credTypeHelpers] = useField({
|
||||||
name: 'credential_type',
|
name: 'credential_type',
|
||||||
validate: required(i18n._(t`Select a value for this field`), i18n),
|
validate: required(i18n._(t`Select a value for this field`), i18n),
|
||||||
});
|
});
|
||||||
@@ -29,6 +34,45 @@ function CredentialFormFields({ i18n, credentialTypes, formik }) {
|
|||||||
})
|
})
|
||||||
.sort((a, b) => (a.label.toLowerCase() > b.label.toLowerCase() ? 1 : -1));
|
.sort((a, b) => (a.label.toLowerCase() > b.label.toLowerCase() ? 1 : -1));
|
||||||
|
|
||||||
|
const resetSubFormFields = (newCredentialType, form) => {
|
||||||
|
const fields = credentialTypes[newCredentialType].inputs.fields || [];
|
||||||
|
fields.forEach(
|
||||||
|
({ ask_at_runtime, type, id, choices, default: defaultValue }) => {
|
||||||
|
if (
|
||||||
|
parseInt(newCredentialType, 10) === form.initialValues.credential_type
|
||||||
|
) {
|
||||||
|
form.setFieldValue(`inputs.${id}`, initialValues.inputs[id]);
|
||||||
|
if (ask_at_runtime) {
|
||||||
|
form.setFieldValue(
|
||||||
|
`passwordPrompts.${id}`,
|
||||||
|
initialValues.passwordPrompts[id]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (type) {
|
||||||
|
case 'string':
|
||||||
|
form.setFieldValue(`inputs.${id}`, defaultValue || '');
|
||||||
|
break;
|
||||||
|
case 'boolean':
|
||||||
|
form.setFieldValue(`inputs.${id}`, defaultValue || false);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (choices) {
|
||||||
|
form.setFieldValue(`inputs.${id}`, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ask_at_runtime) {
|
||||||
|
form.setFieldValue(`passwordPrompts.${id}`, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
form.setFieldTouched(`inputs.${id}`, false);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<FormField
|
<FormField
|
||||||
@@ -76,16 +120,8 @@ function CredentialFormFields({ i18n, credentialTypes, formik }) {
|
|||||||
...credentialTypeOptions,
|
...credentialTypeOptions,
|
||||||
]}
|
]}
|
||||||
onChange={(event, value) => {
|
onChange={(event, value) => {
|
||||||
const { values, initialValues, resetForm } = formik;
|
credTypeHelpers.setValue(value);
|
||||||
resetForm({
|
resetSubFormFields(value, formik);
|
||||||
values: {
|
|
||||||
...initialValues,
|
|
||||||
name: values.name,
|
|
||||||
description: values.description,
|
|
||||||
organization: values.organization,
|
|
||||||
credential_type: value,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
@@ -171,6 +207,7 @@ function CredentialForm({
|
|||||||
<FormColumnLayout>
|
<FormColumnLayout>
|
||||||
<CredentialFormFields
|
<CredentialFormFields
|
||||||
formik={formik}
|
formik={formik}
|
||||||
|
initialValues={initialValues}
|
||||||
credentialTypes={credentialTypes}
|
credentialTypes={credentialTypes}
|
||||||
{...rest}
|
{...rest}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user