mirror of
https://github.com/ansible/awx.git
synced 2026-03-20 18:37:39 -02:30
Gets rid of custom form reset function in favor of formik's resetForm in credential form
This commit is contained in:
@@ -12,14 +12,9 @@ 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({
|
function CredentialFormFields({ i18n, credentialTypes, formik }) {
|
||||||
i18n,
|
|
||||||
credentialTypes,
|
|
||||||
formik,
|
|
||||||
initialValues,
|
|
||||||
}) {
|
|
||||||
const [orgField, orgMeta, orgHelpers] = useField('organization');
|
const [orgField, orgMeta, orgHelpers] = useField('organization');
|
||||||
const [credTypeField, credTypeMeta, credTypeHelpers] = useField({
|
const [credTypeField, credTypeMeta] = 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),
|
||||||
});
|
});
|
||||||
@@ -34,45 +29,6 @@ function CredentialFormFields({
|
|||||||
})
|
})
|
||||||
.sort((a, b) => (a.label > b.label ? 1 : -1));
|
.sort((a, b) => (a.label > b.label ? 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
|
||||||
@@ -120,8 +76,16 @@ function CredentialFormFields({
|
|||||||
...credentialTypeOptions,
|
...credentialTypeOptions,
|
||||||
]}
|
]}
|
||||||
onChange={(event, value) => {
|
onChange={(event, value) => {
|
||||||
credTypeHelpers.setValue(value);
|
const { values, initialValues, resetForm } = formik;
|
||||||
resetSubFormFields(value, formik);
|
resetForm({
|
||||||
|
values: {
|
||||||
|
...initialValues,
|
||||||
|
name: values.name,
|
||||||
|
description: values.description,
|
||||||
|
organization: values.organization,
|
||||||
|
credential_type: value,
|
||||||
|
},
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
@@ -207,7 +171,6 @@ 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