@dhis2-ui/button 8.2.6 → 8.4.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/build/cjs/button/__tests__/Button.test.js +27 -5
- package/build/cjs/button/button.js +11 -1
- package/build/cjs/dropdown-button/__tests__/dropdown-button.test.js +1 -1
- package/build/cjs/dropdown-button/dropdown-button.js +1 -1
- package/build/cjs/split-button/split-button.js +1 -1
- package/build/es/button/__tests__/Button.test.js +19 -0
- package/build/es/button/button.js +11 -1
- package/build/es/dropdown-button/__tests__/dropdown-button.test.js +1 -1
- package/build/es/dropdown-button/dropdown-button.js +1 -1
- package/build/es/split-button/split-button.js +1 -1
- package/package.json +7 -7
|
@@ -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
|
|
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__*/
|
|
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__*/
|
|
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__*/
|
|
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__*/
|
|
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
|
};
|
|
@@ -39,7 +39,7 @@ describe('<DropdownButton>', () => {
|
|
|
39
39
|
// TODO: https://github.com/popperjs/react-popper/issues/350
|
|
40
40
|
const wrapper = (0, _enzyme.mount)(Component);
|
|
41
41
|
await (0, _testUtils.act)(async () => await null);
|
|
42
|
-
wrapper.find(_layer.Layer).simulate('click');
|
|
42
|
+
wrapper.find(_layer.Layer).find('.backdrop').simulate('click');
|
|
43
43
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
44
44
|
const args = onClick.mock.calls[0];
|
|
45
45
|
expect(args).toHaveLength(2);
|
|
@@ -157,7 +157,7 @@ class DropdownButton extends _react.Component {
|
|
|
157
157
|
}, children, /*#__PURE__*/_react.default.createElement(ArrowIconComponent, {
|
|
158
158
|
className: "jsx-3163060161" + " " + (arrow.className || "")
|
|
159
159
|
})), open && /*#__PURE__*/_react.default.createElement(_layer.Layer, {
|
|
160
|
-
|
|
160
|
+
onBackdropClick: this.onClickHandler,
|
|
161
161
|
transparent: true
|
|
162
162
|
}, /*#__PURE__*/_react.default.createElement(_popper.Popper, {
|
|
163
163
|
dataTest: "".concat(dataTest, "-popper"),
|
|
@@ -121,7 +121,7 @@ class SplitButton extends _react.Component {
|
|
|
121
121
|
className: (0, _classnames.default)(className, rightButton.className),
|
|
122
122
|
dataTest: "".concat(dataTest, "-toggle")
|
|
123
123
|
}, arrow), open && /*#__PURE__*/_react.default.createElement(_layer.Layer, {
|
|
124
|
-
|
|
124
|
+
onBackdropClick: this.onToggle,
|
|
125
125
|
transparent: true
|
|
126
126
|
}, /*#__PURE__*/_react.default.createElement(_popper.Popper, {
|
|
127
127
|
dataTest: "".concat(dataTest, "-menu"),
|
|
@@ -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
|
};
|
|
@@ -26,7 +26,7 @@ describe('<DropdownButton>', () => {
|
|
|
26
26
|
// TODO: https://github.com/popperjs/react-popper/issues/350
|
|
27
27
|
const wrapper = mount(Component);
|
|
28
28
|
await act(async () => await null);
|
|
29
|
-
wrapper.find(Layer).simulate('click');
|
|
29
|
+
wrapper.find(Layer).find('.backdrop').simulate('click');
|
|
30
30
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
31
31
|
const args = onClick.mock.calls[0];
|
|
32
32
|
expect(args).toHaveLength(2);
|
|
@@ -138,7 +138,7 @@ class DropdownButton extends Component {
|
|
|
138
138
|
}, children, /*#__PURE__*/React.createElement(ArrowIconComponent, {
|
|
139
139
|
className: "jsx-3163060161" + " " + (arrow.className || "")
|
|
140
140
|
})), open && /*#__PURE__*/React.createElement(Layer, {
|
|
141
|
-
|
|
141
|
+
onBackdropClick: this.onClickHandler,
|
|
142
142
|
transparent: true
|
|
143
143
|
}, /*#__PURE__*/React.createElement(Popper, {
|
|
144
144
|
dataTest: "".concat(dataTest, "-popper"),
|
|
@@ -100,7 +100,7 @@ class SplitButton extends Component {
|
|
|
100
100
|
className: cx(className, rightButton.className),
|
|
101
101
|
dataTest: "".concat(dataTest, "-toggle")
|
|
102
102
|
}, arrow), open && /*#__PURE__*/React.createElement(Layer, {
|
|
103
|
-
|
|
103
|
+
onBackdropClick: this.onToggle,
|
|
104
104
|
transparent: true
|
|
105
105
|
}, /*#__PURE__*/React.createElement(Popper, {
|
|
106
106
|
dataTest: "".concat(dataTest, "-menu"),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dhis2-ui/button",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.4.0",
|
|
4
4
|
"description": "UI Button",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"styled-jsx": "^4"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@dhis2/prop-types": "^3.
|
|
35
|
-
"@dhis2-ui/layer": "8.
|
|
36
|
-
"@dhis2-ui/loader": "8.
|
|
37
|
-
"@dhis2-ui/popper": "8.
|
|
38
|
-
"@dhis2/ui-constants": "8.
|
|
39
|
-
"@dhis2/ui-icons": "8.
|
|
34
|
+
"@dhis2/prop-types": "^3.1.2",
|
|
35
|
+
"@dhis2-ui/layer": "8.4.0",
|
|
36
|
+
"@dhis2-ui/loader": "8.4.0",
|
|
37
|
+
"@dhis2-ui/popper": "8.4.0",
|
|
38
|
+
"@dhis2/ui-constants": "8.4.0",
|
|
39
|
+
"@dhis2/ui-icons": "8.4.0",
|
|
40
40
|
"classnames": "^2.3.1",
|
|
41
41
|
"prop-types": "^15.7.2"
|
|
42
42
|
},
|