-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathFormGrid.tsx
More file actions
168 lines (161 loc) · 4.63 KB
/
FormGrid.tsx
File metadata and controls
168 lines (161 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { useFormioContext } from '../hooks/useFormioContext';
import { Form as FormType } from '@formio/core';
import { usePagination } from '../hooks/usePagination';
import { JSON } from './Form';
import { ReactNode, useCallback } from 'react';
import type { JSX } from 'react';
export type Action = {
name: string;
fn: (id: string) => void;
};
type SomeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
type FormFromServer = SomeRequired<FormType, '_id'>;
export type ComponentProp<T = object> = (props: T) => JSX.Element;
export type FormGridProps = {
actions?: Action[];
forms?: FormFromServer[];
components?: {
Container?: ComponentProp<{ children: ReactNode }>;
FormContainer?: ComponentProp<{ children: ReactNode; form?: FormFromServer }>;
FormNameContainer?: ComponentProp<{
children: ReactNode;
onClick?: () => void;
form?: FormFromServer;
}>;
FormMetaContainer?: ComponentProp<{ form: FormFromServer }>;
FormActionsContainer?: ComponentProp<{ children: ReactNode }>;
FormActionButton?: ComponentProp<{
action: Action;
onClick: () => void;
}>;
PaginationContainer?: ComponentProp<{ children: ReactNode }>;
PaginationButton?: ComponentProp<{
children: ReactNode;
isActive?: boolean;
disabled?: boolean;
onClick: () => void;
}>;
};
onFormClick?: (id: string) => void;
formQuery?: {
[key: string]: JSON;
};
limit?: number;
};
type PaginationResponse = FormType[] & { serverCount: number };
const isFormioPaginationResponse = (obj: any): obj is PaginationResponse => {
return obj.serverCount !== undefined && Array.isArray(obj);
};
export const DEFAULT_COMPONENTS = {};
const DEFAULT_QUERY = {};
export const FormGrid = ({
actions,
components = DEFAULT_COMPONENTS,
onFormClick,
forms,
formQuery = DEFAULT_QUERY,
limit = 10,
}: FormGridProps) => {
const {
Container = ({ children }) => <div>{children}</div>,
FormContainer = ({ children }) => <div>{children}</div>,
FormNameContainer = ({ children, onClick }) => (
<div onClick={onClick}>{children}</div>
),
FormMetaContainer,
FormActionsContainer = ({ children }) => <div>{children}</div>,
FormActionButton = ({ action }) => (
<button type="button">{action?.name}</button>
),
PaginationContainer = ({ children }) => <ul>{children}</ul>,
PaginationButton = ({ children }) => <li>{children}</li>,
} = components;
const { Formio } = useFormioContext();
const fetchFunction = useCallback(
(limit: number, skip: number) => {
const formio = new Formio('/form');
return formio.loadForms({ params: { ...formQuery, limit, skip } });
},
[formQuery, Formio],
);
const dataOrFnArg = forms ? forms : fetchFunction;
const { data, total, page, nextPage, prevPage, setPage, hasMore } =
usePagination<FormFromServer>(1, limit, dataOrFnArg);
const defaultActions = [
{ name: 'Edit', fn: (id: string) => onFormClick?.(id) },
{
name: 'Delete',
fn: async (id: string) => {
if (
window.confirm('Are you sure you want to delete this form?')
) {
const formio = new Formio(`/form/${id}`);
await formio.deleteForm();
setPage(1);
}
},
},
];
const formActions = actions || defaultActions;
return (
<Container>
{data.map((form) => (
<FormContainer key={form._id} form={form}>
<FormNameContainer onClick={() => onFormClick?.(form._id)} form={form}>
{form.title || form.name || form._id}
</FormNameContainer>
<FormActionsContainer>
{formActions.map((action, index) => (
<FormActionButton
action={action}
onClick={() => action.fn(form._id)}
key={`${action.name}-${index}`}
/>
))}
</FormActionsContainer>
{FormMetaContainer ? <FormMetaContainer form={form} /> : null}
</FormContainer>
))}
<PaginationContainer>
<PaginationButton onClick={prevPage} disabled={page === 1}>
Prev
</PaginationButton>
{isFormioPaginationResponse(data) &&
!total &&
Array.from(
{
length: Math.ceil(data.serverCount / limit),
},
(_, i) => i + 1,
).map((n) => (
<PaginationButton
key={`page-link-${n}`}
onClick={() => setPage(n)}
isActive={n === page}
>
{n}
</PaginationButton>
))}
{data &&
total &&
Array.from(
{
length: Math.ceil(total / limit),
},
(_, i) => i + 1,
).map((n) => (
<PaginationButton
key={`page-link-${n}`}
onClick={() => setPage(n)}
isActive={n === page}
>
{n}
</PaginationButton>
))}
<PaginationButton onClick={nextPage} disabled={!hasMore}>
Next
</PaginationButton>
</PaginationContainer>
</Container>
);
};