@dhis2-ui/switch 8.3.1 → 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.
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ var _react = require("@testing-library/react");
4
+
5
+ var _react2 = _interopRequireDefault(require("react"));
6
+
7
+ var _switch = require("../switch.js");
8
+
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+
11
+ describe('<Switch />', () => {
12
+ it('should call the onKeyDown callback when provided', () => {
13
+ const onKeyDown = jest.fn();
14
+ (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_switch.Switch, {
15
+ name: "foo",
16
+ value: "bar",
17
+ checked: false,
18
+ onKeyDown: onKeyDown
19
+ }));
20
+
21
+ _react.fireEvent.keyDown(_react.screen.getByRole('switch'), {
22
+ key: 'Enter',
23
+ code: 'Enter',
24
+ charCode: 13
25
+ });
26
+
27
+ expect(onKeyDown).toHaveBeenCalledWith({
28
+ name: 'foo',
29
+ value: 'bar',
30
+ checked: true
31
+ }, expect.objectContaining({}));
32
+ expect(onKeyDown).toHaveBeenCalledTimes(1);
33
+ });
34
+ });
@@ -48,6 +48,12 @@ class Switch extends _react.Component {
48
48
  this.props.onFocus(this.createHandlerPayload(), e);
49
49
  }
50
50
  });
51
+
52
+ _defineProperty(this, "handleKeyDown", e => {
53
+ if (this.props.onKeyDown) {
54
+ this.props.onKeyDown(this.createHandlerPayload(), e);
55
+ }
56
+ });
51
57
  }
52
58
 
53
59
  componentDidMount() {
@@ -107,6 +113,7 @@ class Switch extends _react.Component {
107
113
  tabIndex: tabIndex,
108
114
  onChange: this.handleChange,
109
115
  onFocus: this.handleFocus,
116
+ onKeyDown: this.handleKeyDown,
110
117
  onBlur: this.handleBlur,
111
118
  className: _style.default.dynamic([["3592976892", [_uiConstants.colors.grey900, _uiConstants.theme.disabled, _uiConstants.theme.focus]]])
112
119
  }), /*#__PURE__*/_react.default.createElement("div", {
@@ -174,5 +181,8 @@ Switch.propTypes = {
174
181
  onChange: _propTypes.default.func,
175
182
 
176
183
  /** Called with signature `({ name: string, value: string, checked: bool }, event)` */
177
- onFocus: _propTypes.default.func
184
+ onFocus: _propTypes.default.func,
185
+
186
+ /** Called with signature `({ name: string, value: string, checked: bool }, event)` */
187
+ onKeyDown: _propTypes.default.func
178
188
  };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ var _react = require("@testing-library/react");
4
+
5
+ var _react2 = _interopRequireDefault(require("react"));
6
+
7
+ var _switchField = require("../switch-field.js");
8
+
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+
11
+ describe('<Switch />', () => {
12
+ it('should call the onKeyDown callback when provided', () => {
13
+ const onKeyDown = jest.fn();
14
+ (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_switchField.SwitchField, {
15
+ label: "label",
16
+ name: "foo",
17
+ value: "bar",
18
+ checked: false,
19
+ onKeyDown: onKeyDown
20
+ }));
21
+
22
+ _react.fireEvent.keyDown(_react.screen.getByRole('switch'), {
23
+ key: 'Enter',
24
+ code: 'Enter',
25
+ charCode: 13
26
+ });
27
+
28
+ expect(onKeyDown).toHaveBeenCalledWith({
29
+ name: 'foo',
30
+ value: 'bar',
31
+ checked: true
32
+ }, expect.objectContaining({}));
33
+ expect(onKeyDown).toHaveBeenCalledTimes(1);
34
+ });
35
+ });
@@ -45,6 +45,7 @@ const SwitchField = _ref2 => {
45
45
  tabIndex,
46
46
  onChange,
47
47
  onFocus,
48
+ onKeyDown,
48
49
  onBlur,
49
50
  checked,
50
51
  disabled,
@@ -80,6 +81,7 @@ const SwitchField = _ref2 => {
80
81
  tabIndex: tabIndex,
81
82
  onChange: onChange,
82
83
  onFocus: onFocus,
84
+ onKeyDown: onKeyDown,
83
85
  onBlur: onBlur,
84
86
  checked: checked,
85
87
  disabled: disabled,
@@ -142,5 +144,8 @@ SwitchField.propTypes = {
142
144
  onChange: _propTypes.default.func,
143
145
 
144
146
  /** Called with signature ({ name: string, value: string, checked: bool }, event) */
145
- onFocus: _propTypes.default.func
147
+ onFocus: _propTypes.default.func,
148
+
149
+ /** Called with signature ({ name: string, value: string, checked: bool }, event) */
150
+ onKeyDown: _propTypes.default.func
146
151
  };
@@ -0,0 +1,25 @@
1
+ import { render, fireEvent, screen } from '@testing-library/react';
2
+ import React from 'react';
3
+ import { Switch } from '../switch.js';
4
+ describe('<Switch />', () => {
5
+ it('should call the onKeyDown callback when provided', () => {
6
+ const onKeyDown = jest.fn();
7
+ render( /*#__PURE__*/React.createElement(Switch, {
8
+ name: "foo",
9
+ value: "bar",
10
+ checked: false,
11
+ onKeyDown: onKeyDown
12
+ }));
13
+ fireEvent.keyDown(screen.getByRole('switch'), {
14
+ key: 'Enter',
15
+ code: 'Enter',
16
+ charCode: 13
17
+ });
18
+ expect(onKeyDown).toHaveBeenCalledWith({
19
+ name: 'foo',
20
+ value: 'bar',
21
+ checked: true
22
+ }, expect.objectContaining({}));
23
+ expect(onKeyDown).toHaveBeenCalledTimes(1);
24
+ });
25
+ });
@@ -31,6 +31,12 @@ class Switch extends Component {
31
31
  this.props.onFocus(this.createHandlerPayload(), e);
32
32
  }
33
33
  });
34
+
35
+ _defineProperty(this, "handleKeyDown", e => {
36
+ if (this.props.onKeyDown) {
37
+ this.props.onKeyDown(this.createHandlerPayload(), e);
38
+ }
39
+ });
34
40
  }
