From 3e060b80428943c6562250a6ff77eff10a0259b7 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 24 Oct 2022 10:46:14 +0200 Subject: repo: integrate packages from former merchant-backoffice.git --- .../src/components/form/FormProvider.tsx | 81 +++++ .../src/components/form/Input.tsx | 71 ++++ .../src/components/form/InputArray.tsx | 97 +++++ .../src/components/form/InputBoolean.tsx | 72 ++++ .../src/components/form/InputCurrency.tsx | 47 +++ .../src/components/form/InputDate.tsx | 159 +++++++++ .../src/components/form/InputDuration.tsx | 172 +++++++++ .../src/components/form/InputGroup.tsx | 66 ++++ .../src/components/form/InputImage.tsx | 95 +++++ .../src/components/form/InputLocation.tsx | 43 +++ .../src/components/form/InputNumber.tsx | 42 +++ .../src/components/form/InputPayto.tsx | 39 ++ .../src/components/form/InputPaytoForm.tsx | 392 +++++++++++++++++++++ .../src/components/form/InputSearchProduct.tsx | 139 ++++++++ .../src/components/form/InputSecured.stories.tsx | 55 +++ .../src/components/form/InputSecured.tsx | 119 +++++++ .../src/components/form/InputSelector.tsx | 86 +++++ .../src/components/form/InputStock.stories.tsx | 162 +++++++++ .../src/components/form/InputStock.tsx | 171 +++++++++ .../src/components/form/InputTaxes.tsx | 97 +++++ .../src/components/form/InputWithAddon.tsx | 77 ++++ .../src/components/form/TextField.tsx | 53 +++ .../src/components/form/useField.tsx | 86 +++++ .../src/components/form/useGroupField.tsx | 40 +++ 24 files changed, 2461 insertions(+) create mode 100644 packages/merchant-backoffice-ui/src/components/form/FormProvider.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/Input.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputArray.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputBoolean.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputCurrency.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputDate.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputDuration.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputGroup.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputImage.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputLocation.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputNumber.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputPayto.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputPaytoForm.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputSearchProduct.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputSecured.stories.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputSecured.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputSelector.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputStock.stories.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputStock.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputTaxes.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/InputWithAddon.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/TextField.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/useField.tsx create mode 100644 packages/merchant-backoffice-ui/src/components/form/useGroupField.tsx (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 new file mode 100644 index 000000000..aef410ce7 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/components/form/FormProvider.tsx @@ -0,0 +1,81 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** +* +* @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; + +export interface Props { + object?: Partial; + errors?: FormErrors; + name?: string; + valueHandler: Updater> | null; + children: ComponentChildren +} + +const noUpdater: Updater> = () => (s: unknown) => s + +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} +
+
; +} + +export interface FormType { + object: Partial; + initialObject: Partial; + errors: FormErrors; + toStr: FormtoStr; + name: string; + fromStr: FormfromStr; + valueHandler: Updater>; +} + +const FormContext = createContext>(null!) + +export function useFormContext() { + return useContext>(FormContext) +} + +export type FormErrors = { + [P in keyof T]?: string | FormErrors +} + +export type FormtoStr = { + [P in keyof T]?: ((f?: T[P]) => string) +} + +export type FormfromStr = { + [P in keyof T]?: ((f: string) => T[P]) +} + +export type FormUpdater = { + [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 new file mode 100644 index 000000000..9a9691e9b --- /dev/null +++ b/packages/merchant-backoffice-ui/src/components/form/Input.tsx @@ -0,0 +1,71 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** +* +* @author Sebastian Javier Marchano (sebasjm) +*/ +import { ComponentChildren, h, VNode } from "preact"; +import { useField, InputProps } from "./useField"; + +interface Props extends InputProps { + inputType?: 'text' | 'number' | 'multiline' | 'password'; + expand?: boolean; + toStr?: (v?: any) => string; + fromStr?: (s: string) => any; + inputExtra?: any, + side?: ComponentChildren; + children?: ComponentChildren; +} + +const defaultToString = (f?: any): string => f || '' +const defaultFromString = (v: string): any => v as any + +const TextInput = ({ inputType, error, ...rest }: any) => inputType === 'multiline' ? +