@engagebay/engagebay-form-module 1.0.0-beta.2 → 1.0.0-beta.3-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 +17 -1
- package/src/api/AxiosConfigProvider.tsx +53 -0
- package/src/api/index.ts +8 -2
- package/src/form/Form.tsx +3 -0
- package/src/form/context/FormContext.tsx +2 -0
- package/src/form/formfields/BusinessHoursField.tsx +3 -4
- package/src/form/formfields/ColorPickerField.tsx +3 -1
- package/src/form/formfields/DynamicMultiSelect.tsx +2 -1
- package/src/form/formfields/DynamicSelect.tsx +1 -1
- package/src/form/formfields/RangeField.tsx +3 -1
- package/src/form/formfields/Typeahead.tsx +6 -7
- package/src/form/formfields/TypeaheadMultiSelect.tsx +7 -8
- package/src/util/LoaderWithText.tsx +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@engagebay/engagebay-form-module",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.3-1",
|
|
4
4
|
"description": "Provide base form components to reacho",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -26,5 +26,21 @@
|
|
|
26
26
|
"react-redux": ">=8.0.4",
|
|
27
27
|
"react-tailwindcss-datepicker": ">=1.6.6",
|
|
28
28
|
"redux-saga": ">=1.3.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@headlessui/react": ">=2.1.2",
|
|
32
|
+
"@heroicons/react": ">=2.1.5",
|
|
33
|
+
"@reduxjs/toolkit": ">=2.2.7",
|
|
34
|
+
"@tippyjs/react": ">=4.2.6",
|
|
35
|
+
"@types/lodash": ">=4.17.7",
|
|
36
|
+
"@types/react-redux": ">=7.1.33",
|
|
37
|
+
"axios": ">=1.7.2",
|
|
38
|
+
"clsx": ">=2.1.1",
|
|
39
|
+
"moment": ">=2.30.1",
|
|
40
|
+
"react-hook-form": ">=7.52.1",
|
|
41
|
+
"react-popper": ">=2.3.0",
|
|
42
|
+
"react-redux": ">=8.0.4",
|
|
43
|
+
"react-tailwindcss-datepicker": ">=1.6.6",
|
|
44
|
+
"redux-saga": ">=1.3.0"
|
|
29
45
|
}
|
|
30
46
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import React, { createContext, useContext, ReactNode } from 'react';
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
|
|
4
|
+
interface AxiosConfigContextType {
|
|
5
|
+
axiosInstance: AxiosInstance;
|
|
6
|
+
baseURL?: string;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const AxiosConfigContext = createContext<AxiosConfigContextType | null>(null);
|
|
12
|
+
|
|
13
|
+
interface AxiosConfigProviderProps {
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
axiosInstance: AxiosInstance;
|
|
16
|
+
baseURL?: string;
|
|
17
|
+
timeout?: number;
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const AxiosConfigProvider: React.FC<AxiosConfigProviderProps> = ({
|
|
22
|
+
children,
|
|
23
|
+
axiosInstance,
|
|
24
|
+
baseURL,
|
|
25
|
+
timeout,
|
|
26
|
+
headers,
|
|
27
|
+
}) => {
|
|
28
|
+
const config: AxiosConfigContextType = {
|
|
29
|
+
axiosInstance,
|
|
30
|
+
baseURL,
|
|
31
|
+
timeout,
|
|
32
|
+
headers,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<AxiosConfigContext.Provider value={config}>
|
|
37
|
+
{children}
|
|
38
|
+
</AxiosConfigContext.Provider>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const useAxiosConfig = (): AxiosConfigContextType => {
|
|
43
|
+
const context = useContext(AxiosConfigContext);
|
|
44
|
+
if (!context) {
|
|
45
|
+
throw new Error('useAxiosConfig must be used within an AxiosConfigProvider');
|
|
46
|
+
}
|
|
47
|
+
return context;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Hook for optional usage (doesn't throw error if not provided)
|
|
51
|
+
export const useAxiosConfigOptional = (): AxiosConfigContextType | null => {
|
|
52
|
+
return useContext(AxiosConfigContext);
|
|
53
|
+
};
|
package/src/api/index.ts
CHANGED
|
@@ -25,9 +25,15 @@ reachoAPI.interceptors.request.use(
|
|
|
25
25
|
}
|
|
26
26
|
);
|
|
27
27
|
|
|
28
|
-
export const getAxiosInstance = (fieldConfig: FormFieldSchema): AxiosInstance => {
|
|
29
|
-
|
|
28
|
+
export const getAxiosInstance = (axiosInstance: AxiosInstance | undefined, fieldConfig: FormFieldSchema): AxiosInstance => {
|
|
29
|
+
|
|
30
|
+
if (fieldConfig && fieldConfig.axiosInstance) {
|
|
30
31
|
return fieldConfig.axiosInstance;
|
|
31
32
|
}
|
|
33
|
+
|
|
34
|
+
if (axiosInstance) {
|
|
35
|
+
return axiosInstance;
|
|
36
|
+
}
|
|
37
|
+
|
|
32
38
|
return fieldConfig.disableHeaderInFetch ? axios : reachoAPI;
|
|
33
39
|
};
|
package/src/form/Form.tsx
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
} from "react-hook-form";
|
|
13
13
|
import { FormFieldSchema } from "./schema/FormFieldSchema";
|
|
14
14
|
import { FormContext } from "./context/FormContext";
|
|
15
|
+
import { AxiosInstance } from 'axios';
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Props for the Form component
|
|
@@ -26,6 +27,7 @@ import { FormContext } from "./context/FormContext";
|
|
|
26
27
|
*/
|
|
27
28
|
type FormPropsSchema = {
|
|
28
29
|
fieldSchema: FormFieldSchema[];
|
|
30
|
+
axiosInstance?: AxiosInstance
|
|
29
31
|
formData: any;
|
|
30
32
|
onError?: (error: any) => void;
|
|
31
33
|
onSubmit?: (data: FormData) => void;
|
|
@@ -132,6 +134,7 @@ export default function Form(props: FormPropsSchema): JSX.Element {
|
|
|
132
134
|
setError,
|
|
133
135
|
setFocus,
|
|
134
136
|
unregister,
|
|
137
|
+
axiosInstance: props.axiosInstance,
|
|
135
138
|
}}>
|
|
136
139
|
{/* Form element */}
|
|
137
140
|
<form
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
UseFormWatch,
|
|
18
18
|
} from "react-hook-form";
|
|
19
19
|
import { FormFieldSchema } from "../schema/FormFieldSchema";
|
|
20
|
+
import { AxiosInstance } from "axios";
|
|
20
21
|
|
|
21
22
|
export interface FormContextType {
|
|
22
23
|
formFields: FormFieldSchema[];
|
|
@@ -43,6 +44,7 @@ export interface FormContextType {
|
|
|
43
44
|
// control: Control<TFieldValues, TContext>;
|
|
44
45
|
// register: UseFormRegister<TFieldValues>;
|
|
45
46
|
setFocus: UseFormSetFocus<any>;
|
|
47
|
+
axiosInstance?: AxiosInstance;
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
export const FormContext = createContext({
|
|
@@ -10,7 +10,6 @@ import {FormContext} from "../context/FormContext";
|
|
|
10
10
|
import {RegisterOptions} from "react-hook-form";
|
|
11
11
|
import {convertToTitleCase, registerFormField} from "../util";
|
|
12
12
|
import axios from "axios";
|
|
13
|
-
import {reachoAPI} from "../../api";
|
|
14
13
|
import SwitchField from "./SwitchField";
|
|
15
14
|
import FormField from "../FormField";
|
|
16
15
|
import RenderFormField from "../util/RenderFormField";
|
|
@@ -138,9 +137,9 @@ export const BusinessHoursField: React.FC<FormFieldComponentPropSchema> = (props
|
|
|
138
137
|
try {
|
|
139
138
|
let response = await (props.fieldConfig.disableHeaderInFetch
|
|
140
139
|
? axios.get(url)
|
|
141
|
-
:
|
|
140
|
+
: formContext.axiosInstance?.get(url));
|
|
142
141
|
|
|
143
|
-
if (response
|
|
142
|
+
if (response?.data) {
|
|
144
143
|
const data: FieldOptionsSchema[] = response.data;
|
|
145
144
|
setListOptions(data);
|
|
146
145
|
setLoading(false);
|
|
@@ -149,7 +148,7 @@ export const BusinessHoursField: React.FC<FormFieldComponentPropSchema> = (props
|
|
|
149
148
|
props.fieldConfig.fetchCallback(response);
|
|
150
149
|
}
|
|
151
150
|
} else {
|
|
152
|
-
console.error(response
|
|
151
|
+
console.error(response?.statusText);
|
|
153
152
|
setLoading(false);
|
|
154
153
|
}
|
|
155
154
|
} catch (error) {
|
|
@@ -7,7 +7,7 @@ import { Field } from "@headlessui/react";
|
|
|
7
7
|
import RenderFormField from "../util/RenderFormField";
|
|
8
8
|
import { handleChange, registerFormField } from "../util";
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const ColorPickerField: React.FC<FormFieldComponentPropSchema> = (
|
|
11
11
|
props: FormFieldComponentPropSchema
|
|
12
12
|
) => {
|
|
13
13
|
const formContext = useContext(FormContext);
|
|
@@ -57,3 +57,5 @@ export const ColorPickerField: React.FC<FormFieldComponentPropSchema> = (
|
|
|
57
57
|
<RenderFormField fieldConfig={props.fieldConfig} getInput={getInput} />
|
|
58
58
|
);
|
|
59
59
|
};
|
|
60
|
+
|
|
61
|
+
export default ColorPickerField;
|
|
@@ -97,9 +97,10 @@ const DynamicMultiSelect: React.FC<FormFieldComponentPropSchema> = (
|
|
|
97
97
|
let options = props.fieldConfig.options;
|
|
98
98
|
setListOptions(prevState => [...options, ...prevState]);
|
|
99
99
|
}
|
|
100
|
+
|
|
100
101
|
const fetchData = async () => {
|
|
101
102
|
if (!props.fieldConfig.fetchUrl) return;
|
|
102
|
-
const axiosInstance = getAxiosInstance(props.fieldConfig);
|
|
103
|
+
const axiosInstance = getAxiosInstance(formContext.axiosInstance, props.fieldConfig);
|
|
103
104
|
let response = await axiosInstance.get(props.fieldConfig.fetchUrl);
|
|
104
105
|
|
|
105
106
|
if (response.data) {
|
|
@@ -96,7 +96,7 @@ const DynamicSelect: React.FC<FormFieldComponentPropSchema> = (
|
|
|
96
96
|
const fetchData = useCallback(async () => {
|
|
97
97
|
if (!props.fieldConfig.fetchUrl) return;
|
|
98
98
|
|
|
99
|
-
const axiosInstance = getAxiosInstance(props.fieldConfig);
|
|
99
|
+
const axiosInstance = getAxiosInstance(formContext.axiosInstance, props.fieldConfig);
|
|
100
100
|
|
|
101
101
|
try {
|
|
102
102
|
let response = await axiosInstance.get(props.fieldConfig.fetchUrl);
|
|
@@ -7,7 +7,7 @@ import { Input } from "@headlessui/react";
|
|
|
7
7
|
import RenderFormField from "../util/RenderFormField";
|
|
8
8
|
import { handleChange, registerFormField } from "../util";
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const RangeField: React.FC<FormFieldComponentPropSchema> = (
|
|
11
11
|
props: FormFieldComponentPropSchema
|
|
12
12
|
) => {
|
|
13
13
|
const formContext = useContext(FormContext);
|
|
@@ -51,3 +51,5 @@ export const RangeField: React.FC<FormFieldComponentPropSchema> = (
|
|
|
51
51
|
<RenderFormField fieldConfig={props.fieldConfig} getInput={getInput} />
|
|
52
52
|
);
|
|
53
53
|
};
|
|
54
|
+
|
|
55
|
+
export default RangeField;
|
|
@@ -9,7 +9,6 @@ import React, {
|
|
|
9
9
|
import { Listbox, ListboxButton } from "@headlessui/react";
|
|
10
10
|
import axios from "axios";
|
|
11
11
|
import { RegisterOptions } from "react-hook-form";
|
|
12
|
-
import { reachoAPI } from "../../api";
|
|
13
12
|
import { FormContext } from "../context/FormContext";
|
|
14
13
|
import { getListOption, getListOptions } from "../FormFieldUtils";
|
|
15
14
|
import {
|
|
@@ -68,8 +67,8 @@ const Typeahead: React.FC<FormFieldComponentPropSchema> = (
|
|
|
68
67
|
|
|
69
68
|
let response = await (props.fieldConfig.disableHeaderInFetch
|
|
70
69
|
? axios.get(url)
|
|
71
|
-
:
|
|
72
|
-
if (response
|
|
70
|
+
: formContext.axiosInstance?.get(url));
|
|
71
|
+
if (response?.data) {
|
|
73
72
|
const data: FieldOptionsSchema[] = getListOptions(
|
|
74
73
|
response.data,
|
|
75
74
|
props.fieldConfig.optionsConfig
|
|
@@ -87,7 +86,7 @@ const Typeahead: React.FC<FormFieldComponentPropSchema> = (
|
|
|
87
86
|
props.fieldConfig.fetchCallback(response);
|
|
88
87
|
}
|
|
89
88
|
} else {
|
|
90
|
-
console.error(response
|
|
89
|
+
console.error(response?.statusText);
|
|
91
90
|
}
|
|
92
91
|
} catch (err) {
|
|
93
92
|
} finally {
|
|
@@ -116,11 +115,11 @@ const Typeahead: React.FC<FormFieldComponentPropSchema> = (
|
|
|
116
115
|
let response = await (
|
|
117
116
|
props.fieldConfig.disableHeaderInFetch
|
|
118
117
|
? axios.get(url)
|
|
119
|
-
:
|
|
118
|
+
: formContext.axiosInstance?.get(url)
|
|
120
119
|
);
|
|
121
|
-
if (response
|
|
120
|
+
if (response?.data) {
|
|
122
121
|
const data: FieldOptionsSchema[] = getListOptions(
|
|
123
|
-
response
|
|
122
|
+
response?.data,
|
|
124
123
|
props.fieldConfig.optionsConfig
|
|
125
124
|
);
|
|
126
125
|
setSelectedValues([...data]);
|
|
@@ -23,7 +23,6 @@ import RenderListOptions, {
|
|
|
23
23
|
} from "../util/RenderListOptions";
|
|
24
24
|
// import _ from "lodash";
|
|
25
25
|
import axios from "axios";
|
|
26
|
-
import { reachoAPI } from "../../api";
|
|
27
26
|
|
|
28
27
|
const TypeaheadMultiSelect: React.FC<FormFieldComponentPropSchema> = (
|
|
29
28
|
props: FormFieldComponentPropSchema
|
|
@@ -69,10 +68,10 @@ const TypeaheadMultiSelect: React.FC<FormFieldComponentPropSchema> = (
|
|
|
69
68
|
|
|
70
69
|
let response = await (props.fieldConfig.disableHeaderInFetch
|
|
71
70
|
? axios.get(url)
|
|
72
|
-
:
|
|
73
|
-
if (response
|
|
71
|
+
: formContext.axiosInstance?.get(url));
|
|
72
|
+
if (response?.data) {
|
|
74
73
|
const data: FieldOptionsSchema[] = getListOptions(
|
|
75
|
-
response
|
|
74
|
+
response?.data,
|
|
76
75
|
props.fieldConfig.optionsConfig
|
|
77
76
|
);
|
|
78
77
|
setListOptions([...data]);
|
|
@@ -91,7 +90,7 @@ const TypeaheadMultiSelect: React.FC<FormFieldComponentPropSchema> = (
|
|
|
91
90
|
props.fieldConfig.fetchCallback(response);
|
|
92
91
|
}
|
|
93
92
|
} else {
|
|
94
|
-
console.error(response
|
|
93
|
+
console.error(response?.statusText);
|
|
95
94
|
}
|
|
96
95
|
} catch (err) {
|
|
97
96
|
} finally {
|
|
@@ -118,10 +117,10 @@ const TypeaheadMultiSelect: React.FC<FormFieldComponentPropSchema> = (
|
|
|
118
117
|
|
|
119
118
|
let response = await (props.fieldConfig.disableHeaderInFetch
|
|
120
119
|
? axios.get(url)
|
|
121
|
-
:
|
|
122
|
-
if (response
|
|
120
|
+
: formContext.axiosInstance?.get(url));
|
|
121
|
+
if (response?.data) {
|
|
123
122
|
const data: FieldOptionsSchema[] = getListOptions(
|
|
124
|
-
response
|
|
123
|
+
response?.data,
|
|
125
124
|
props.fieldConfig.optionsConfig
|
|
126
125
|
);
|
|
127
126
|
setSelectedValues([...data]);
|