diff --git a/app/frontend/src/javascript/components/authentication-provider/provider-form.tsx b/app/frontend/src/javascript/components/authentication-provider/provider-form.tsx index 5c3ae02ea..a85e1754f 100644 --- a/app/frontend/src/javascript/components/authentication-provider/provider-form.tsx +++ b/app/frontend/src/javascript/components/authentication-provider/provider-form.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { useForm, SubmitHandler } from 'react-hook-form'; import { react2angular } from 'react2angular'; import { AuthenticationProvider } from '../../models/authentication-provider'; @@ -32,6 +32,7 @@ type selectProvidableTypeOption = { value: string, label: string }; */ export const ProviderForm: React.FC = ({ action, provider, onError, onSuccess }) => { const { handleSubmit, register, control } = useForm({ defaultValues: { ...provider } }); + const [providableType, setProvidableType] = useState(provider?.providable_type); const { t } = useTranslation('shared'); /** @@ -54,11 +55,28 @@ export const ProviderForm: React.FC = ({ action, provider, on }); }; + /** + * Callback triggered when the providable type is changed. + * Changing the providable type will change the form to match the new type. + */ + const onProvidableTypeChange = (type: string) => { + setProvidableType(type); + }; + return (
- - - {provider?.providable_type === 'OAuth2Provider' && } + + + {providableType === 'OAuth2Provider' && } ); diff --git a/app/frontend/src/javascript/components/form/form-multi-select.tsx b/app/frontend/src/javascript/components/form/form-multi-select.tsx index e97d49318..427108be1 100644 --- a/app/frontend/src/javascript/components/form/form-multi-select.tsx +++ b/app/frontend/src/javascript/components/form/form-multi-select.tsx @@ -1,4 +1,4 @@ -import React, { SelectHTMLAttributes } from 'react'; +import React from 'react'; import Select from 'react-select'; import { Controller, Path } from 'react-hook-form'; import { FieldValues } from 'react-hook-form/dist/types/fields'; @@ -6,11 +6,15 @@ import { FieldPath } from 'react-hook-form/dist/types/path'; import { FieldPathValue, UnpackNestedValue } from 'react-hook-form/dist/types'; import { FormControlledComponent } from '../../models/form-component'; -interface FormSelectProps extends SelectHTMLAttributes, FormControlledComponent { +interface FormSelectProps extends FormControlledComponent { id: string, label?: string, options: Array>, valuesDefault?: Array, + onChange?: (values: Array) => void, + className?: string, + placeholder?: string, + disabled?: boolean, } /** @@ -23,13 +27,23 @@ type selectOption = { value: TOptionValue, label: string }; * This component is a wrapper around react-select to use with react-hook-form. * It is a multi-select component. */ -export const FormMultiSelect = ({ id, label, className, control, placeholder, options, valuesDefault, error, rules, disabled }: FormSelectProps) => { +export const FormMultiSelect = ({ id, label, className, control, placeholder, options, valuesDefault, error, rules, disabled, onChange }: FormSelectProps) => { const classNames = ` form-item ${className || ''} ${error && error[id] ? 'is-incorrect' : ''} ${rules && rules.required ? 'is-required' : ''} ${disabled ? 'is-disabled' : ''}`; + /** + * The following callback will trigger the onChange callback, if it was passed to this component, + * when the selected option changes. + */ + const onChangeCb = (newValues: Array): void => { + if (typeof onChange === 'function') { + onChange(newValues); + } + }; + return ( ); diff --git a/app/frontend/src/javascript/components/form/form-select.tsx b/app/frontend/src/javascript/components/form/form-select.tsx index c65045a2f..d4ba82ff2 100644 --- a/app/frontend/src/javascript/components/form/form-select.tsx +++ b/app/frontend/src/javascript/components/form/form-select.tsx @@ -1,4 +1,4 @@ -import React, { SelectHTMLAttributes } from 'react'; +import React from 'react'; import Select from 'react-select'; import { Controller, Path } from 'react-hook-form'; import { FieldValues } from 'react-hook-form/dist/types/fields'; @@ -6,11 +6,15 @@ import { FieldPath } from 'react-hook-form/dist/types/path'; import { FieldPathValue, UnpackNestedValue } from 'react-hook-form/dist/types'; import { FormControlledComponent } from '../../models/form-component'; -interface FormSelectProps extends SelectHTMLAttributes, FormControlledComponent { +interface FormSelectProps extends FormControlledComponent { id: string, label?: string, options: Array>, valueDefault?: TOptionValue, + onChange?: (value: TOptionValue) => void, + className?: string, + placeholder?: string, + disabled?: boolean, } /** @@ -22,13 +26,23 @@ type selectOption = { value: TOptionValue, label: string }; /** * This component is a wrapper for react-select to use with react-hook-form */ -export const FormSelect = ({ id, label, className, control, placeholder, options, valueDefault, error, rules, disabled }: FormSelectProps) => { +export const FormSelect = ({ id, label, className, control, placeholder, options, valueDefault, error, rules, disabled, onChange }: FormSelectProps) => { const classNames = ` form-item ${className || ''} ${error && error[id] ? 'is-incorrect' : ''} ${rules && rules.required ? 'is-required' : ''} ${disabled ? 'is-disabled' : ''}`; + /** + * The following callback will trigger the onChange callback, if it was passed to this component, + * when the selected option changes. + */ + const onChangeCb = (newValue: TOptionValue): void => { + if (typeof onChange === 'function') { + onChange(newValue); + } + }; + return ( );