/
ModalPicker.tsx
189 lines (167 loc) · 4.2 KB
/
ModalPicker.tsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, { useEffect } from 'react';
import { Page } from '@42.nl/spring-connect';
import {
Modal,
ModalHeader,
ModalBody,
ModalFooter,
Button,
Row,
Col
} from 'reactstrap';
import Pagination from '../../core/Pagination/Pagination';
import { t } from '../../utilities/translation/translation';
import SearchInput from '../../core/SearchInput/SearchInput';
import { useBodyFixOnModalClose } from '../../core/useBodyFixOnModalClose/useBodyFixOnModalClose';
interface Text {
placeholder?: string;
cancel?: string;
select?: string;
}
interface Props {
/**
* The placeholder of the form element.
*/
placeholder: string;
/**
* Whether or not the modal is open.
*/
isOpen: boolean;
/**
* The current page of options. Used to determine if the
* pagination should be rendered.
*/
page: Page<any>;
/**
* Here the component using the ModalPicker must render in the options.
*/
children: React.ReactNode;
/**
* The value to show in the search input.
*/
query: string;
/**
* Whether or not to show the search input.
*/
canSearch: boolean;
/**
* Callback to fetch the options to display to the user.
*/
fetchOptions: (query: string) => void;
/**
* Callback for when the page changes.
*/
pageChanged: (page: number) => void;
/**
* Callback for when the modal should close.
*/
closeModal: () => void;
/**
* Callback for when the modal is saved
*/
modalSaved: () => void;
/**
* Whether or not the save button is enabled
*/
saveButtonEnabled: boolean;
/**
* Optionally an add button to display in the Modal. Can
* be used to dynamically add an option which was not there
* before.
*/
addButton?: {
label: string;
onClick: () => void;
};
/**
* Optionally customized text within the component.
* This text should already be translated.
*/
text?: Text;
}
/**
* The ModalPicker component is an abstraction used by the ModalPickerMultiple
* and ModalPickerSingle to render a modal. It contains the code which
* could be shared by the two components.
*/
export default function ModalPicker(props: Props) {
const {
placeholder,
isOpen,
page,
children,
query,
canSearch,
fetchOptions,
pageChanged,
closeModal,
modalSaved,
saveButtonEnabled,
addButton,
text = {}
} = props;
useBodyFixOnModalClose(isOpen);
const shouldRenderPagination = !(page.first && page.last);
return (
<Modal isOpen={isOpen} toggle={() => closeModal()}>
<ModalHeader toggle={() => closeModal()}>{placeholder}</ModalHeader>
<ModalBody>
{canSearch ? (
<Row>
<Col>
<SearchInput
value={query}
placeholder={t({
overrideText: text.placeholder,
key: 'ModalPicker.SEARCH',
fallback: 'Search...'
})}
onChange={value => fetchOptions(value)}
/>
</Col>
</Row>
) : null}
{children}
</ModalBody>
{shouldRenderPagination ? (
<ModalFooter className="d-flex justify-content-center">
<Pagination page={page} onChange={page => pageChanged(page)} />
</ModalFooter>
) : null}
<ModalFooter className="d-flex flex-row justify-content-between">
{addButton ? (
<Button color="primary" onClick={() => addButton.onClick()}>
{addButton.label}
</Button>
) : (
<div />
)}
<div>
<Button
className="ml-1"
color="secondary"
onClick={() => closeModal()}
>
{t({
overrideText: text.cancel,
key: 'ModalPicker.CANCEL',
fallback: 'Cancel'
})}
</Button>
<Button
className="ml-1"
color="primary"
onClick={() => modalSaved()}
disabled={!saveButtonEnabled}
>
{t({
overrideText: text.select,
key: 'ModalPicker.SELECT',
fallback: 'Select'
})}
</Button>
</div>
</ModalFooter>
</Modal>
);
}