Fix showWhen failure (#55337)

When a contact point configuration field has `showWhen` set on it, the contact point form was not correctly displaying. This change fixes that so that forms display correctly.
This commit is contained in:
aimuz 2022-09-29 15:00:49 +08:00 committed by GitHub
parent 5a9dfd7173
commit bf07deb992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -3337,11 +3337,9 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
],
"public/app/features/alerting/unified/components/receivers/form/ChannelOptions.tsx:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"],
[0, 0, 0, "Do not use any type assertions.", "3"],
[0, 0, 0, "Unexpected any. Specify a different type.", "4"]
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"]
],
"public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],

View File

@ -29,14 +29,17 @@ export function ChannelOptions<R extends ChannelValues>({
readOnly = false,
}: Props<R>): JSX.Element {
const { watch } = useFormContext<ReceiverFormValues<R>>();
const currentFormValues = watch() as Record<string, any>; // react hook form types ARE LYING!
const currentFormValues = watch(); // react hook form types ARE LYING!
return (
<>
{selectedChannelOptions.map((option: NotificationChannelOption, index: number) => {
const key = `${option.label}-${index}`;
// Some options can be dependent on other options, this determines what is selected in the dependency options
// I think this needs more thought.
const selectedOptionValue = currentFormValues[`${pathPrefix}settings.${option.showWhen.field}`];
// pathPrefix = items.index.
const paths = pathPrefix.split('.');
const selectedOptionValue =
paths.length >= 2 ? currentFormValues.items[Number(paths[1])].settings[option.showWhen.field] : undefined;
if (option.showWhen.field && selectedOptionValue !== option.showWhen.is) {
return null;