From 72b429321553841ac1ff48cf974bfc65da01bb06 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 19 Dec 2022 12:23:39 -0300 Subject: pretty --- .../src/components/form/FormProvider.tsx | 76 ++++--- .../src/components/form/Input.tsx | 118 +++++++---- .../src/components/form/InputArray.tsx | 156 +++++++++----- .../src/components/form/InputBoolean.tsx | 89 ++++---- .../src/components/form/InputCurrency.tsx | 51 +++-- .../src/components/form/InputDuration.tsx | 2 +- .../src/components/form/InputGroup.tsx | 82 +++++--- .../src/components/form/InputImage.tsx | 144 +++++++------ .../src/components/form/InputLocation.tsx | 51 +++-- .../src/components/form/InputNumber.tsx | 44 ++-- .../src/components/form/InputPayto.tsx | 41 ++-- .../src/components/form/InputSearchProduct.tsx | 199 +++++++++++------- .../src/components/form/InputSecured.stories.tsx | 56 ++--- .../src/components/form/InputSecured.tsx | 222 +++++++++++++------- .../src/components/form/InputStock.tsx | 232 +++++++++++++-------- .../src/components/form/InputTaxes.tsx | 119 +++++++---- .../src/components/form/InputWithAddon.tsx | 118 +++++++---- .../src/components/form/TextField.tsx | 64 ++++-- .../src/components/form/useField.tsx | 65 +++--- .../src/components/form/useGroupField.tsx | 17 +- 20 files changed, 1230 insertions(+), 716 deletions(-) (limited to 'packages/merchant-backoffice-ui/src/components/form') diff --git a/packages/merchant-backoffice-ui/src/components/form/FormProvider.tsx b/packages/merchant-backoffice-ui/src/components/form/FormProvider.tsx index ab32b6bed..7bcebd706 100644 --- a/packages/merchant-backoffice-ui/src/components/form/FormProvider.tsx +++ b/packages/merchant-backoffice-ui/src/components/form/FormProvider.tsx @@ -15,37 +15,59 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ import { ComponentChildren, createContext, h, VNode } from "preact"; import { useContext, useMemo } from "preact/hooks"; -type Updater = (value: ((prevState: S) => S) ) => void; +type Updater = (value: (prevState: S) => S) => void; export interface Props { object?: Partial; errors?: FormErrors; name?: string; valueHandler: Updater> | null; - children: ComponentChildren + children: ComponentChildren; } -const noUpdater: Updater> = () => (s: unknown) => s +const noUpdater: Updater> = () => (s: unknown) => s; -export function FormProvider({ object = {}, errors = {}, name = '', valueHandler, children }: Props): VNode { +export function FormProvider({ + object = {}, + errors = {}, + name = "", + valueHandler, + children, +}: Props): VNode { const initialObject = useMemo(() => object, []); - const value = useMemo>(() => ({ errors, object, initialObject, valueHandler: valueHandler ? valueHandler : noUpdater, name, toStr: {}, fromStr: {} }), [errors, object, valueHandler]); - - return -
{ - e.preventDefault(); - // if (valueHandler) valueHandler(object); - }}> - {children} -
-
; + const value = useMemo>( + () => ({ + errors, + object, + initialObject, + valueHandler: valueHandler ? valueHandler : noUpdater, + name, + toStr: {}, + fromStr: {}, + }), + [errors, object, valueHandler], + ); + + return ( + +
{ + e.preventDefault(); + // if (valueHandler) valueHandler(object); + }} + > + {children} +
+
+ ); } export interface FormType { @@ -58,24 +80,24 @@ export interface FormType { valueHandler: Updater>; } -const FormContext = createContext>(null!) +const FormContext = createContext>(null!); export function useFormContext() { - return useContext>(FormContext) + return useContext>(FormContext); } export type FormErrors = { - [P in keyof T]?: string | FormErrors -} + [P in keyof T]?: string | FormErrors; +}; export type FormtoStr = { - [P in keyof T]?: ((f?: T[P]) => string) -} + [P in keyof T]?: (f?: T[P]) => string; +}; export type FormfromStr = { - [P in keyof T]?: ((f: string) => T[P]) -} + [P in keyof T]?: (f: string) => T[P]; +}; export type FormUpdater = { - [P in keyof T]?: (f: keyof T) => (v: T[P]) => void -} + [P in keyof T]?: (f: keyof T) => (v: T[P]) => void; +}; diff --git a/packages/merchant-backoffice-ui/src/components/form/Input.tsx b/packages/merchant-backoffice-ui/src/components/form/Input.tsx index 793477f3d..54140ba4d 100644 --- a/packages/merchant-backoffice-ui/src/components/form/Input.tsx +++ b/packages/merchant-backoffice-ui/src/components/form/Input.tsx @@ -15,57 +15,101 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ import { ComponentChildren, h, VNode } from "preact"; import { useField, InputProps } from "./useField.js"; interface Props extends InputProps { - inputType?: 'text' | 'number' | 'multiline' | 'password'; + inputType?: "text" | "number" | "multiline" | "password"; expand?: boolean; toStr?: (v?: any) => string; fromStr?: (s: string) => any; - inputExtra?: any, + inputExtra?: any; side?: ComponentChildren; children?: ComponentChildren; } -const defaultToString = (f?: any): string => f || '' -const defaultFromString = (v: string): any => v as any +const defaultToString = (f?: any): string => f || ""; +const defaultFromString = (v: string): any => v as any; -const TextInput = ({ inputType, error, ...rest }: any) => inputType === 'multiline' ? -