grafana/public/app/features/alerting/components/OptionElement.tsx
Peter Holmberg 400aafa3b3
Migration: Edit notification channel (#25980)
* implement edit page

* connectWithCleanup

* remove angular related code

* use loadingindicator

* use the correct loading component

* handle secureFields

* fixed implementation of secure fields

* Keep secureFields after rerendering the form

* CollapsableSection and Page refactor

* use checkbox instead of switch

* fix comment

* add cursor to section

* Fixed issues after PR review

* Fix issue with some settings being undefined

* new reducer and start with test

* algorithm to migrate secure fields

* UX: Minor UI Tweaks

* Added field around checkboxes, and missing required field

* fixed test

* tests for util

* minor tweaks and changes

* define as records

* fix typ error

* forward invalid to textarea and inputcontrol

* merge formdata and redux data in test

* fix issue with creating channel

* do not figure out securefields in migration

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-09-09 12:46:19 +02:00

59 lines
1.7 KiB
TypeScript

import React, { FC } from 'react';
import { FormAPI, Input, InputControl, Select, TextArea } from '@grafana/ui';
import { NotificationChannelOption } from '../../../types';
interface Props extends Pick<FormAPI<any>, 'register' | 'control'> {
option: NotificationChannelOption;
invalid?: boolean;
}
export const OptionElement: FC<Props> = ({ control, option, register, invalid }) => {
const modelValue = option.secure ? `secureSettings.${option.propertyName}` : `settings.${option.propertyName}`;
switch (option.element) {
case 'input':
return (
<Input
invalid={invalid}
type={option.inputType}
name={`${modelValue}`}
ref={register({
required: option.required ? 'Required' : false,
validate: v => (option.validationRule !== '' ? validateOption(v, option.validationRule) : true),
})}
placeholder={option.placeholder}
/>
);
case 'select':
return (
<InputControl
as={Select}
options={option.selectOptions}
control={control}
name={`${modelValue}`}
invalid={invalid}
/>
);
case 'textarea':
return (
<TextArea
invalid={invalid}
name={`${modelValue}`}
ref={register({
required: option.required ? 'Required' : false,
validate: v => (option.validationRule !== '' ? validateOption(v, option.validationRule) : true),
})}
/>
);
default:
console.error('Element not supported', option.element);
return null;
}
};
const validateOption = (value: string, validationRule: string) => {
return RegExp(validationRule).test(value) ? true : 'Invalid format';
};