@dhis2-ui/button 8.3.1 → 8.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
 
3
+ var _react = require("@testing-library/react");
4
+
3
5
  var _enzyme = require("enzyme");
4
6
 
5
- var _react = _interopRequireDefault(require("react"));
7
+ var _react2 = _interopRequireDefault(require("react"));
6
8
 
7
9
  var _button = require("../button.js");
8
10
 
@@ -11,7 +13,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
11
13
  describe('<Button>', () => {
12
14
  it('renders a default data-test attribute', () => {
13
15
  const dataTest = 'dhis2-uicore-button';
14
- const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react.default.createElement(_button.Button, {
16
+ const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react2.default.createElement(_button.Button, {
15
17
  dataTest: dataTest
16
18
  }));
17
19
  const actual = wrapper.find({
@@ -21,7 +23,7 @@ describe('<Button>', () => {
21
23
  });
22
24
  it('renders a custom data-test attribute', () => {
23
25
  const dataTest = 'button-data-test';
24
- const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react.default.createElement(_button.Button, {
26
+ const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react2.default.createElement(_button.Button, {
25
27
  dataTest: dataTest
26
28
  }));
27
29
  const actual = wrapper.find({
@@ -31,16 +33,36 @@ describe('<Button>', () => {
31
33
  });
32
34
  describe('toggle', () => {
33
35
  it('should have class "toggled" if toggled-prop is true', () => {
34
- const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react.default.createElement(_button.Button, {
36
+ const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react2.default.createElement(_button.Button, {
35
37
  toggled: true
36
38
  }));
37
39
  const actual = wrapper.find('button');
38
40
  expect(actual.hasClass('toggled')).toBe(true);
39
41
  });
40
42
  it('should not have class "toggled" if toggled-prop is not passed', () => {
41
- const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react.default.createElement(_button.Button, null));
43
+ const wrapper = (0, _enzyme.mount)( /*#__PURE__*/_react2.default.createElement(_button.Button, null));
42
44
  const actual = wrapper.find('button');
43
45
  expect(actual.hasClass('toggled')).toBe(false);
44
46
  });
45
47
  });
48
+ it('should call the onKeyDown callback when provided', () => {
49
+ const onKeyDown = jest.fn();
50
+ (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_button.Button, {
51
+ name: "button-name",
52
+ value: "button-value",
53
+ onKeyDown: onKeyDown
54
+ }, "btn"));
55
+
56
+ _react.fireEvent.keyDown(_react.screen.getByRole('button'), {
57
+ key: 'Enter',
58
+ code: 'Enter',
59
+ charCode: 13
60
+ });
61
+
62
+ expect(onKeyDown).toHaveBeenCalledWith({
63
+ name: 'button-name',
64
+ value: 'button-value'
65
+ }, expect.objectContaining({}));
66
+ expect(onKeyDown).toHaveBeenCalledTimes(1);
67
+ });
46
68
  });
@@ -46,6 +46,7 @@ const Button = _ref => {
46
46
  onBlur,
47
47
  onClick,
48
48
  onFocus,
49
+ onKeyDown,
49
50
  loading
50
51
  } = _ref;
51
52
  const ref = (0, _react.useRef)();
@@ -70,6 +71,11 @@ const Button = _ref => {
70
71
  name
71
72
  }, event);
72
73
 
74
+ const handleKeyDown = event => onKeyDown && onKeyDown({
75
+ value,
76
+ name
77
+ }, event);
78
+
73
79
  const iconOnly = icon && !children;
74
80
  const buttonClassName = (0, _classnames.default)(className, {
75
81
  primary,
@@ -91,6 +97,7 @@ const Button = _ref => {
91
97
  onBlur: handleBlur,
92
98
  onClick: handleClick,
93
99
  onFocus: handleFocus,
100
+ onKeyDown: handleKeyDown,
94
101
  className: "jsx-".concat(_buttonStyles.default.__hash) + " " + (buttonClassName || "")
95
102
  }, loading && /*#__PURE__*/_react.default.createElement("span", {
96
103
  className: "jsx-".concat(_buttonStyles.default.__hash) + " " + "loader"
@@ -195,5 +202,8 @@ Button.propTypes = {
195
202
  onClick: _propTypes.default.func,
196
203
 
197
204
  /** Callback to trigger on focus. Called with same args as `onClick` */
198
- onFocus: _propTypes.default.func
205
+ onFocus: _propTypes.default.func,
206
+
207
+ /** Callback to trigger on key-down. Called with same args as `onClick` */
208
+ onKeyDown: _propTypes.default.func
199
209
  };
@@ -1,3 +1,4 @@
1
+ import { render, fireEvent, screen } from '@testing-library/react';
1
2
  import { mount } from 'enzyme';
2
3
  import React from 'react';
3
4
  import { Button } from '../button.js';
@@ -36,4 +37,22 @@ describe('<Button>', () => {
36
37
  expect(actual.hasClass('toggled')).toBe(false);
37
38
  });
38
39
  });
40
+ it('should call the onKeyDown callback when provided', () => {
41
+ const onKeyDown = jest.fn();
42
+ render( /*#__PURE__*/React.createElement(Button, {
43
+ name: "button-name",
44
+ value: "button-value",
45
+ onKeyDown: onKeyDown
46
+ }, "btn"));
47
+ fireEvent.keyDown(screen.getByRole('button'), {
48
+ key: 'Enter',
49
+ code: 'Enter',
50
+ charCode: 13
51
+ });
52
+ expect(onKeyDown).toHaveBeenCalledWith({
53
+ name: 'button-name',
54
+ value: 'button-value'
55
+ }, expect.objectContaining({}));
56
+ expect(onKeyDown).toHaveBeenCalledTimes(1);
57
+ });
39
58
  });
@@ -26,6 +26,7 @@ export const Button = _ref => {
26
26
  onBlur,
27
27
  onClick,
28
28
  onFocus,
29
+ onKeyDown,
29
30
  loading
30
31
  } = _ref;
31
32
  const ref = useRef();
@@ -50,6 +51,11 @@ export const Button = _ref => {
50
51
  name
51
52
  }, event);
52
53
 
54
+ const handleKeyDown = event => onKeyDown && onKeyDown({
55
+ value,
56
+ name
57
+ }, event);
58
+
53
59
  const iconOnly = icon && !children;
54
60
  const buttonClassName = cx(className, {
55
61
  primary,
@@ -71,6 +77,7 @@ export const Button = _ref => {
71
77
  onBlur: handleBlur,
72
78
  onClick: handleClick,
73
79
  onFocus: handleFocus,
80
+ onKeyDown: handleKeyDown,
74
81
  className: "jsx-".concat(styles.__hash) + " " + (buttonClassName || "")
75
82
  }, loading && /*#__PURE__*/React.createElement("span", {
76
83
  className: "jsx-".concat(styles.__hash) + " " + "loader"
@@ -173,5 +180,8 @@ Button.propTypes = {
173
180
  onClick: PropTypes.func,
174
181
 
175
182
  /** Callback to trigger on focus. Called with same args as `onClick` */
176
- onFocus: PropTypes.func
183
+ onFocus: PropTypes.func,
184
+
185
+ /** Callback to trigger on key-down. Called with same args as `onClick` */
186
+ onKeyDown: PropTypes.func
177
187
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2-ui/button",
3
- "version": "8.3.1",
3
+ "version": "8.4.2",
4
4
  "description": "UI Button",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,11 +32,11 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@dhis2/prop-types": "^3.1.2",
35
- "@dhis2-ui/layer": "8.3.1",
36
- "@dhis2-ui/loader": "8.3.1",
37
- "@dhis2-ui/popper": "8.3.1",
38
- "@dhis2/ui-constants": "8.3.1",
39
- "@dhis2/ui-icons": "8.3.1",
35
+ "@dhis2-ui/layer": "8.4.2",
36
+ "@dhis2-ui/loader": "8.4.2",
37
+ "@dhis2-ui/popper": "8.4.2",
38
+ "@dhis2/ui-constants": "8.4.2",
39
+ "@dhis2/ui-icons": "8.4.2",
40
40
  "classnames": "^2.3.1",
41
41
  "prop-types": "^15.7.2"
42
42
  },