@woosmap/ui 2.46.0 → 2.47.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@woosmap/ui",
3
- "version": "2.46.0",
3
+ "version": "2.47.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/WebGeoServices/ui.git"
@@ -289,6 +289,28 @@ class Dropdown extends Component {
289
289
  this.setOpen(false);
290
290
  };
291
291
 
292
+ onMouseEnter = () => {
293
+ const { openOnMouseEnter } = this.props;
294
+ if (openOnMouseEnter) {
295
+ if (this.mouseLeaveTimeout) {
296
+ clearTimeout(this.mouseLeaveTimeout);
297
+ }
298
+ this.setOpen(true);
299
+ }
300
+ };
301
+
302
+ onMouseLeave = () => {
303
+ const { openOnMouseEnter } = this.props;
304
+ if (openOnMouseEnter) {
305
+ if (this.mouseLeaveTimeout) {
306
+ clearTimeout(this.mouseLeaveTimeout);
307
+ }
308
+ this.mouseLeaveTimeout = setTimeout(() => {
309
+ this.setOpen(false);
310
+ }, 500);
311
+ }
312
+ };
313
+
292
314
  onClick = (e) => {
293
315
  const { onClick } = this.props;
294
316
  const { open } = this.state;
@@ -329,6 +351,8 @@ class Dropdown extends Component {
329
351
  ref={this.clickOutsideRef}
330
352
  className={cl('dropdown', { open }, `dropdown--${size}`, className)}
331
353
  {...rest}
354
+ onMouseEnter={this.onMouseEnter}
355
+ onMouseLeave={this.onMouseLeave}
332
356
  >
333
357
  <Button
334
358
  disabled={disabled}
@@ -357,6 +381,7 @@ Dropdown.defaultProps = {
357
381
  label: null,
358
382
  className: null,
359
383
  closeCb: null,
384
+ openOnMouseEnter: false,
360
385
  btnFace: 'link-flex',
361
386
  btnFaceIcon: 'caret-bottom',
362
387
  btnFaceIconSize: 24,
@@ -369,6 +394,7 @@ Dropdown.propTypes = {
369
394
  onDoubleClick: PropTypes.func,
370
395
  disabled: PropTypes.bool,
371
396
  disableCloseOutside: PropTypes.bool,
397
+ openOnMouseEnter: PropTypes.bool,
372
398
  label: PropTypes.string,
373
399
  testId: PropTypes.string,
374
400
  children: PropTypes.node.isRequired,
@@ -15,6 +15,10 @@ class Modal extends Component {
15
15
  this.modalRef = React.createRef();
16
16
  }
17
17
 
18
+ componentDidMount() {
19
+ this.handleEscFunction('add');
20
+ }
21
+
18
22
  componentDidUpdate() {
19
23
  const { isLoading } = this.state;
20
24
  const { hasErrors } = this.props;
@@ -23,6 +27,10 @@ class Modal extends Component {
23
27
  }
24
28
  }
25
29
 
30
+ componentWillUnmount() {
31
+ this.handleEscFunction();
32
+ }
33
+
26
34
  handleClickOutside = () => {
27
35
  this.close();
28
36
  };
@@ -31,6 +39,20 @@ class Modal extends Component {
31
39
  this.setState({ isLoading: loading });
32
40
  }
33
41
 
42
+ handleEscFunction = (key) => {
43
+ const { closesWithEscape } = this.props;
44
+ if (closesWithEscape) {
45
+ if (key === 'add') document.addEventListener('keydown', this.escFunction, false);
46
+ else document.removeEventListener('keydown', this.escFunction, false);
47
+ }
48
+ };
49
+
50
+ escFunction = (event) => {
51
+ if (event.keyCode === 27) {
52
+ this.close();
53
+ }
54
+ };
55
+
34
56
  open = () => {
35
57
  this.setState({ open: true });
36
58
  };
@@ -141,6 +163,7 @@ Modal.defaultProps = {
141
163
  validateBtnProps: {},
142
164
  mainButtonType: 'primary',
143
165
  testId: 'modal',
166
+ closesWithEscape: false,
144
167
  };
145
168
 
146
169
  Modal.propTypes = {
@@ -160,6 +183,7 @@ Modal.propTypes = {
160
183
  validateBtnProps: PropTypes.object,
161
184
  mainButtonType: PropTypes.string,
162
185
  testId: PropTypes.string,
186
+ closesWithEscape: PropTypes.bool,
163
187
  };
164
188
 
165
189
  export default withClickOutside(Modal, '.ignore-click-outside-modal');
@@ -78,3 +78,15 @@ const TemplateConfirmationModal = () => {
78
78
  };
79
79
  export const ModalConfirmation = TemplateConfirmationModal.bind({});
80
80
  Default.args = {};
81
+
82
+ const TemplateEscapeModal = () => {
83
+ const modalRef = React.createRef();
84
+ return (
85
+ <div style={{ paddingLeft: '50px' }}>
86
+ <Button onClick={() => modalRef.current.open()} label="Open modal" />
87
+ <Modal ref={modalRef} closesWithEscape title="My modal" />
88
+ </div>
89
+ );
90
+ };
91
+ export const ModalEscape = TemplateEscapeModal.bind({});
92
+ Default.args = {};
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { render, screen } from '@testing-library/react';
2
+ import { render, screen, fireEvent } from '@testing-library/react';
3
3
  import '@testing-library/jest-dom/extend-expect';
4
4
  import { act } from 'react-dom/test-utils';
5
5
  import userEvent from '@testing-library/user-event';
@@ -56,7 +56,7 @@ it('is hidden after clicking cancel', () => {
56
56
  }
57
57
  });
58
58
 
59
- it('is hidden after clicking cancel', () => {
59
+ it('is hidden after clicking validate', () => {
60
60
  const modalRef = React.createRef();
61
61
  const validateCb = jest.fn();
62
62
  render(
@@ -72,3 +72,19 @@ it('is hidden after clicking cancel', () => {
72
72
  userEvent.click(validate);
73
73
  expect(validateCb.mock.calls.length).toBe(1);
74
74
  });
75
+
76
+ it('is closing after escaping if it is set to be closed with it', () => {
77
+ const modalRef = React.createRef();
78
+ const closeCb = jest.fn();
79
+ render(
80
+ <Modal ref={modalRef} closesWithEscape closeCb={closeCb}>
81
+ content
82
+ </Modal>
83
+ );
84
+ act(() => {
85
+ modalRef.current.open();
86
+ });
87
+ expect(closeCb).not.toHaveBeenCalled();
88
+ fireEvent.keyDown(document, { keyCode: 27 });
89
+ expect(closeCb).toHaveBeenCalled();
90
+ });