@dhis2-ui/select 10.12.5 → 10.12.7

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.
@@ -17,6 +17,10 @@ const WithClearButton = () => {
17
17
  return /*#__PURE__*/_react.default.createElement(_simpleSingleSelect.SimpleSingleSelect, {
18
18
  clearable: true,
19
19
  name: "simple",
20
+ onClear: () => setSelected({
21
+ label: '',
22
+ value: ''
23
+ }),
20
24
  selected: selected,
21
25
  onChange: setSelected,
22
26
  options: _options.fiveOptions
@@ -153,7 +153,9 @@ function SimpleSingleSelect(_ref2) {
153
153
  listBoxRef,
154
154
  focussedOptionIndex,
155
155
  setFocussedOptionIndex,
156
- selectFocussedOption
156
+ selectFocussedOption,
157
+ clearable,
158
+ onClear
157
159
  });
158
160
  const handleKeyDownOnFilterInput = (0, _index4.useHandleKeyPressOnFilterInput)({
159
161
  value: selectedValue,
@@ -2,9 +2,10 @@
2
2
 
3
3
  require("@testing-library/jest-dom");
4
4
  var _react = require("@testing-library/react");
5
- var _react2 = _interopRequireDefault(require("react"));
5
+ var _react2 = _interopRequireWildcard(require("react"));
6
6
  var _simpleSingleSelect = require("./simple-single-select.js");
7
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
7
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
8
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
8
9
  describe('<SimpleSingleSelect />', () => {
9
10
  beforeAll(() => {
10
11
  const consoleError = console.error;
@@ -620,6 +621,93 @@ describe('<SimpleSingleSelect />', () => {
620
621
  label: 'Foo'
621
622
  });
622
623
  });
624
+ describe('Clear the selected value when ESCAPE is pressed', () => {
625
+ it('should clear the selected value when closed, clearable and user presses Escape', () => {
626
+ function TestSelect() {
627
+ const [selected, setSelected] = (0, _react2.useState)({
628
+ label: 'Foo',
629
+ value: 'foo'
630
+ });
631
+ return /*#__PURE__*/_react2.default.createElement(_simpleSingleSelect.SimpleSingleSelect, {
632
+ clearable: true,
633
+ name: "simple",
634
+ selected: selected,
635
+ onClear: () => setSelected({
636
+ label: '',
637
+ value: ''
638
+ }),
639
+ options: [{
640
+ value: '',
641
+ label: 'None'
642
+ }, {
643
+ value: 'foo',
644
+ label: 'Foo'
645
+ }, {
646
+ value: 'bar',
647
+ label: 'Bar'
648
+ }]
649
+ });
650
+ }
651
+ (0, _react.render)(/*#__PURE__*/_react2.default.createElement(TestSelect, null));
652
+
653
+ // menu is closed
654
+ expect(_react.screen.queryByRole('listbox')).toBeNull();
655
+
656
+ // there are selected value
657
+ expect(_react.screen.getByText('Foo')).toBeInTheDocument();
658
+
659
+ // press Escape once to clear the selected value
660
+ _react.fireEvent.keyDown(_react.screen.getByRole('combobox'), {
661
+ key: 'Escape'
662
+ });
663
+
664
+ // the value is cleared
665
+ expect(_react.screen.queryByText('Foo')).toBeNull();
666
+ });
667
+ it('should clear the selected value when open and ESCAPE is pressed twice', () => {
668
+ const onClear = jest.fn();
669
+ (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_simpleSingleSelect.SimpleSingleSelect, {
670
+ clearable: true,
671
+ name: "simple",
672
+ selected: {
673
+ value: 'bar',
674
+ label: 'Bar'
675
+ },
676
+ onChange: () => {},
677
+ onClear: onClear,
678
+ clearText: "Clear",
679
+ options: [{
680
+ value: '',
681
+ label: 'None'
682
+ }, {
683
+ value: 'foo',
684
+ label: 'Foo'
685
+ }, {
686
+ value: 'bar',
687
+ label: 'Bar'
688
+ }]
689
+ }));
690
+ const comboBox = _react.screen.getByRole('combobox');
691
+
692
+ // open the menu
693
+ expect(comboBox.attributes.getNamedItem('aria-expanded').value).toBe('false');
694
+ _react.fireEvent.click(comboBox);
695
+ expect(comboBox.attributes.getNamedItem('aria-expanded').value).toBe('true');
696
+
697
+ // The first escape should close the combo BUT not call clear
698
+ _react.fireEvent.keyDown(_react.screen.getByRole('combobox'), {
699
+ key: 'Escape'
700
+ });
701
+ expect(comboBox.attributes.getNamedItem('aria-expanded').value).toBe('false');
702
+ expect(onClear).not.toHaveBeenCalledWith();
703
+
704
+ // The second escape should call clear
705
+ _react.fireEvent.keyDown(_react.screen.getByRole('combobox'), {
706
+ key: 'Escape'
707
+ });
708
+ expect(onClear).toHaveBeenCalledWith();
709
+ });
710
+ });
623
711
  it('should not select the next option when closed, disabled and user presses ArrowDown', () => {
624
712
  const onChange = jest.fn();
625
713
  (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_simpleSingleSelect.SimpleSingleSelect, {
@@ -45,7 +45,9 @@ function useHandleKeyPressOnCombobox(_ref2) {
45
45
  focussedOptionIndex,
46
46
  setFocussedOptionIndex,
47
47
  selectFocussedOption,
48
- onChange
48
+ onChange,
49
+ clearable,
50
+ onClear
49
51
  } = _ref2;
50
52
  const {
51
53
  onTyping,
@@ -136,6 +138,10 @@ function useHandleKeyPressOnCombobox(_ref2) {
136
138
  closeMenu();
137
139
  return;
138
140
  }
141
+ if (!expanded && clearable && key === 'Escape') {
142
+ onClear === null || onClear === void 0 ? void 0 : onClear();
143
+ return;
144
+ }
139
145
  if (!expanded && (key === ' ' && !typing || key === 'Enter' || key === 'ArrowUp' && altKey || key === 'ArrowDown' && altKey)) {
140
146
  openMenu();
141
147
  return;
@@ -9,6 +9,10 @@ export const WithClearButton = () => {
9
9
  return /*#__PURE__*/React.createElement(SimpleSingleSelect, {
10
10
  clearable: true,
11
11
  name: "simple",
12
+ onClear: () => setSelected({
13
+ label: '',
14
+ value: ''
15
+ }),
12
16
  selected: selected,
13
17
  onChange: setSelected,
14
18
  options: fiveOptions
@@ -144,7 +144,9 @@ export function SimpleSingleSelect(_ref2) {
144
144
  listBoxRef,
145
145
  focussedOptionIndex,
146
146
  setFocussedOptionIndex,
147
- selectFocussedOption
147
+ selectFocussedOption,
148
+ clearable,
149
+ onClear
148
150
  });
149
151
  const handleKeyDownOnFilterInput = useHandleKeyPressOnFilterInput({
150
152
  value: selectedValue,
@@ -1,6 +1,6 @@
1
1
  import '@testing-library/jest-dom';
2
2
  import { render, fireEvent, screen } from '@testing-library/react';
3
- import React from 'react';
3
+ import React, { useState } from 'react';
4
4
  import { SimpleSingleSelect } from './simple-single-select.js';
5
5
  describe('<SimpleSingleSelect />', () => {
6
6
  beforeAll(() => {
@@ -617,6 +617,93 @@ describe('<SimpleSingleSelect />', () => {
617
617
  label: 'Foo'
618
618
  });
619
619
  });
620
+ describe('Clear the selected value when ESCAPE is pressed', () => {
621
+ it('should clear the selected value when closed, clearable and user presses Escape', () => {
622
+ function TestSelect() {
623
+ const [selected, setSelected] = useState({
624
+ label: 'Foo',
625
+ value: 'foo'
626
+ });
627
+ return /*#__PURE__*/React.createElement(SimpleSingleSelect, {
628
+ clearable: true,
629
+ name: "simple",
630
+ selected: selected,
631
+ onClear: () => setSelected({
632
+ label: '',
633
+ value: ''
634
+ }),
635
+ options: [{
636
+ value: '',
637
+ label: 'None'
638
+ }, {
639
+ value: 'foo',
640
+ label: 'Foo'
641
+ }, {
642
+ value: 'bar',
643
+ label: 'Bar'
644
+ }]
645
+ });
646
+ }
647
+ render(/*#__PURE__*/React.createElement(TestSelect, null));
648
+
649
+ // menu is closed
650
+ expect(screen.queryByRole('listbox')).toBeNull();
651
+
652
+ // there are selected value
653
+ expect(screen.getByText('Foo')).toBeInTheDocument();
654
+
655
+ // press Escape once to clear the selected value
656
+ fireEvent.keyDown(screen.getByRole('combobox'), {
657
+ key: 'Escape'
658
+ });
659
+
660
+ // the value is cleared
661
+ expect(screen.queryByText('Foo')).toBeNull();
662
+ });
663
+ it('should clear the selected value when open and ESCAPE is pressed twice', () => {
664
+ const onClear = jest.fn();
665
+ render(/*#__PURE__*/React.createElement(SimpleSingleSelect, {
666
+ clearable: true,
667
+ name: "simple",
668
+ selected: {
669
+ value: 'bar',
670
+ label: 'Bar'
671
+ },
672
+ onChange: () => {},
673
+ onClear: onClear,
674
+ clearText: "Clear",
675
+ options: [{
676
+ value: '',
677
+ label: 'None'
678
+ }, {
679
+ value: 'foo',
680
+ label: 'Foo'
681
+ }, {
682
+ value: 'bar',
683
+ label: 'Bar'
684
+ }]
685
+ }));
686
+ const comboBox = screen.getByRole('combobox');
687
+
688
+ // open the menu
689
+ expect(comboBox.attributes.getNamedItem('aria-expanded').value).toBe('false');
690
+ fireEvent.click(comboBox);
691
+ expect(comboBox.attributes.getNamedItem('aria-expanded').value).toBe('true');
692
+
693
+ // The first escape should close the combo BUT not call clear
694
+ fireEvent.keyDown(screen.getByRole('combobox'), {
695
+ key: 'Escape'
696
+ });
697
+ expect(comboBox.attributes.getNamedItem('aria-expanded').value).toBe('false');
698
+ expect(onClear).not.toHaveBeenCalledWith();
699
+
700
+ // The second escape should call clear
701
+ fireEvent.keyDown(screen.getByRole('combobox'), {
702
+ key: 'Escape'
703
+ });
704
+ expect(onClear).toHaveBeenCalledWith();
705
+ });
706
+ });
620
707
  it('should not select the next option when closed, disabled and user presses ArrowDown', () => {
621
708
  const onChange = jest.fn();
622
709
  render(/*#__PURE__*/React.createElement(SimpleSingleSelect, {
@@ -39,7 +39,9 @@ export function useHandleKeyPressOnCombobox(_ref2) {
39
39
  focussedOptionIndex,
40
40
  setFocussedOptionIndex,
41
41
  selectFocussedOption,
42
- onChange
42
+ onChange,
43
+ clearable,
44
+ onClear
43
45
  } = _ref2;
44
46
  const {
45
47
  onTyping,
@@ -130,6 +132,10 @@ export function useHandleKeyPressOnCombobox(_ref2) {
130
132
  closeMenu();
131
133
  return;
132
134
  }
135
+ if (!expanded && clearable && key === 'Escape') {
136
+ onClear === null || onClear === void 0 ? void 0 : onClear();
137
+ return;
138
+ }
133
139
  if (!expanded && (key === ' ' && !typing || key === 'Enter' || key === 'ArrowUp' && altKey || key === 'ArrowDown' && altKey)) {
134
140
  openMenu();
135
141
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2-ui/select",
3
- "version": "10.12.5",
3
+ "version": "10.12.7",
4
4
  "description": "UI Select",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,21 +33,21 @@
33
33
  "styled-jsx": "^4"
34
34
  },
35
35
  "dependencies": {
36
- "@dhis2-ui/box": "10.12.5",
37
- "@dhis2-ui/button": "10.12.5",
38
- "@dhis2-ui/card": "10.12.5",
39
- "@dhis2-ui/checkbox": "10.12.5",
40
- "@dhis2-ui/chip": "10.12.5",
41
- "@dhis2-ui/field": "10.12.5",
42
- "@dhis2-ui/input": "10.12.5",
43
- "@dhis2-ui/layer": "10.12.5",
44
- "@dhis2-ui/loader": "10.12.5",
45
- "@dhis2-ui/popper": "10.12.5",
46
- "@dhis2-ui/status-icon": "10.12.5",
47
- "@dhis2-ui/tooltip": "10.12.5",
36
+ "@dhis2-ui/box": "10.12.7",
37
+ "@dhis2-ui/button": "10.12.7",
38
+ "@dhis2-ui/card": "10.12.7",
39
+ "@dhis2-ui/checkbox": "10.12.7",
40
+ "@dhis2-ui/chip": "10.12.7",
41
+ "@dhis2-ui/field": "10.12.7",
42
+ "@dhis2-ui/input": "10.12.7",
43
+ "@dhis2-ui/layer": "10.12.7",
44
+ "@dhis2-ui/loader": "10.12.7",
45
+ "@dhis2-ui/popper": "10.12.7",
46
+ "@dhis2-ui/status-icon": "10.12.7",
47
+ "@dhis2-ui/tooltip": "10.12.7",
48
48
  "@dhis2/prop-types": "^3.1.2",
49
- "@dhis2/ui-constants": "10.12.5",
50
- "@dhis2/ui-icons": "10.12.5",
49
+ "@dhis2/ui-constants": "10.12.7",
50
+ "@dhis2/ui-icons": "10.12.7",
51
51
  "classnames": "^2.3.1",
52
52
  "prop-types": "^15.7.2"
53
53
  },