35
41
 
36
42
  componentDidMount() {
@@ -90,6 +96,7 @@ class Switch extends Component {
90
96
  tabIndex: tabIndex,
91
97
  onChange: this.handleChange,
92
98
  onFocus: this.handleFocus,
99
+ onKeyDown: this.handleKeyDown,
93
100
  onBlur: this.handleBlur,
94
101
  className: _JSXStyle.dynamic([["3592976892", [colors.grey900, theme.disabled, theme.focus]]])
95
102
  }), /*#__PURE__*/React.createElement("div", {
@@ -156,6 +163,9 @@ Switch.propTypes = {
156
163
  onChange: PropTypes.func,
157
164
 
158
165
  /** Called with signature `({ name: string, value: string, checked: bool }, event)` */
159
- onFocus: PropTypes.func
166
+ onFocus: PropTypes.func,
167
+
168
+ /** Called with signature `({ name: string, value: string, checked: bool }, event)` */
169
+ onKeyDown: PropTypes.func
160
170
  };
161
171
  export { Switch };
@@ -0,0 +1,26 @@
1
+ import { render, fireEvent, screen } from '@testing-library/react';
2
+ import React from 'react';
3
+ import { SwitchField } from '../switch-field.js';
4
+ describe('<Switch />', () => {
5
+ it('should call the onKeyDown callback when provided', () => {
6
+ const onKeyDown = jest.fn();
7
+ render( /*#__PURE__*/React.createElement(SwitchField, {
8
+ label: "label",
9
+ name: "foo",
10
+ value: "bar",
11
+ checked: false,
12
+ onKeyDown: onKeyDown
13
+ }));
14
+ fireEvent.keyDown(screen.getByRole('switch'), {
15
+ key: 'Enter',
16
+ code: 'Enter',
17
+ charCode: 13
18
+ });
19
+ expect(onKeyDown).toHaveBeenCalledWith({
20
+ name: 'foo',
21
+ value: 'bar',
22
+ checked: true
23
+ }, expect.objectContaining({}));
24
+ expect(onKeyDown).toHaveBeenCalledTimes(1);
25
+ });
26
+ });
@@ -31,6 +31,7 @@ const SwitchField = _ref2 => {
31
31
  tabIndex,
32
32
  onChange,
33
33
  onFocus,
34
+ onKeyDown,
34
35
  onBlur,
35
36
  checked,
36
37
  disabled,
@@ -66,6 +67,7 @@ const SwitchField = _ref2 => {
66
67
  tabIndex: tabIndex,
67
68
  onChange: onChange,
68
69
  onFocus: onFocus,
70
+ onKeyDown: onKeyDown,
69
71
  onBlur: onBlur,
70
72
  checked: checked,
71
73
  disabled: disabled,
@@ -127,6 +129,9 @@ SwitchField.propTypes = {
127
129
  onChange: PropTypes.func,
128
130
 
129
131
  /** Called with signature ({ name: string, value: string, checked: bool }, event) */
130
- onFocus: PropTypes.func
132
+ onFocus: PropTypes.func,
133
+
134
+ /** Called with signature ({ name: string, value: string, checked: bool }, event) */
135
+ onKeyDown: PropTypes.func
131
136
  };
132
137
  export { SwitchField };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2-ui/switch",
3
- "version": "8.3.1",
3
+ "version": "8.4.0",
4
4
  "description": "UI Switch",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,9 +32,9 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@dhis2/prop-types": "^3.1.2",
35
- "@dhis2-ui/field": "8.3.1",
36
- "@dhis2-ui/required": "8.3.1",
37
- "@dhis2/ui-constants": "8.3.1",
35
+ "@dhis2-ui/field": "8.4.0",
36
+ "@dhis2-ui/required": "8.4.0",
37
+ "@dhis2/ui-constants": "8.4.0",
38
38
  "classnames": "^2.3.1",
39
39
  "prop-types": "^15.7.2"
40
40
  },