@engagebay/engagebay-form-module 1.0.1 → 1.0.2-beta.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.
@@ -8,52 +8,53 @@ import RenderFormField from "../util/RenderFormField";
8
8
  import { handleChange, registerFormField } from "../util";
9
9
 
10
10
  export const ColorPickerField: React.FC<FormFieldComponentPropSchema> = (
11
- props: FormFieldComponentPropSchema
11
+ props: FormFieldComponentPropSchema
12
12
  ) => {
13
- const formContext = useContext(FormContext);
13
+ const formContext = useContext(FormContext);
14
14
 
15
- let registerOptions: RegisterOptions = registerFormField(props.fieldConfig);
15
+ let registerOptions: RegisterOptions = registerFormField(props.fieldConfig);
16
16
 
17
- formContext.register(props.fieldConfig.name, registerOptions);
18
-
19
- const getInput = () => {
20
- return (
21
- <Field className="flex" disabled={props.fieldConfig.disabled}>
22
- <input
23
- type="color"
24
- onChange={(e) =>
25
- handleChange(
26
- e.target.value,
27
- formContext,
28
- props.fieldConfig,
29
- props.onChange
30
- )
31
- }
32
- defaultValue={props.fieldConfig.defaultValue as string}
33
- disabled={props.fieldConfig.readOnly}
34
- value={formContext.getValues(props.fieldConfig.name) ?? ""}
35
- className="inline-flex w-[30%] h-auto rounded-l-md border border-r-0 border-gray-300 px-2 cursor-pointer"></input>
36
- <input
37
- type="text"
38
- value={formContext.getValues(props.fieldConfig.name) ?? ""}
39
- disabled={props.fieldConfig.readOnly}
40
- defaultValue={props.fieldConfig.defaultValue as string}
41
- maxLength={7}
42
- onChange={(e) =>
43
- handleChange(
44
- e.target.value,
45
- formContext,
46
- props.fieldConfig,
47
- props.onChange
48
- )
49
- }
50
- className="flex-1 form-input rounded-s-none"
51
- />
52
- </Field>
53
- );
54
- };
17
+ formContext.register(props.fieldConfig.name, registerOptions);
55
18
 
19
+ const getInput = () => {
56
20
  return (
57
- <RenderFormField fieldConfig={props.fieldConfig} getInput={getInput} />
21
+ <Field className="flex" disabled={props.fieldConfig.disabled}>
22
+ <input
23
+ type="color"
24
+ onChange={(e) =>
25
+ handleChange(
26
+ e.target.value,
27
+ formContext,
28
+ props.fieldConfig,
29
+ props.onChange
30
+ )
31
+ }
32
+ defaultValue={props.fieldConfig.defaultValue as string}
33
+ disabled={props.fieldConfig.readOnly}
34
+ value={formContext.getValues(props.fieldConfig.name) ?? ""}
35
+ className="inline-flex w-[30%] h-auto rounded-l-md border-r-0 border-gray-300 cursor-pointer"
36
+ ></input>
37
+ <input
38
+ type="text"
39
+ value={formContext.getValues(props.fieldConfig.name) ?? ""}
40
+ disabled={props.fieldConfig.readOnly}
41
+ defaultValue={props.fieldConfig.defaultValue as string}
42
+ maxLength={7}
43
+ onChange={(e) =>
44
+ handleChange(
45
+ e.target.value,
46
+ formContext,
47
+ props.fieldConfig,
48
+ props.onChange
49
+ )
50
+ }
51
+ className="flex-1 form-input rounded-l-none"
52
+ />
53
+ </Field>
58
54
  );
55
+ };
56
+
57
+ return (
58
+ <RenderFormField fieldConfig={props.fieldConfig} getInput={getInput} />
59
+ );
59
60
  };
@@ -8,82 +8,79 @@ import { handleChange, registerFormField } from "../util";
8
8
  import moment from "moment";
9
9
 
