1 | import { createElement, useEffect, useLayoutEffect, useRef, Fragment, createContext } from 'react';
|
2 | import { node, bool, object, func } from 'prop-types';
|
3 | import clsx from 'clsx';
|
4 | import _extends from '@babel/runtime/helpers/esm/extends';
|
5 | import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
|
6 | import { makeStyles, withStyles, createStyles } from '@material-ui/core/styles';
|
7 | import Button from '@material-ui/core/Button';
|
8 | import DialogActions from '@material-ui/core/DialogActions';
|
9 | import DialogContent from '@material-ui/core/DialogContent';
|
10 | import Dialog from '@material-ui/core/Dialog';
|
11 | import Popover from '@material-ui/core/Popover';
|
12 |
|
13 |
|
14 | function arrayIncludes(array, itemOrItems) {
|
15 | if (Array.isArray(itemOrItems)) {
|
16 | return itemOrItems.every(function (item) {
|
17 | return array.indexOf(item) !== -1;
|
18 | });
|
19 | }
|
20 |
|
21 | return array.indexOf(itemOrItems) !== -1;
|
22 | }
|
23 |
|
24 | var DIALOG_WIDTH = 310;
|
25 | var DIALOG_WIDTH_WIDER = 325;
|
26 | var VIEW_HEIGHT = 305;
|
27 |
|
28 | var useStyles = makeStyles(function (theme) {
|
29 | return {
|
30 | staticWrapperRoot: {
|
31 | overflow: 'hidden',
|
32 | minWidth: DIALOG_WIDTH,
|
33 | display: 'flex',
|
34 | flexDirection: 'column',
|
35 | backgroundColor: theme.palette.background.paper
|
36 | }
|
37 | };
|
38 | }, {
|
39 | name: 'MuiPickersStaticWrapper'
|
40 | });
|
41 | var StaticWrapper = function StaticWrapper(_ref) {
|
42 | var children = _ref.children;
|
43 | var classes = useStyles();
|
44 | return createElement("div", {
|
45 | className: classes.staticWrapperRoot,
|
46 | children: children
|
47 | });
|
48 | };
|
49 |
|
50 | var ModalDialog = function ModalDialog(_ref) {
|
51 | var children = _ref.children,
|
52 | classes = _ref.classes,
|
53 | onAccept = _ref.onAccept,
|
54 | onDismiss = _ref.onDismiss,
|
55 | onClear = _ref.onClear,
|
56 | onSetToday = _ref.onSetToday,
|
57 | okLabel = _ref.okLabel,
|
58 | cancelLabel = _ref.cancelLabel,
|
59 | clearLabel = _ref.clearLabel,
|
60 | todayLabel = _ref.todayLabel,
|
61 | clearable = _ref.clearable,
|
62 | showTodayButton = _ref.showTodayButton,
|
63 | showTabs = _ref.showTabs,
|
64 | wider = _ref.wider,
|
65 | other = _objectWithoutProperties(_ref, ["children", "classes", "onAccept", "onDismiss", "onClear", "onSetToday", "okLabel", "cancelLabel", "clearLabel", "todayLabel", "clearable", "showTodayButton", "showTabs", "wider"]);
|
66 |
|
67 | return createElement(Dialog, _extends({
|
68 | role: "dialog",
|
69 | onClose: onDismiss,
|
70 | classes: {
|
71 | paper: clsx(classes.dialogRoot, wider && classes.dialogRootWider)
|
72 | }
|
73 | }, other), createElement(DialogContent, {
|
74 | children: children,
|
75 | className: classes.dialog
|
76 | }), createElement(DialogActions, {
|
77 | classes: {
|
78 | root: clsx((clearable || showTodayButton) && classes.withAdditionalAction)
|
79 | }
|
80 | }, clearable && createElement(Button, {
|
81 | color: "primary",
|
82 | onClick: onClear
|
83 | }, clearLabel), showTodayButton && createElement(Button, {
|
84 | color: "primary",
|
85 | onClick: onSetToday
|
86 | }, todayLabel), cancelLabel && createElement(Button, {
|
87 | color: "primary",
|
88 | onClick: onDismiss
|
89 | }, cancelLabel), okLabel && createElement(Button, {
|
90 | color: "primary",
|
91 | onClick: onAccept
|
92 | }, okLabel)));
|
93 | };
|
94 | ModalDialog.displayName = 'ModalDialog';
|
95 | var styles = createStyles({
|
96 | dialogRoot: {
|
97 | minWidth: DIALOG_WIDTH
|
98 | },
|
99 | dialogRootWider: {
|
100 | minWidth: DIALOG_WIDTH_WIDER
|
101 | },
|
102 | dialog: {
|
103 | '&:first-child': {
|
104 | padding: 0
|
105 | }
|
106 | },
|
107 | withAdditionalAction: {
|
108 |
|
109 |
|
110 | justifyContent: 'flex-start',
|
111 | '& > *:first-child': {
|
112 | marginRight: 'auto'
|
113 | }
|
114 | }
|
115 | });
|
116 | var ModalDialog$1 = withStyles(styles, {
|
117 | name: 'MuiPickersModal'
|
118 | })(ModalDialog);
|
119 |
|
120 | var useIsomorphicEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
121 | function runKeyHandler(e, keyHandlers) {
|
122 | var handler = keyHandlers[e.key];
|
123 |
|
124 | if (handler) {
|
125 | handler();
|
126 |
|
127 | e.preventDefault();
|
128 | }
|
129 | }
|
130 | function useKeyDown(active, keyHandlers) {
|
131 | var keyHandlersRef = useRef(keyHandlers);
|
132 | keyHandlersRef.current = keyHandlers;
|
133 | useIsomorphicEffect(function () {
|
134 | if (active) {
|
135 | var handleKeyDown = function handleKeyDown(event) {
|
136 | runKeyHandler(event, keyHandlersRef.current);
|
137 | };
|
138 |
|
139 | window.addEventListener('keydown', handleKeyDown);
|
140 | return function () {
|
141 | window.removeEventListener('keydown', handleKeyDown);
|
142 | };
|
143 | }
|
144 | }, [active]);
|
145 | }
|
146 |
|
147 | var ModalWrapper = function ModalWrapper(_ref) {
|
148 | var open = _ref.open,
|
149 | children = _ref.children,
|
150 | okLabel = _ref.okLabel,
|
151 | cancelLabel = _ref.cancelLabel,
|
152 | clearLabel = _ref.clearLabel,
|
153 | todayLabel = _ref.todayLabel,
|
154 | showTodayButton = _ref.showTodayButton,
|
155 | clearable = _ref.clearable,
|
156 | DialogProps = _ref.DialogProps,
|
157 | showTabs = _ref.showTabs,
|
158 | wider = _ref.wider,
|
159 | InputComponent = _ref.InputComponent,
|
160 | DateInputProps = _ref.DateInputProps,
|
161 | onClear = _ref.onClear,
|
162 | onAccept = _ref.onAccept,
|
163 | onDismiss = _ref.onDismiss,
|
164 | onSetToday = _ref.onSetToday,
|
165 | other = _objectWithoutProperties(_ref, ["open", "children", "okLabel", "cancelLabel", "clearLabel", "todayLabel", "showTodayButton", "clearable", "DialogProps", "showTabs", "wider", "InputComponent", "DateInputProps", "onClear", "onAccept", "onDismiss", "onSetToday"]);
|
166 |
|
167 | useKeyDown(open, {
|
168 | Enter: onAccept
|
169 | });
|
170 | return createElement(Fragment, null, createElement(InputComponent, _extends({}, other, DateInputProps)), createElement(ModalDialog$1, _extends({
|
171 | wider: wider,
|
172 | showTabs: showTabs,
|
173 | open: open,
|
174 | onClear: onClear,
|
175 | onAccept: onAccept,
|
176 | onDismiss: onDismiss,
|
177 | onSetToday: onSetToday,
|
178 | clearLabel: clearLabel,
|
179 | todayLabel: todayLabel,
|
180 | okLabel: okLabel,
|
181 | cancelLabel: cancelLabel,
|
182 | clearable: clearable,
|
183 | showTodayButton: showTodayButton,
|
184 | children: children
|
185 | }, DialogProps)));
|
186 | };
|
187 | process.env.NODE_ENV !== "production" ? ModalWrapper.propTypes = {
|
188 | okLabel: node,
|
189 | cancelLabel: node,
|
190 | clearLabel: node,
|
191 | clearable: bool,
|
192 | todayLabel: node,
|
193 | showTodayButton: bool,
|
194 | DialogProps: object
|
195 | } : void 0;
|
196 | ModalWrapper.defaultProps = {
|
197 | okLabel: 'OK',
|
198 | cancelLabel: 'Cancel',
|
199 | clearLabel: 'Clear',
|
200 | todayLabel: 'Today',
|
201 | clearable: false,
|
202 | showTodayButton: false
|
203 | };
|
204 |
|
205 | var InlineWrapper = function InlineWrapper(_ref) {
|
206 | var open = _ref.open,
|
207 | wider = _ref.wider,
|
208 | children = _ref.children,
|
209 | PopoverProps = _ref.PopoverProps,
|
210 | onClear = _ref.onClear,
|
211 | onDismiss = _ref.onDismiss,
|
212 | onSetToday = _ref.onSetToday,
|
213 | onAccept = _ref.onAccept,
|
214 | showTabs = _ref.showTabs,
|
215 | DateInputProps = _ref.DateInputProps,
|
216 | InputComponent = _ref.InputComponent,
|
217 | other = _objectWithoutProperties(_ref, ["open", "wider", "children", "PopoverProps", "onClear", "onDismiss", "onSetToday", "onAccept", "showTabs", "DateInputProps", "InputComponent"]);
|
218 |
|
219 | var ref = useRef();
|
220 | useKeyDown(open, {
|
221 | Enter: onAccept
|
222 | });
|
223 | return createElement(Fragment, null, createElement(InputComponent, _extends({}, other, DateInputProps, {
|
224 | inputRef: ref
|
225 | })), createElement(Popover, _extends({
|
226 | open: open,
|
227 | onClose: onDismiss,
|
228 | anchorEl: ref.current,
|
229 | anchorOrigin: {
|
230 | vertical: 'bottom',
|
231 | horizontal: 'center'
|
232 | },
|
233 | transformOrigin: {
|
234 | vertical: 'top',
|
235 | horizontal: 'center'
|
236 | },
|
237 | children: children
|
238 | }, PopoverProps)));
|
239 | };
|
240 | process.env.NODE_ENV !== "production" ? InlineWrapper.propTypes = {
|
241 | onOpen: func,
|
242 | onClose: func,
|
243 | PopoverProps: object
|
244 | } : void 0;
|
245 |
|
246 | function getWrapperFromVariant(variant) {
|
247 | switch (variant) {
|
248 | case 'inline':
|
249 | return InlineWrapper;
|
250 |
|
251 | case 'static':
|
252 | return StaticWrapper;
|
253 |
|
254 | default:
|
255 | return ModalWrapper;
|
256 | }
|
257 | }
|
258 | var VariantContext = createContext(null);
|
259 | var Wrapper = function Wrapper(_ref) {
|
260 | var variant = _ref.variant,
|
261 | props = _objectWithoutProperties(_ref, ["variant"]);
|
262 |
|
263 | var Component = getWrapperFromVariant(variant);
|
264 | return createElement(VariantContext.Provider, {
|
265 | value: variant || 'dialog'
|
266 | }, createElement(Component, props));
|
267 | };
|
268 |
|
269 | export { DIALOG_WIDTH as D, VariantContext as V, Wrapper as W, arrayIncludes as a, VIEW_HEIGHT as b, DIALOG_WIDTH_WIDER as c, runKeyHandler as r, useIsomorphicEffect as u };
|
270 |
|