@luscii-healthtech/web-ui 2.4.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.4.0",
2
+ "version": "2.4.1",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { Control, useForm } from "react-hook-form";
2
+ import { Control, useForm, UseFormReturn } from "react-hook-form";
3
3
 
4
4
  import { PrimaryButton } from "../ButtonV2/PrimaryButton";
5
5
  import { InputProps } from "../Input/Input";
@@ -7,85 +7,56 @@ import { RadioGroupProps } from "../RadioGroup/RadioGroupV2";
7
7
  import { SelectProps } from "../Select/SelectV2";
8
8
 
9
9
  import { FormInput } from "./FormInput";
10
- import { FormFieldProps, FormFieldRowProps, FormProps } from "./form.types";
10
+ import {
11
+ FormFieldProps,
12
+ FormFieldRowProps,
13
+ FormProps,
14
+ GenericFormProps,
15
+ } from "./form.types";
11
16
  import { isRequired } from "./form.transformer";
12
17
  import { FormRadioGroup } from "./FormRadioGroup";
13
18
  import { FormSelect } from "./FormSelect";
14
19
 
15
20
  /**
16
- * Create a straight forward Form.
21
+ * Create a straight forward Form, which takes away the 'overhead' of react-hook-form.
17
22
  *
18
- * TODO: wrap this in some Page component to style the div and buttons
19
- * WARNING: don't use this component before some styling errors are resolved.
23
+ * You will probably don't want to use this component for now, given that the button is hardcoded.
24
+ * This is an example for the Form for now, though it could be improved to enforce further unification.
25
+ *
26
+ * TODO: make the buttons configurable.
20
27
  */