10
10
  const DatePicker: React.FC<FormFieldComponentPropSchema> = (
11
- props: FormFieldComponentPropSchema
11
+ props: FormFieldComponentPropSchema
12
12
  ) => {
13
- const formContext = useContext(FormContext);
14
- const initialDate = formContext.getValues(props.fieldConfig.name)
15
- ? {
16
- startDate: formContext.getValues(props.fieldConfig.name),
17
- endDate: formContext.getValues(props.fieldConfig.name),
18
- }
19
- : { startDate: "", endDate: "" };
20
-
21
- useEffect(() => {
22
- if (
23
- props.fieldConfig.defaultValue &&
24
- !formContext.getValues(props.fieldConfig.name)
25
- ) {
26
- formContext.setValue(
27
- props.fieldConfig.name,
28
- props.fieldConfig.defaultValue
29
- );
30
- }
31
- }, [props.fieldConfig.forceUpdate]);
32
-
33
- let registerOptions: RegisterOptions = registerFormField(props.fieldConfig);
13
+ const formContext = useContext(FormContext);
14
+ const initialDate = formContext.getValues(props.fieldConfig.name)
15
+ ? {
16
+ startDate: formContext.getValues(props.fieldConfig.name),
17
+ endDate: formContext.getValues(props.fieldConfig.name),
18
+ }
19
+ : { startDate: "", endDate: "" };
34
20
 
35
- let hookProps = formContext.register(
21
+ useEffect(() => {
22
+ if (
23
+ props.fieldConfig.defaultValue &&
24
+ !formContext.getValues(props.fieldConfig.name)
25
+ ) {
26
+ formContext.setValue(
36
27
  props.fieldConfig.name,
37
- registerOptions
38
- );
28
+ props.fieldConfig.defaultValue
29
+ );
30
+ }
31
+ }, [props.fieldConfig.forceUpdate]);
39
32
 
40
- function getInput() {
41
- return (
42
- <Datepicker
43
- value={
44
- initialDate || {
45
- startDate: props.fieldConfig.defaultValue,
46
- endDate: props.fieldConfig.defaultValue,
47
- }
48
- }
49
- {...hookProps}
50
- placeholder={
51
- props.fieldConfig.placeholder
52
- ? props.fieldConfig.placeholder
53
- : "YYYY-MM-DD"
54
- }
55
- asSingle={true}
56
- popoverDirection="down"
57
- useRange={false}
58
- inputName={props.fieldConfig.name}
59
- key={props.fieldConfig.name}
60
- containerClassName={"relative"}
61
- minDate={props.fieldConfig.minDate}
62
- maxDate={props.fieldConfig.maxDate}
63
- inputClassName={
64
- props.fieldConfig.customClassNames?.fieldClassName
65
- ? "form-input " +
66
- props.fieldConfig.customClassNames.fieldClassName
67
- : "form-input flex-1"
68
- }
69
- onChange={(dates) => {
70
- let date = null;
71
- if (dates?.startDate != null)
72
- date = moment(dates?.startDate).format("YYYY-MM-DD");
33
+ let registerOptions: RegisterOptions = registerFormField(props.fieldConfig);
73
34
 
74
- handleChange(
75
- date,
76
- formContext,
77
- props.fieldConfig,
78
- props.onChange
79
- );
80
- }}
81
- />
82
- );
83
- }
35
+ let hookProps = formContext.register(props.fieldConfig.name, registerOptions);
36
+
37
+ function getInput() {
84
38
  return (
85
- <RenderFormField fieldConfig={props.fieldConfig} getInput={getInput} />
39
+ <Datepicker
40
+ value={
41
+ initialDate || {
42
+ startDate: props.fieldConfig.defaultValue,
43
+ endDate: props.fieldConfig.defaultValue,
44
+ }
45
+ }
46
+ {...hookProps}
47
+ placeholder={
48
+ props.fieldConfig.placeholder
49
+ ? props.fieldConfig.placeholder
50
+ : "YYYY-MM-DD"
51
+ }
52
+ asSingle={true}
53
+ popoverDirection="down"
54
+ useRange={false}
55
+ inputName={props.fieldConfig.name}
56
+ key={
57
+ props.fieldConfig.name +
58
+ "_" +
59
+ initialDate.startDate +
60
+ "_" +
61
+ initialDate.endDate
62
+ }
63
+ containerClassName={"relative"}
64
+ minDate={props.fieldConfig.minDate}
65
+ maxDate={props.fieldConfig.maxDate}
66
+ inputClassName={
67
+ props.fieldConfig.customClassNames?.fieldClassName
68
+ ? "form-input " + props.fieldConfig.customClassNames.fieldClassName
69
+ : "form-input flex-1"
70
+ }
71
+ onChange={(dates) => {
72
+ let date = null;
73
+ if (dates?.startDate != null)
74
+ date = moment(dates?.startDate).format("YYYY-MM-DD");
75
+
76
+ handleChange(date, formContext, props.fieldConfig, props.onChange);
77
+ }}
78
+ />
86
79
  );
80
+ }
81
+ return (
82
+ <RenderFormField fieldConfig={props.fieldConfig} getInput={getInput} />
83
+ );
87
84
  };
