From 6be3a4c324c5673905d829ec46ae56de8be93da6 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 27 Apr 2022 15:36:36 +0200 Subject: [PATCH] (ui) stylized form-switch --- .../components/form/form-multi-select.tsx | 2 +- .../components/form/form-select.tsx | 2 +- .../components/form/form-switch.tsx | 26 ++++++++++++++++--- .../components/user/user-profile-form.tsx | 12 +++++---- .../stylesheets/modules/form/form-item.scss | 2 +- .../stylesheets/modules/form/form-switch.scss | 25 ++++++++++++++++++ config/locales/app.shared.en.yml | 3 ++- 7 files changed, 60 insertions(+), 12 deletions(-) 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 1171f16cc..c3d5d0537 100644 --- a/app/frontend/src/javascript/components/form/form-multi-select.tsx +++ b/app/frontend/src/javascript/components/form/form-multi-select.tsx @@ -37,7 +37,7 @@ export const FormMultiSelect = } diff --git a/app/frontend/src/javascript/components/form/form-select.tsx b/app/frontend/src/javascript/components/form/form-select.tsx index 3220190f9..7cdb6f108 100644 --- a/app/frontend/src/javascript/components/form/form-select.tsx +++ b/app/frontend/src/javascript/components/form/form-select.tsx @@ -37,7 +37,7 @@ export const FormSelect = } diff --git a/app/frontend/src/javascript/components/form/form-switch.tsx b/app/frontend/src/javascript/components/form/form-switch.tsx index 91bb9a0af..d46a09752 100644 --- a/app/frontend/src/javascript/components/form/form-switch.tsx +++ b/app/frontend/src/javascript/components/form/form-switch.tsx @@ -8,22 +8,42 @@ import { AbstractFormItem, AbstractFormItemProps } from './abstract-form-item'; interface FormSwitchProps extends FormControlledComponent, AbstractFormItemProps { defaultValue?: boolean, + onChange?: (value: boolean) => void, } /** * This component is a wrapper for react-switch, to use with react-hook-form. */ -export const FormSwitch = ({ id, label, tooltip, className, error, rules, disabled, control, defaultValue, formState, readOnly, warning }: FormSwitchProps) => { +export const FormSwitch = ({ id, label, tooltip, className, error, rules, disabled, control, defaultValue, formState, readOnly, warning, onChange }: FormSwitchProps) => { + /** + * The following callback will trigger the onChange callback, if it was passed to this component, + * when the selected option changes. + */ + const onChangeCb = (newValue: boolean): void => { + if (typeof onChange === 'function') { + onChange(newValue); + } + }; + return ( } control={control} defaultValue={defaultValue as UnpackNestedValue>>} render={({ field: { onChange, value, ref } }) => - + { + onChange(val); + onChangeCb(val); + }} + checked={value as boolean} + height={19} + width={40} + ref={ref} + disabled={disabled} + readOnly={readOnly} /> } /> ); diff --git a/app/frontend/src/javascript/components/user/user-profile-form.tsx b/app/frontend/src/javascript/components/user/user-profile-form.tsx index 398294cb9..7c4122a22 100644 --- a/app/frontend/src/javascript/components/user/user-profile-form.tsx +++ b/app/frontend/src/javascript/components/user/user-profile-form.tsx @@ -9,8 +9,8 @@ import { useTranslation } from 'react-i18next'; import { Avatar } from './avatar'; import { GenderInput } from './gender-input'; import { ChangePassword } from './change-password'; -import Switch from 'react-switch'; import { PasswordInput } from './password-input'; +import { FormSwitch } from '../form/form-switch'; declare const Application: IApplication; @@ -106,10 +106,12 @@ export const UserProfileForm: React.FC = ({ action, size,

{t('app.shared.user_profile_form.organization_data')}

- + {isOrganization &&