From 08ca016f657a0b15e0d8259190260e2badda0a78 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Tue, 31 Oct 2023 12:12:24 +0530 Subject: [PATCH] fix: custom theme form validations (#2565) --- .../ui/src/form-fields/input-color-picker.tsx | 58 +++-- .../core/theme/custom-theme-selector.tsx | 222 +++++++++++------- 2 files changed, 175 insertions(+), 105 deletions(-) diff --git a/packages/ui/src/form-fields/input-color-picker.tsx b/packages/ui/src/form-fields/input-color-picker.tsx index 738d0e0c8..0a91a8838 100644 --- a/packages/ui/src/form-fields/input-color-picker.tsx +++ b/packages/ui/src/form-fields/input-color-picker.tsx @@ -11,12 +11,14 @@ export interface InputColorPickerProps { value: string | undefined; onChange: (value: string) => void; name: string; - className: string; + className?: string; + style?: React.CSSProperties; placeholder: string; } export const InputColorPicker: React.FC = (props) => { - const { value, hasError, onChange, name, className, placeholder } = props; + const { value, hasError, onChange, name, className, style, placeholder } = + props; const [referenceElement, setReferenceElement] = React.useState(null); @@ -32,12 +34,12 @@ export const InputColorPicker: React.FC = (props) => { onChange(hex); }; - const handleInputChange = (value: any) => { - onChange(value); + const handleInputChange = (e: React.ChangeEvent) => { + onChange(e.target.value); }; return ( -
+
= (props) => { onChange={handleInputChange} hasError={hasError} placeholder={placeholder} - className={`border-none ${className}`} + className={`border-[0.5px] border-custom-border-200 ${className}`} + style={style} /> - + {({ open }) => { if (open) { } @@ -60,26 +66,26 @@ export const InputColorPicker: React.FC = (props) => { ref={setReferenceElement} variant="neutral-primary" size="sm" - className="border-none !p-1.5" + className="border-none !bg-transparent" > - {value && value !== "" ? ( - - ) : ( - - - - )} + + + + + + + = observer(() => { +export const CustomThemeSelector: React.FC = observer(() => { const { user: userStore } = useMobxStore(); const userTheme = userStore?.currentUser?.theme; // hooks const { setTheme } = useTheme(); const { + control, formState: { errors, isSubmitting }, handleSubmit, - control, + watch, } = useForm({ defaultValues: { background: userTheme?.background !== "" ? userTheme?.background : "#0d101b", @@ -51,100 +64,151 @@ export const CustomThemeSelector: FC = observer(() => { return userStore.updateCurrentUser({ theme: payload }); }; + const handleValueChange = (val: string | undefined, onChange: any) => { + let hex = val; + + // prepend a hashtag if it doesn't exist + if (val && val[0] !== "#") hex = `#${val}`; + + onChange(hex); + }; + return (

Customize your theme

-
+

Background color

- ( - - )} - /> +
+ ( + handleValueChange(val, onChange)} + placeholder="#0d101b" + className="w-full" + style={{ + backgroundColor: value, + color: watch("text"), + }} + hasError={Boolean(errors?.background)} + /> + )} + /> + {errors.background &&

{errors.background.message}

} +

Text color

- ( - - )} - /> +
+ ( + handleValueChange(val, onChange)} + placeholder="#c5c5c5" + className="w-full" + style={{ + backgroundColor: watch("background"), + color: value, + }} + hasError={Boolean(errors?.text)} + /> + )} + /> + {errors.text &&

{errors.text.message}

} +

Primary(Theme) color

- ( - - )} - /> +
+ ( + handleValueChange(val, onChange)} + placeholder="#3f76ff" + className="w-full" + style={{ + backgroundColor: value, + color: watch("text"), + }} + hasError={Boolean(errors?.primary)} + /> + )} + /> + {errors.primary &&

{errors.primary.message}

} +

Sidebar background color

- ( - +
+ ( + handleValueChange(val, onChange)} + placeholder="#0d101b" + className="w-full" + style={{ + backgroundColor: value, + color: watch("sidebarText"), + }} + hasError={Boolean(errors?.sidebarBackground)} + /> + )} + /> + {errors.sidebarBackground && ( +

{errors.sidebarBackground.message}

)} - /> +

Sidebar text color

- ( - - )} - /> +
+ ( + handleValueChange(val, onChange)} + placeholder="#c5c5c5" + className="w-full" + style={{ + backgroundColor: watch("sidebarBackground"), + color: value, + }} + hasError={Boolean(errors?.sidebarText)} + /> + )} + /> + {errors.sidebarText &&

{errors.sidebarText.message}

} +