88
85
 
89
86
  export default DatePicker;
@@ -1,189 +1,189 @@
1
- import React, {useContext, useEffect, useRef, useState} from "react";
1
+ import React, { useContext, useEffect, useRef, useState } from "react";
2
2
  import {
3
- FieldOptionsSchema,
4
- FormFieldComponentPropSchema,
5
- FormFieldType,
6
- StoreStateSchema,
3
+ FieldOptionsSchema,
4
+ FormFieldComponentPropSchema,
5
+ FormFieldType,
6
+ StoreStateSchema,
7
7
  } from "../schema/FormFieldSchema";
8
- import {RegisterOptions} from "react-hook-form";
9
- import {Listbox, ListboxButton} from "@headlessui/react";
8
+ import { RegisterOptions } from "react-hook-form";
9
+ import { Listbox, ListboxButton } from "@headlessui/react";
10
10
 
11
- import {FormContext} from "../context/FormContext";
12
- import {getListOptions} from "../FormFieldUtils";
13
- import axios from "axios";
11
+ import { FormContext } from "../context/FormContext";
12
+ import { getListOptions } from "../FormFieldUtils";
14
13
  import useDymanicReducer from "../hooks/useDynamicReducer";
15
- import {useDispatch} from "react-redux";
14
+ import { useDispatch } from "react-redux";
16
15
  import RenderFormField from "../util/RenderFormField";
17
- import {handleChange, registerFormField} from "../util";
18
- import RenderListOptions, {renderListBoxValue,} from "../util/RenderListOptions";
19
- import {reachoAPI} from "../../api";
16
+ import { handleChange, registerFormField } from "../util";
17
+ import RenderListOptions, {
18
+ renderListBoxValue,
19
+ } from "../util/RenderListOptions";
20
+ import { getAxiosInstance } from "../../api";
20
21
 