21
- export function Form<TFieldValues>({
28
+ export function GenericForm<TFieldValues>({
22
29
  fields,
23
30
  onValid,
24
31
  onError,
25
32
  defaultValues,
26
- }: FormProps<TFieldValues>): JSX.Element {
27
- const {
28
- register,
29
- handleSubmit,
30
- control,
31
- formState: { errors },
32
- } = useForm<TFieldValues>({
33
+ }: GenericFormProps<TFieldValues>): JSX.Element {
34
+ const useFormReturn = useForm<TFieldValues>({
33
35
  criteriaMode: "all",
34
36
  defaultValues: defaultValues,
35
37
  });
36
38
 
37
- const fieldMapper = ({
38
- type,
39
- name,
40
- options,
41
- fieldProps = {},
42
- ...decoratorProps
43
- }: FormFieldProps<TFieldValues>) => {
44
- switch (type) {
45
- case "text":
46
- case "number":
47
- case "email":
48
- case "password":
49
- return (
50
- <FormInput
51
- key={name}
52
- {...decoratorProps}
53
- fieldRequired={isRequired(options)}
54
- fieldErrors={errors}
55
- {...(fieldProps as InputProps)}
56
- {...register(name, options)}
57
- type={type || "text"}
58
- />
59
- );
60
- case "select":
61
- return (
62
- <FormSelect
63
- key={name}
64
- {...decoratorProps}
65
- fieldRequired={isRequired(options)}
66
- fieldErrors={errors}
67
- {...(fieldProps as SelectProps)}
68
- control={control as Control}
69
- rules={options}
70
- name={name}
71
- />
72
- );
73
- case "radioGroup":
74
- return (
75
- <FormRadioGroup
76
- key={name}
77
- {...decoratorProps}
78
- fieldRequired={isRequired(options)}
79
- fieldErrors={errors}
80
- {...(fieldProps as RadioGroupProps)}
81
- {...register(name, options)}
82
- />
83
- );
84
- default:
85
- return undefined;
86
- }
87
- };
39
+ const { handleSubmit } = useFormReturn;
88
40
 
41
+ return (
42
+ <div className="space-y-4">
43
+ <Form fields={fields} useFormReturn={useFormReturn} />
44
+ <PrimaryButton onClick={handleSubmit(onValid, onError)} text={"submit"} />
45
+ </div>
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Creates a Form based on the fields input.
51
+ *
52
+ * Expects the results of the useForm hook to be injected into the useFormReturn parameter.
53
+ *
54
+ * This allows you to use and modify the useFormReturn before injecting it here.
55
+ */
56
+ export function Form<TFieldValues, TContext>({
57
+ fields,
58
+ useFormReturn,
59
+ }: FormProps<TFieldValues, TContext>): JSX.Element {
89
60
  return (
90
61
  <div className="space-y-4">
91
62
  {fields.map((props) => {
@@ -93,14 +64,75 @@ export function Form<TFieldValues>({
93
64
  const { fields: rowFields, key } =
94
65
  props as FormFieldRowProps<TFieldValues>;
95
66
  return (
96
- <div className={"flex flex-row"} key={key}>
97
- {rowFields.map(fieldMapper)}
67
+ <div className={"flex flex-row space-x-4"} key={key}>
68
+ {rowFields.map((field) => FormFieldMapper(field, useFormReturn))}
98
69
  </div>
99
70
  );
100
71
  }
101
- return fieldMapper(props);
72
+ return FormFieldMapper(props, useFormReturn);
102
73
  })}
103
- <PrimaryButton onClick={handleSubmit(onValid, onError)} text={"submit"} />
104
74
  </div>
105
75
  );
106
76
  }
77
+
78
+ function FormFieldMapper<TFieldValues, TContext>(
79
+ formFieldProps: FormFieldProps<TFieldValues>,
80
+ useFormReturn: UseFormReturn<TFieldValues, TContext>
81
+ ): JSX.Element {
82
+ const {
83
+ type,
84
+ name,
85
+ options,
86
+ fieldProps = {},
87
+ ...decoratorProps
88
+ } = formFieldProps;
89
+ const {
90
+ register,
91
+ control,
92
+ formState: { errors },
93
+ } = useFormReturn;
94
+
95
+ switch (type) {
96
+ case "text":
97
+ case "number":
98
+ case "email":
99
+ case "password":
100
+ return (
101
+ <FormInput
102
+ key={name}
103
+ {...decoratorProps}
104
+ fieldRequired={isRequired(options)}
105
+ fieldErrors={errors}
106
+ {...(fieldProps as InputProps)}
107
+ {...register(name, options)}
108
+ type={type || "text"}
109
+ />
110
+ );
111
+ case "select":
112
+ return (
113
+ <FormSelect
114
+ key={name}
115
+ {...decoratorProps}
116
+ fieldRequired={isRequired(options)}
117
+ fieldErrors={errors}
118
+ {...(fieldProps as SelectProps)}
119
+ control={control as Control}
120
+ rules={options}
121
+ name={name}
122
+ />
123
+ );
124
+ case "radioGroup":
125
+ return (
126
+ <FormRadioGroup
127
+ key={name}
128
+ {...decoratorProps}
129
+ fieldRequired={isRequired(options)}
130
+ fieldErrors={errors}
131
+ {...(fieldProps as RadioGroupProps)}
132
+ {...register(name, options)}
133
+ />
134
+ );
135
+ default:
136
+ return <></>;
137
+ }
138
+ }
@@ -7,6 +7,7 @@ import {
7
7
  RegisterOptions,
8
8
  SubmitErrorHandler,
9
9
  SubmitHandler,
10
+ UseFormReturn,
10
11
  } from "react-hook-form";
11
12
  import React, { HTMLInputTypeAttribute } from "react";
12
13
 
@@ -27,7 +28,7 @@ export type AllowedTextInputTypes = Extract<
27
28
  // --------------------------------------------
28
29
 
29
30
  // the input for the 'out-of-the-box' Form
30
- export interface FormProps<TFieldValues extends FieldValues> {
31
+ export interface GenericFormProps<TFieldValues extends FieldValues> {
31
32
  // the fields to be rendered
32
33
  fields: (FormFieldProps<TFieldValues> | FormFieldRowProps<TFieldValues>)[];
33
34
  onValid: SubmitHandler<TFieldValues>;
@@ -36,6 +37,12 @@ export interface FormProps<TFieldValues extends FieldValues> {
36
37
  defaultValues?: DeepPartial<TFieldValues>;
37
38
  }
38
39
 
40
+ export interface FormProps<TFieldValues extends FieldValues, TContext> {
41
+ // the fields to be rendered
42
+ fields: (FormFieldProps<TFieldValues> | FormFieldRowProps<TFieldValues>)[];
43
+ useFormReturn: UseFormReturn<TFieldValues, TContext>;
44
+ }
45
+
39
46
  interface FormFieldGenericProps<TFieldType, TFieldValues>
40
47
  extends FormFieldDecoratorProps {
41
48
  // the name of the field, which is registered within react-hook-form
package/src/index.tsx CHANGED
@@ -69,6 +69,11 @@ export {
69
69
  export { default as PreviewPhone } from "./components/PreviewPhone/PreviewPhone";
70
70
  export { default as Radio } from "./components/Radio/Radio";
71
71
  export { default as RadioGroup } from "./components/RadioGroup/RadioGroup";
72
+ export { RadioProps, RadioV2 } from "./components/Radio/RadioV2";
73
+ export {
74
+ RadioGroupProps,
75
+ RadioGroupV2,
76
+ } from "./components/RadioGroup/RadioGroupV2";
72
77
  export { default as Section } from "./components/Section/Section";
73
78
  export { default as SectionItemWithContent } from "./components/Section/SectionItemWithContent";
74
79
  export {
@@ -76,6 +81,7 @@ export {
76
81
  SectionItemProps,
77
82
  } from "./components/Section/SectionItem";
78
83
  //export { default as Select } from "./components/Select/Select";
84
+ export { SelectProps, Select } from "./components/Select/SelectV2";
79
85
  export {
80
86
  SettingsMenuButton,
81
87
  SettingsMenuButtonProps,
@@ -115,8 +121,8 @@ export { default as Text } from "./components/Text/Text";
115
121
 
116
122
  export { SearchInput, SearchInputProps } from "./components/Input/SearchInput";
117
123
 
118
- export { Form } from "./components/Form/Form";
119
- export { FormProps } from "./components/Form/form.types";
124
+ export { GenericForm, Form } from "./components/Form/Form";
125
+ export { GenericFormProps, FormProps } from "./components/Form/form.types";
120
126
 
121
127
  export { IconProps } from "./components/Icons/types/IconProps.type";
122
128
  export { AddIcon } from "./components/Icons/AddIcon";