@jasperoosthoek/react-toolbox 0.5.4 → 0.5.6
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/LICENSE +1 -1
- package/change-log.md +7 -0
- package/dist/components/forms/FormModal.d.ts +41 -0
- package/dist/components/forms/FormModalProvider.d.ts +42 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/package.json +2 -9
- package/src/components/forms/{CreateEditModal.tsx → FormModal.tsx} +5 -4
- package/src/components/forms/{CreateEditModalProvider.tsx → FormModalProvider.tsx} +29 -21
- package/src/components/tables/DataTable.tsx +3 -3
- package/src/index.ts +2 -2
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jasperoosthoek/react-toolbox",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6",
|
|
4
4
|
"author": "jasperoosthoek",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/jasperoosthoek/react-toolbox.git"
|
|
9
9
|
},
|
|
10
|
-
"description": "
|
|
10
|
+
"description": "Extensive library of React components that work together with react-bootstrap",
|
|
11
11
|
"files": [
|
|
12
12
|
"src/",
|
|
13
13
|
"dist/",
|
|
@@ -31,13 +31,6 @@
|
|
|
31
31
|
"css-loader": "^6.7.1",
|
|
32
32
|
"jest": "^29.5.0",
|
|
33
33
|
"jest-environment-jsdom": "^29.5.0",
|
|
34
|
-
"moment": "^2.29.4",
|
|
35
|
-
"react": "^18.3.1",
|
|
36
|
-
"react-bootstrap": "^2.7.4",
|
|
37
|
-
"react-dnd": "^16.0.1",
|
|
38
|
-
"react-dom": "^18.3.1",
|
|
39
|
-
"react-icons": "^5.3.0",
|
|
40
|
-
"react-localization": "^1.0.19",
|
|
41
34
|
"style-loader": "^3.3.1",
|
|
42
35
|
"ts-jest": "^29.1.0",
|
|
43
36
|
"ts-loader": "^9.4.2",
|
|
@@ -32,7 +32,7 @@ export type Validate = (state: any) => any
|
|
|
32
32
|
export type ModalTitle = ReactElement | string;
|
|
33
33
|
|
|
34
34
|
export type Width = 25 | 50 | 75 | 100;
|
|
35
|
-
export type
|
|
35
|
+
export type FormModalProps<
|
|
36
36
|
T extends FormFields,
|
|
37
37
|
K extends IncludeData<T>
|
|
38
38
|
> = {
|
|
@@ -49,7 +49,7 @@ export type CreateEditModalProps<
|
|
|
49
49
|
width?: Width;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
export const
|
|
52
|
+
export const FormModal = <
|
|
53
53
|
T extends FormFields,
|
|
54
54
|
K extends IncludeData<T>
|
|
55
55
|
>({
|
|
@@ -65,9 +65,10 @@ export const CreateEditModal = <
|
|
|
65
65
|
dialogClassName='',
|
|
66
66
|
width,
|
|
67
67
|
...restProps
|
|
68
|
-
}:
|
|
68
|
+
}: FormModalProps<T, K>) => {
|
|
69
|
+
|
|
69
70
|
if (Object.values(restProps).length !==0) {
|
|
70
|
-
console.error(`Unrecognised props given to
|
|
71
|
+
console.error(`Unrecognised props given to FormModal:`, restProps);
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
const getInitialFormData = () => ({
|
|
@@ -9,26 +9,28 @@ import {
|
|
|
9
9
|
Validate,
|
|
10
10
|
ModalTitle,
|
|
11
11
|
Width,
|
|
12
|
-
|
|
12
|
+
FormModal,
|
|
13
13
|
|
|
14
|
-
} from './
|
|
14
|
+
} from './FormModal';
|
|
15
15
|
import { FormValue } from './FormFields';
|
|
16
16
|
import { ButtonProps, CreateButton, EditButton } from '../buttons/IconButtons';
|
|
17
|
-
const out_of_context_error = 'The useError hook should only be used in a child of the CreateEditModalProvider component.';
|
|
18
17
|
|
|
19
18
|
export type ShowCreateModal = (show?: boolean) => void;
|
|
20
19
|
export type ShowEditModal<T, K> = (state: { [key in keyof T]: FormValue } & K) => void;
|
|
21
20
|
|
|
22
21
|
export type ShowCreateModalButton = ButtonProps;
|
|
23
22
|
export const ShowCreateModalButton = ({ onClick, ...props }: ButtonProps) => {
|
|
24
|
-
const { showCreateModal } =
|
|
23
|
+
const { showCreateModal, hasProvider } = useFormModal();
|
|
25
24
|
|
|
26
25
|
return (
|
|
27
26
|
<CreateButton
|
|
28
27
|
{...props}
|
|
29
|
-
onClick={(e) => {
|
|
28
|
+
onClick={(e) => {
|
|
29
|
+
// A onClick function was given to CreateButton
|
|
30
30
|
if (onClick) onClick(e);
|
|
31
|
-
|
|
31
|
+
// CreateButton is inside a CreateEditButtonProvider which can handle
|
|
32
|
+
// showCreateModal. Without the provider, showCreateModal will log an error
|
|
33
|
+
if (hasProvider) showCreateModal();
|
|
32
34
|
}}
|
|
33
35
|
/>
|
|
34
36
|
)
|
|
@@ -37,40 +39,45 @@ export interface ShowEditModalButtonProps<T, K> extends ButtonProps {
|
|
|
37
39
|
state: { [key in keyof T]: FormValue } & K;
|
|
38
40
|
}
|
|
39
41
|
export const ShowEditModalButton = ({ state, onClick, ... props }: ShowEditModalButtonProps<T, K>) => {
|
|
40
|
-
const { showEditModal } =
|
|
42
|
+
const { showEditModal, hasProvider } = useFormModal();
|
|
41
43
|
|
|
42
44
|
return (
|
|
43
45
|
<EditButton
|
|
44
46
|
{...props}
|
|
45
47
|
onClick={(e) => {
|
|
48
|
+
// A onClick function was given to CreateButton
|
|
46
49
|
if (onClick) onClick(e);
|
|
47
|
-
|
|
50
|
+
// CreateButton is inside a CreateEditButtonProvider which can handle
|
|
51
|
+
// showEditModal. Without the provider, showEditModal will log an error
|
|
52
|
+
if (hasProvider) showEditModal(state);
|
|
48
53
|
}}
|
|
49
54
|
/>
|
|
50
55
|
)
|
|
51
56
|
}
|
|
52
57
|
|
|
53
|
-
type
|
|
58
|
+
type FormModalContextType<T, K> = {
|
|
54
59
|
showCreateModal: ShowCreateModal;
|
|
55
60
|
showEditModal: ShowEditModal<T, K>;
|
|
61
|
+
hasProvider: boolean;
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
type T = any;
|
|
59
65
|
type K = any;
|
|
60
|
-
const defaultErrorState:
|
|
66
|
+
const defaultErrorState: FormModalContextType<T, K> = {
|
|
61
67
|
showCreateModal: () => {
|
|
62
|
-
console.error(
|
|
68
|
+
console.error('The showCreateModal function should only be used in a child of the FormModalProvider component.');
|
|
63
69
|
},
|
|
64
|
-
showEditModal:
|
|
65
|
-
console.error(
|
|
70
|
+
showEditModal: (state: { [key in keyof T]: FormValue } & K) => {
|
|
71
|
+
console.error('The showEditModal function should only be used in a child of the FormModalProvider component.');
|
|
66
72
|
},
|
|
73
|
+
hasProvider: false,
|
|
67
74
|
};
|
|
68
|
-
export const
|
|
75
|
+
export const FormModalContext = React.createContext(defaultErrorState);
|
|
69
76
|
|
|
70
|
-
export const
|
|
77
|
+
export const useFormModal = () => useContext(FormModalContext);
|
|
71
78
|
|
|
72
79
|
|
|
73
|
-
export type
|
|
80
|
+
export type FormModalProviderProps<
|
|
74
81
|
T extends FormFields,
|
|
75
82
|
K extends IncludeData<T>
|
|
76
83
|
> = {
|
|
@@ -88,7 +95,7 @@ export type CreateEditModalProviderProps<
|
|
|
88
95
|
width?: Width;
|
|
89
96
|
children: ReactNode;
|
|
90
97
|
}
|
|
91
|
-
export const
|
|
98
|
+
export const FormModalProvider: React.FC<FormModalProviderProps<T, K>> = ({
|
|
92
99
|
createModalTitle,
|
|
93
100
|
editModalTitle,
|
|
94
101
|
formFields,
|
|
@@ -104,16 +111,17 @@ export const CreateEditModalProvider: React.FC<CreateEditModalProviderProps<T, K
|
|
|
104
111
|
const [instanceInEditModal, showEditModal] = useState<({ [key in keyof T]: FormValue } & K) | null>(null);
|
|
105
112
|
const { strings } = useLocalization();
|
|
106
113
|
return (
|
|
107
|
-
<
|
|
114
|
+
<FormModalContext.Provider
|
|
108
115
|
value={{
|
|
109
116
|
showCreateModal: (show?: boolean) =>
|
|
110
117
|
showCreateModal(typeof show === 'undefined' ? true : show),
|
|
111
118
|
showEditModal,
|
|
119
|
+
hasProvider: true,
|
|
112
120
|
}}>
|
|
113
121
|
{children}
|
|
114
122
|
|
|
115
123
|
{createModalActive && (onCreate || onSave) && (
|
|
116
|
-
<
|
|
124
|
+
<FormModal
|
|
117
125
|
modalTitle={createModalTitle || strings.getString('modal_create')}
|
|
118
126
|
onHide={() => showCreateModal(false)}
|
|
119
127
|
initialState={initialState}
|
|
@@ -125,7 +133,7 @@ export const CreateEditModalProvider: React.FC<CreateEditModalProviderProps<T, K
|
|
|
125
133
|
/>
|
|
126
134
|
)}
|
|
127
135
|
{instanceInEditModal && (onUpdate || onSave) && (
|
|
128
|
-
<
|
|
136
|
+
<FormModal
|
|
129
137
|
show={!!instanceInEditModal}
|
|
130
138
|
modalTitle={editModalTitle || strings.getString('modal_edit')}
|
|
131
139
|
onHide={() => showEditModal(null)}
|
|
@@ -137,6 +145,6 @@ export const CreateEditModalProvider: React.FC<CreateEditModalProviderProps<T, K
|
|
|
137
145
|
dialogClassName={dialogClassName}
|
|
138
146
|
/>
|
|
139
147
|
)}
|
|
140
|
-
</
|
|
148
|
+
</FormModalContext.Provider>
|
|
141
149
|
);
|
|
142
150
|
};
|
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
import { CloseButton } from '../buttons/IconButtons';
|
|
25
25
|
import { DragAndDropList, DragAndDropListComponent, DragAndDropListComponentProps } from './DragAndDropList';
|
|
26
26
|
import { useLocalization } from '../../localization/LocalizationContext';
|
|
27
|
-
import {
|
|
27
|
+
import { useFormModal } from '../forms/FormModalProvider';
|
|
28
28
|
|
|
29
29
|
const PaginationButton = (props: ButtonProps) => (
|
|
30
30
|
<Button variant='outline-secondary' size='sm' {...props} />
|
|
@@ -109,7 +109,7 @@ export const DataTable = <D extends any[]>({
|
|
|
109
109
|
style,
|
|
110
110
|
...restProps
|
|
111
111
|
}: DataTableProps<D>) => {
|
|
112
|
-
const { showEditModal } =
|
|
112
|
+
const { showEditModal, hasProvider } = useFormModal();
|
|
113
113
|
type R = D[number];
|
|
114
114
|
|
|
115
115
|
if (Object.keys(restProps).length !== 0) console.error('Unrecognised props:', restProps);
|
|
@@ -177,7 +177,7 @@ export const DataTable = <D extends any[]>({
|
|
|
177
177
|
{...{ ...(typeof onClickRow === 'function' || showEditModal)
|
|
178
178
|
? { onClick: () => {
|
|
179
179
|
if (onClickRow) onClickRow(row);
|
|
180
|
-
if (
|
|
180
|
+
if (hasProvider) showEditModal(row);
|
|
181
181
|
} }
|
|
182
182
|
: {}
|
|
183
183
|
}}
|
package/src/index.ts
CHANGED
|
@@ -4,8 +4,8 @@ export { default as ConfirmButton } from './components/buttons/ConfirmButton';
|
|
|
4
4
|
export { default as DeleteConfirmButton } from './components/buttons/DeleteConfirmButton';
|
|
5
5
|
export * from './components/buttons/DeleteConfirmButton';
|
|
6
6
|
|
|
7
|
-
export * from './components/forms/
|
|
8
|
-
export * from './components/forms/
|
|
7
|
+
export * from './components/forms/FormModal';
|
|
8
|
+
export * from './components/forms/FormModalProvider';
|
|
9
9
|
export * from './components/forms/FormFields';
|
|
10
10
|
|
|
11
11
|
export * from './components/indicators/LoadingIndicator';
|