21
22
  const DynamicMultiSelect: React.FC<FormFieldComponentPropSchema> = (
22
- props: FormFieldComponentPropSchema
23
+ props: FormFieldComponentPropSchema
23
24
  ) => {
24
- const dynamicSelectRef = useRef<HTMLUListElement>(null);
25
+ const dynamicSelectRef = useRef<HTMLUListElement>(null);
25
26
 
26
- const formContext = useContext(FormContext);
27
+ const formContext = useContext(FormContext);
27
28
 
28
- const [listOptions, setListOptions] = useState<FieldOptionsSchema[]>([]);
29
- const [loading, setLoading] = useState<boolean>(false);
29
+ const [listOptions, setListOptions] = useState<FieldOptionsSchema[]>([]);
30
+ const [loading, setLoading] = useState<boolean>(false);
30
31
 
31
- useEffect(() => {
32
- const {fieldConfig} = props;
33
-
34
- // Initialize dropdownFieldConfig with defaults if not already set
35
- fieldConfig.dropdownFieldConfig = {
36
- showCreateOption:
37
- fieldConfig.dropdownFieldConfig?.showCreateOption ?? false,
38
- showDeleteOption:
39
- fieldConfig.dropdownFieldConfig?.showDeleteOption ?? true,
40
- showSearchBox:
41
- fieldConfig.dropdownFieldConfig?.showSearchBox ?? true,
42
- showSelectedCount:
43
- fieldConfig.dropdownFieldConfig?.showSelectedCount ?? true,
44
- };
45
-
46
- if (
47
- !formContext.getValues(props.fieldConfig.name) &&
48
- props.fieldConfig.defaultValue
49
- ) {
50
- formContext.setValue(
51
- props.fieldConfig.name,
52
- props.fieldConfig.defaultValue
53
- );
54
- }
55
- }, []);
32
+ useEffect(() => {
33
+ const { fieldConfig } = props;
56
34
 
57
- if (props.fieldConfig.store) {
58
- const {state: state} = useDymanicReducer<StoreStateSchema<any>>({
59
- reducerConfig: props.fieldConfig.store,
60
- });
61
-
62
- const dispatch = useDispatch();
63
-
64
- useEffect(() => {
65
- if (
66
- props.fieldConfig.store?.initialFetchReducerAction &&
67
- (!state || !state.data)
68
- ) {
69
- dispatch(props.fieldConfig.store.initialFetchReducerAction(""));
70
- } else {
71
- if (state && state.data && Array.isArray(state.data)) {
72
- const list: FieldOptionsSchema[] = getListOptions(
73
- state.data,
74
- props.fieldConfig.optionsConfig
75
- );
76
- setListOptions([...list]);
77
- setLoading(false);
78
- }
79
- }
80
- }, []);
81
-
82
- useEffect(() => {
83
- if (state && state.data && Array.isArray(state.data)) {
84
- const list = getListOptions(
85
- state.data,
86
- props.fieldConfig.optionsConfig
87
- );
88
- setListOptions([...list]);
89
- setLoading(false);
90
- }
91
- }, [state]);
92
- } else if (props.fieldConfig.fetchUrl) {
93
- useEffect(() => {
94
- fetchData();
95
- }, []);
96
- }
97
- if (props.fieldConfig.options && props.fieldConfig.options.length > 0) {
98
- let options = props.fieldConfig.options;
99
- setListOptions(prevState => [...options, ...prevState]);
100
- }
101
- const fetchData = async () => {
102
- if (!props.fieldConfig.fetchUrl) return;
103
-
104
- let response = await (props.fieldConfig.disableHeaderInFetch
105
- ? axios.get(props.fieldConfig.fetchUrl)
106
- : reachoAPI.get(props.fieldConfig.fetchUrl));
107
- if (response.data) {
108
- const data: FieldOptionsSchema[] = getListOptions(
109
- response.data,
110
- props.fieldConfig.optionsConfig
111
- );
112
- setListOptions([...data]);
113
- if (props.fieldConfig.fetchCallback) {
114
- props.fieldConfig.fetchCallback(response);
115
- }
116
- } else {
117
- console.error(response.statusText);
118
- setLoading(false);
119
- }
35
+ // Initialize dropdownFieldConfig with defaults if not already set
36
+ fieldConfig.dropdownFieldConfig = {
37
+ showCreateOption:
38
+ fieldConfig.dropdownFieldConfig?.showCreateOption ?? false,
39
+ showDeleteOption:
40
+ fieldConfig.dropdownFieldConfig?.showDeleteOption ?? true,
41
+ showSearchBox: fieldConfig.dropdownFieldConfig?.showSearchBox ?? true,
42
+ showSelectedCount:
43
+ fieldConfig.dropdownFieldConfig?.showSelectedCount ?? true,
120
44
  };
121
45
 
122
- let registerOptions: RegisterOptions = registerFormField(
123
- props.fieldConfig,
124
- true
125
- );
126
-
127
- let hookProps = formContext.register(
46
+ if (
47
+ !formContext.getValues(props.fieldConfig.name) &&
48
+ props.fieldConfig.defaultValue
49
+ ) {
50
+ formContext.setValue(
128
51
  props.fieldConfig.name,
129
- registerOptions
130
- );
52
+ props.fieldConfig.defaultValue
53
+ );
54
+ }
55
+ }, []);
56
+
57
+ if (props.fieldConfig.store) {
58
+ const { state: state } = useDymanicReducer<StoreStateSchema<any>>({
59
+ reducerConfig: props.fieldConfig.store,
60
+ });
61
+
62
+ const dispatch = useDispatch();
131
63
 
132
- function getInput() {
133
- return (
134
- <Listbox
135
- multiple
136
- as={"div"}
137
- value={formContext.getValues(props.fieldConfig.name) ?? []}
138
- {...hookProps}
139
- onChange={(selectedOptions) =>
140
- handleChange(
141
- selectedOptions,
142
- formContext,
143
- props.fieldConfig,
144
- props.onChange
145
- )
146
- }
147
- defaultValue={props.fieldConfig.defaultValue}
148
- name={props.fieldConfig.name}
149
- disabled={props.fieldConfig.disabled}
150
- className={`relative form-listbox flex-1`}>
151
- <ListboxButton
152
- className={
153
- props.fieldConfig.customClassNames?.fieldClassName
154
- ? "form-listbox-select " +
155
- props.fieldConfig.customClassNames.fieldClassName
156
- : "form-listbox-select"
157
- }>
158
- {renderListBoxValue(
159
- formContext,
160
- props.fieldConfig,
161
- listOptions,
162
- props.onChange
163
- )
164
- }
165
- </ListboxButton>
166
- <RenderListOptions
167
- formContext={formContext}
168
- onChange={props.onChange}
169
- formField={FormFieldType.DYNAMIC_MULTI_SELECT}
170
- ref={dynamicSelectRef}
171
- fieldConfig={props.fieldConfig}
172
- listOptions={listOptions}
173
- setListOptions={setListOptions}
174
- loading={loading}
175
- setLoading={setLoading}
176
- />
177
- </Listbox>
64
+ useEffect(() => {
65
+ if (
66
+ props.fieldConfig.store?.initialFetchReducerAction &&
67
+ (!state || !state.data)
68
+ ) {
69
+ dispatch(props.fieldConfig.store.initialFetchReducerAction(""));
70
+ } else {
71
+ if (state && state.data && Array.isArray(state.data)) {
72
+ const list: FieldOptionsSchema[] = getListOptions(
73
+ state.data,
74
+ props.fieldConfig.optionsConfig
75
+ );
76
+ setListOptions([...list]);
77
+ setLoading(false);
78
+ }
79
+ }
80
+ }, []);
81
+
82
+ useEffect(() => {
83
+ if (state && state.data && Array.isArray(state.data)) {
84
+ const list = getListOptions(
85
+ state.data,
86
+ props.fieldConfig.optionsConfig
178
87
  );
88
+ setListOptions([...list]);
89
+ setLoading(false);
90
+ }
91
+ }, [state]);
92
+ } else if (props.fieldConfig.fetchUrl) {
93
+ useEffect(() => {
94
+ fetchData();
95
+ }, []);
96
+ }
97
+ if (props.fieldConfig.options && props.fieldConfig.options.length > 0) {
98
+ let options = props.fieldConfig.options;
99
+ setListOptions((prevState) => [...options, ...prevState]);
100
+ }
101
+
102
+ const fetchData = async () => {
103
+ if (!props.fieldConfig.fetchUrl) return;
104
+ const axiosInstance = getAxiosInstance(
105
+ formContext.axiosInstance,
106
+ props.fieldConfig
107
+ );
108
+ let response = await axiosInstance.get(props.fieldConfig.fetchUrl);
109
+
110
+ if (response.data) {
111
+ const data: FieldOptionsSchema[] = getListOptions(
112
+ response.data,
113
+ props.fieldConfig.optionsConfig
114
+ );
115
+ setListOptions([...data]);
116
+ if (props.fieldConfig.fetchCallback) {
117
+ props.fieldConfig.fetchCallback(response);
118
+ }
119
+ } else {
120
+ console.error(response.statusText);
121
+ setLoading(false);
179
122
  }
180
- if(props.fieldConfig.hideWhenNoResults && listOptions.length==0 )
181
- {
182
- return <></>
183
- }
123
+ };
184
124
 
125
+ let registerOptions: RegisterOptions = registerFormField(
126
+ props.fieldConfig,
127
+ true
128
+ );
129
+
130
+ let hookProps = formContext.register(props.fieldConfig.name, registerOptions);
131
+
132
+ function getInput() {
185
133
  return (
186
- <RenderFormField fieldConfig={props.fieldConfig} getInput={getInput}/>
134
+ <Listbox
135
+ multiple
136
+ as={"div"}
137
+ value={formContext.getValues(props.fieldConfig.name) ?? []}
138
+ {...hookProps}
139
+ onChange={(selectedOptions) =>
140
+ handleChange(
141
+ selectedOptions,
142
+ formContext,
143
+ props.fieldConfig,
144
+ props.onChange
145
+ )
146
+ }
147
+ defaultValue={props.fieldConfig.defaultValue}
148
+ name={props.fieldConfig.name}
149
+ disabled={props.fieldConfig.disabled}
150
+ className={`relative form-listbox flex-1 overflow-hidden`}
151
+ >
152
+ <ListboxButton
153
+ className={
154
+ props.fieldConfig.customClassNames?.fieldClassName
155
+ ? "form-listbox-select " +
156
+ props.fieldConfig.customClassNames.fieldClassName
157
+ : "form-listbox-select"
158
+ }
159
+ >
160
+ {renderListBoxValue(
161
+ formContext,
162
+ props.fieldConfig,
163
+ listOptions,
164
+ props.onChange
165
+ )}
166
+ </ListboxButton>
167
+ <RenderListOptions
168
+ formContext={formContext}
169
+ onChange={props.onChange}
170
+ formField={FormFieldType.DYNAMIC_MULTI_SELECT}
171
+ ref={dynamicSelectRef}
172
+ fieldConfig={props.fieldConfig}
173
+ listOptions={listOptions}
174
+ setListOptions={setListOptions}
175
+ loading={loading}
176
+ setLoading={setLoading}
177
+ />
178
+ </Listbox>
187
179
  );
180
+ }
181
+ if (props.fieldConfig.hideWhenNoResults && listOptions.length == 0) {
182
+ return <></>;
183
+ }
184
+
185
+ return (
186
+ <RenderFormField fieldConfig={props.fieldConfig} getInput={getInput} />
187
+ );
188
188
  };
189
189
  export default DynamicMultiSelect;
@@ -15,8 +15,7 @@ import useDymanicReducer from "../hooks/useDynamicReducer";
15
15
  import RenderFormField from "../util/RenderFormField";
16
16
  import RenderListOptions, {renderListBoxValue,} from "../util/RenderListOptions";
17
17
  import {handleChange, registerFormField} from "../util";
18
- import {reachoAPI} from "../../api/index";
19
- import axios from "axios";
18
+ import {getAxiosInstance} from "../../api";
20
19
 
21
20
  const DynamicSelect: React.FC<FormFieldComponentPropSchema> = (
22
21
  props: FormFieldComponentPropSchema
@@ -97,10 +96,10 @@ const DynamicSelect: React.FC<FormFieldComponentPropSchema> = (
97
96
  const fetchData = useCallback(async () => {
98
97
  if (!props.fieldConfig.fetchUrl) return;
99
98
 
99
+ const axiosInstance = getAxiosInstance(formContext.axiosInstance, props.fieldConfig);
100
+
100
101
  try {
101
- let response = await (props.fieldConfig.disableHeaderInFetch
102
- ? axios.get(props.fieldConfig.fetchUrl)
103
- : reachoAPI.get(props.fieldConfig.fetchUrl));
102
+ let response = await axiosInstance.get(props.fieldConfig.fetchUrl);
104
103
 
105
104
  if (response.data) {
106
105
  const data: FieldOptionsSchema[] = getListOptions(response.data, props.fieldConfig.optionsConfig);