@nestio/pam 2.0.6 → 2.0.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.
Files changed (49) hide show
  1. package/examples/components/Select.jsx +26 -0
  2. package/examples/index.html +5 -0
  3. package/lib/components/Collapse.js +50 -0
  4. package/lib/components/MultiSelect.js +3 -1
  5. package/lib/components/MultiSelectOption.js +22 -3
  6. package/lib/components/OptionListSet.js +11 -3
  7. package/lib/components/SelectCore.js +84 -26
  8. package/lib/components/SelectOption.js +44 -11
  9. package/lib/components/SingleSelect.js +2 -0
  10. package/lib/components/TagBoxMenuSelect.js +2 -0
  11. package/lib/components/styles/MultiSelectOption.scss +4 -0
  12. package/lib/components/styles/SelectOption.scss +12 -0
  13. package/lib/components/styles/_variables.scss +1 -0
  14. package/lib/components/styles/icons/icons.css +1 -57
  15. package/lib/constants.js +7 -0
  16. package/package.json +1 -1
  17. package/src/components/Collapse.jsx +30 -0
  18. package/src/components/MultiSelect.jsx +2 -0
  19. package/src/components/MultiSelectOption.jsx +21 -3
  20. package/src/components/OptionListSet.jsx +9 -2
  21. package/src/components/SelectCore.jsx +62 -11
  22. package/src/components/SelectOption.jsx +27 -8
  23. package/src/components/SingleSelect.jsx +2 -0
  24. package/src/components/TagBoxMenuSelect.jsx +2 -0
  25. package/src/components/__tests__/Collapse.spec.js +26 -0
  26. package/src/components/__tests__/MultiSelectOption.spec.js +42 -2
  27. package/src/components/__tests__/OptionListSet.spec.js +24 -1
  28. package/src/components/__tests__/SelectCore.spec.js +103 -0
  29. package/src/components/__tests__/SelectOption.spec.js +33 -2
  30. package/src/components/__tests__/SingleSelectOption.spec.js +2 -1
  31. package/src/components/styles/MultiSelectOption.scss +4 -0
  32. package/src/components/styles/SelectOption.scss +12 -0
  33. package/src/components/styles/_variables.scss +1 -0
  34. package/src/components/styles/icons/icons.css +1 -57
  35. package/src/constants.js +2 -0
  36. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  37. package/.idea/inspectionProfiles/profiles_settings.xml +0 -6
  38. package/.idea/misc.xml +0 -4
  39. package/.idea/modules.xml +0 -8
  40. package/.idea/pam.iml +0 -15
  41. package/.idea/vcs.xml +0 -6
  42. package/lib/components/styles/icons/05a4d7fb.eot +0 -0
  43. package/lib/components/styles/icons/05a4d7fb.otf +0 -0
  44. package/lib/components/styles/icons/05a4d7fb.ttf +0 -0
  45. package/lib/components/styles/icons/05a4d7fb.woff +0 -0
  46. package/src/components/styles/icons/05a4d7fb.eot +0 -0
  47. package/src/components/styles/icons/05a4d7fb.otf +0 -0
  48. package/src/components/styles/icons/05a4d7fb.ttf +0 -0
  49. package/src/components/styles/icons/05a4d7fb.woff +0 -0
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var OPTION_COLLAPSED = exports.OPTION_COLLAPSED = 'option-collapsed';
7
+ var OPTION_NOT_COLLAPSED = exports.OPTION_NOT_COLLAPSED = 'option-not-collapsed';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestio/pam",
3
- "version": "2.0.6",
3
+ "version": "2.0.7",
4
4
  "description": "Nestio UI modules",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -0,0 +1,30 @@
1
+ import cn from 'classnames';
2
+ import PropTypes from 'prop-types';
3
+ import React from 'react';
4
+
5
+ import { OPTION_COLLAPSED } from '../constants';
6
+ import icons from './styles/icons/icons.css';
7
+ import styles from './styles/SelectOption.scss';
8
+
9
+ export default function Collapse ({onClick, collapsibleState}) {
10
+ return (
11
+ <span
12
+ onClick={onClick}
13
+ className={styles['collapse']}
14
+ role="button"
15
+ >
16
+ <i className={
17
+ cn(
18
+ icons.pamicon,
19
+ collapsibleState == OPTION_COLLAPSED ? icons['pamicon-triangle-right'] : icons['pamicon-triangle-down'],
20
+ styles['collapse-icon']
21
+ )
22
+ } />
23
+ </span>
24
+ );
25
+ }
26
+
27
+ Collapse.propTypes = {
28
+ onClick: PropTypes.func,
29
+ collapsibleState: PropTypes.string
30
+ };
@@ -36,12 +36,14 @@ export default function MultiSelect (props){
36
36
  value={props.value}
37
37
  valueAdapter={valueAdapter}
38
38
  ValueComponent={props.ValueComponent}
39
+ collapsibleOptions={props.collapsibleOptions}
39
40
  />
40
41
  );
41
42
  }
42
43
 
43
44
  MultiSelect.propTypes = {
44
45
  ariaLabel: PropTypes.string,
46
+ collapsibleOptions: PropTypes.array,
45
47
  filterable: PropTypes.bool,
46
48
  menuContainerClassName: PropTypes.string,
47
49
  menuContainerText: PropTypes.string,
@@ -10,11 +10,27 @@ import styles from './styles/MultiSelectOption.scss';
10
10
  OptionComponent designed for use in MultiSelects
11
11
  */
12
12
  export default function MultiSelectOption (props) {
13
- const { isSelected, isHighlighted, option } = props;
13
+ const { isSelected, isHighlighted, option, currentText, collapsibleOptions } = props;
14
+
15
+ function _getCollapsibleClassName(){
16
+ if (currentText) {
17
+ return null;
18
+ }
19
+
20
+ if (collapsibleOptions && collapsibleOptions.length) {
21
+ if (option.collapsedBy && collapsibleOptions.indexOf(option.collapsedBy) > -1) {
22
+ return styles['option__indented'];
23
+ }
24
+ }
25
+
26
+ return null;
27
+ }
28
+
14
29
  const optionClass = classnames(
15
30
  styles.option,
16
31
  option.disabled && styles.disabled,
17
- isHighlighted && styles.highlighted
32
+ isHighlighted && styles.highlighted,
33
+ _getCollapsibleClassName()
18
34
  );
19
35
 
20
36
  const checkboxClass = classnames({[styles.checked]: isSelected}, {[styles.unchecked]: !isSelected});
@@ -35,7 +51,9 @@ MultiSelectOption.propTypes = {
35
51
  isHighlighted: PropTypes.bool.isRequired,
36
52
  isSelected: PropTypes.bool.isRequired,
37
53
  option: PropTypes.object.isRequired,
38
- renderOptionLabel: PropTypes.func
54
+ renderOptionLabel: PropTypes.func,
55
+ collapsibleOptions: PropTypes.array,
56
+ currentText: PropTypes.string
39
57
  };
40
58
 
41
59
  MultiSelectOption.defaultProps = {
@@ -81,13 +81,13 @@ export default class OptionListSet extends Component {
81
81
  }
82
82
 
83
83
  renderOptionSet(options, label) {
84
- const { OptionComponent, EmptyOptionComponent, renderOptionLabel, optionIndex, valueIsSelected, optionSets } = this.props;
85
-
84
+ const { OptionComponent, EmptyOptionComponent, renderOptionLabel, optionIndex, valueIsSelected, optionSets, getCollapsibleState } = this.props;
86
85
  let renderedOptions;
87
86
 
88
87
  if (options.length) {
89
88
  renderedOptions = options.map((option, index) => {
90
89
  index += option.groupIndex || 0;
90
+
91
91
  const rendered = (
92
92
  <OptionComponent
93
93
  id={this.getOptionId(option, index)}
@@ -97,6 +97,7 @@ export default class OptionListSet extends Component {
97
97
  isSelected={valueIsSelected(option)}
98
98
  renderOptionLabel={renderOptionLabel}
99
99
  isHighlighted={index === optionIndex}
100
+ collapsibleState={getCollapsibleState(option.value)}
100
101
  {...this.props}
101
102
  />
102
103
  );
@@ -206,6 +207,7 @@ OptionListSet.propTypes = {
206
207
  OptionComponent
207
208
  */
208
209
  renderOptionLabel: PropTypes.func,
210
+
209
211
  /**
210
212
  Currently highlighted option
211
213
  */
@@ -214,6 +216,11 @@ OptionListSet.propTypes = {
214
216
  OptionComponent to be rendered in OptionList
215
217
  */
216
218
  OptionComponent: PropTypes.func,
219
+ /**
220
+ Function used to determine if an option is collapsed and can be collapsed
221
+ */
222
+
223
+ getCollapsibleState: PropTypes.func,
217
224
  /**
218
225
  Function used to determine if individual options are selected
219
226
  */
@@ -10,6 +10,8 @@ import uniqueId from 'lodash/uniqueId';
10
10
 
11
11
  import styles from './styles/SelectCore.scss';
12
12
 
13
+ import { OPTION_COLLAPSED, OPTION_NOT_COLLAPSED } from '../constants';
14
+
13
15
  /**
14
16
  The primary Select component which manages the state for the entire select and renders subcomponents. It controls
15
17
  whether the dropdown is opened/closed, which option is currently selected, which open has focus, filtering options
@@ -28,6 +30,7 @@ class SelectCore extends Component {
28
30
  this.changeValue = this.changeValue.bind(this);
29
31
  this.clearValue = this.clearValue.bind(this);
30
32
  this.closeMenu = this.closeMenu.bind(this);
33
+ this.getCollapsibleState = this.getCollapsibleState.bind(this);
31
34
  this.onBackspace = this.onBackspace.bind(this);
32
35
  this.onDown = this.onDown.bind(this);
33
36
  this.onEnter = this.onEnter.bind(this);
@@ -37,6 +40,7 @@ class SelectCore extends Component {
37
40
  this.onEscape = this.onEscape.bind(this);
38
41
  this.openMenu = this.openMenu.bind(this);
39
42
  this.setIndex = this.setIndex.bind(this);
43
+ this.toggleCollapsedValue = this.toggleCollapsedValue.bind(this);
40
44
  this.toggleValue = this.toggleValue.bind(this);
41
45
  this.valueIsSelected = this.valueIsSelected.bind(this);
42
46
 
@@ -48,7 +52,8 @@ class SelectCore extends Component {
48
52
  isOpen: false,
49
53
  optionIndex: this._getInitialOptionIndex(props),
50
54
  options: props.options || null,
51
- loading: false
55
+ loading: false,
56
+ collapsedOptions: {}
52
57
  };
53
58
 
54
59
  this.menuRef = React.createRef();
@@ -102,10 +107,16 @@ class SelectCore extends Component {
102
107
  }
103
108
 
104
109
  componentDidUpdate(prevProps, prevState) {
105
- const { onOptionsFiltered } = this.props;
110
+ const { onOptionsFiltered, onChangeText } = this.props;
111
+
112
+ if (prevState.currentText !== this.state.currentText) {
113
+ if (onOptionsFiltered) {
114
+ onOptionsFiltered(this._getFilteredOptions());
115
+ }
106
116
 
107
- if (onOptionsFiltered && prevState.currentText !== this.state.currentText) {
108
- onOptionsFiltered(this._getFilteredOptions());
117
+ if (onChangeText) {
118
+ onChangeText(this.state.currentText);
119
+ }
109
120
  }
110
121
  }
111
122
 
@@ -351,6 +362,17 @@ class SelectCore extends Component {
351
362
  });
352
363
  }
353
364
 
365
+ toggleCollapsedValue(value){
366
+ this.setState(currentState => {
367
+ const currentCollapsedState = !!currentState.collapsedOptions[value];
368
+ const newValue = Object.assign({}, currentState.collapsedOptions, {[value]: !currentCollapsedState});
369
+
370
+ return {
371
+ collapsedOptions: newValue
372
+ };
373
+ });
374
+ }
375
+
354
376
  // Async functions
355
377
  /**
356
378
  * This is typically triggered when the select gets passed a new value and is used to populate the select with the
@@ -400,6 +422,29 @@ class SelectCore extends Component {
400
422
  return this.props.valueAdapter.valueIsSelected(this.state.currentValue, newValue);
401
423
  }
402
424
 
425
+ getCollapsibleState(value){
426
+ if (!this.props.collapsibleOptions) {
427
+ return null;
428
+ }
429
+
430
+ if (this.state.currentText) {
431
+ return null;
432
+ }
433
+
434
+ const isCollapsible = this.props.collapsibleOptions.indexOf(value) > -1;
435
+
436
+ if (!isCollapsible) {
437
+ return null;
438
+ }
439
+
440
+
441
+ if (this.state.collapsedOptions[value]) {
442
+ return OPTION_COLLAPSED;
443
+ }
444
+
445
+ return OPTION_NOT_COLLAPSED;
446
+ }
447
+
403
448
  // Private methods and helpers
404
449
  /**
405
450
  * Returns options filtered by props.filterOption and currentText. Is memoized to reduce repeating same computation
@@ -415,13 +460,19 @@ class SelectCore extends Component {
415
460
  return this.state.options;
416
461
  }
417
462
 
418
- const args = [this.state.options, this.props.filterOption, text];
419
- if (this._cachedArgs && this._cachedArgs[0] == args[0] && this._cachedArgs[1] == args[1] && this._cachedArgs[2] == args[2]) {
463
+ const args = [this.state.options, this.props.filterOption, text, this.state.collapsedOptions];
464
+ if (this._cachedArgs && this._cachedArgs.every((arg, i) => args[i] === arg)) {
420
465
  return this._cachedResult;
421
466
  }
422
467
 
423
468
  this._cachedArgs = args;
424
469
  this._cachedResult = this.state.options.filter((option) => {
470
+ if (!text && option.hasOwnProperty('collapsedBy')) {
471
+ const isHidden = this.getCollapsibleState(option.collapsedBy) === OPTION_COLLAPSED;
472
+ if (isHidden) {
473
+ return false;
474
+ }
475
+ }
425
476
  return this.props.filterOption(option, text);
426
477
  });
427
478
 
@@ -540,6 +591,7 @@ class SelectCore extends Component {
540
591
  setIndex: this.setIndex,
541
592
  shouldShowOptionList: this._shouldShowOptionList(),
542
593
  toggleValue: this.toggleValue,
594
+ toggleCollapsedValue: this.toggleCollapsedValue
543
595
  });
544
596
  }
545
597
 
@@ -552,6 +604,7 @@ class SelectCore extends Component {
552
604
  <MenuComponent
553
605
  options={options}
554
606
  valueIsSelected={this.valueIsSelected}
607
+ getCollapsibleState={this.getCollapsibleState}
555
608
  OptionComponent={OptionComponent}
556
609
  renderOptionLabel={renderOptionLabel}
557
610
  onUp={this.onUp}
@@ -626,11 +679,6 @@ SelectCore.propTypes = {
626
679
  */
627
680
  filterOption: PropTypes.func,
628
681
  /**
629
- Function intercepts input value change
630
- * @param {string} str
631
- */
632
- handleValueChange: PropTypes.func,
633
- /**
634
682
  Function used to fetch async options. Is invoked with currentText and should return a promise with the options
635
683
  * @param {string} str
636
684
  * @returns {Promise}
@@ -733,6 +781,9 @@ SelectCore.propTypes = {
733
781
  disabled: PropTypes.bool,
734
782
  filterable: PropTypes.bool,
735
783
  onOptionsFiltered: PropTypes.func,
784
+ onChangeText: PropTypes.func,
785
+ collapsibleOptions: PropTypes.array
786
+
736
787
  };
737
788
 
738
789
  SelectCore.defaultProps = {
@@ -1,7 +1,9 @@
1
- import React, { Component } from 'react';
1
+ import cn from 'classnames';
2
+ import isEqual from 'lodash/isEqual';
2
3
  import PropTypes from 'prop-types';
4
+ import React, { Component } from 'react';
3
5
 
4
- import isEqual from 'lodash/isEqual';
6
+ import Collapse from './Collapse';
5
7
 
6
8
  /**
7
9
  * Generic component for rendering an option in a select. Contains functionality for handling clicking and focusing.
@@ -11,12 +13,13 @@ export default class SelectOption extends Component {
11
13
  super();
12
14
  this.onClick = this.onClick.bind(this);
13
15
  this.onMouseEnter = this.onMouseEnter.bind(this);
16
+ this.onCollapseClick = this.onCollapseClick.bind(this);
14
17
  }
15
18
 
16
19
  shouldComponentUpdate(nextProps) {
17
20
  // check only interesting props
18
21
  const currentProps = this.props;
19
- const propsToCheck = ['isSelected', 'option', 'disabled', 'isHighlighted'];
22
+ const propsToCheck = ['isSelected', 'option', 'disabled', 'isHighlighted', 'collapsibleState', 'currentText'];
20
23
 
21
24
  return propsToCheck
22
25
  .map((val) => isEqual( currentProps[val], nextProps[val]) )
@@ -42,19 +45,32 @@ export default class SelectOption extends Component {
42
45
  }
43
46
  }
44
47
 
48
+ onCollapseClick(e){
49
+ e.stopPropagation();
50
+ e.nativeEvent.stopImmediatePropagation();
51
+ const { toggleCollapsedValue, option } = this.props;
52
+ toggleCollapsedValue(option.value);
53
+ }
54
+
45
55
  render() {
56
+ const { isSelected, option, id, className, collapsibleState, renderOptionLabel } = this.props;
57
+
46
58
  return (
47
59
  <li
48
- aria-selected={this.props.isSelected}
49
- aria-disabled={this.props.option.disabled}
50
- aria-label={this.props.renderOptionLabel(this.props.option)}
51
- className={this.props.className}
52
- id={this.props.id}
60
+ aria-selected={isSelected}
61
+ aria-disabled={option.disabled}
62
+ aria-label={renderOptionLabel(this.props.option)}
63
+ className={cn(className, option.optionClassName)}
64
+ id={id}
53
65
  onClick={this.onClick}
54
66
  onMouseEnter={this.onMouseEnter}
55
67
  role="option"
56
68
  tabIndex="-1"
57
69
  >
70
+ {
71
+ collapsibleState &&
72
+ <Collapse collapsibleState={collapsibleState} onClick={this.onCollapseClick} />
73
+ }
58
74
  { this.props.children }
59
75
  </li>
60
76
  );
@@ -73,4 +89,7 @@ SelectOption.propTypes = {
73
89
  toggleValue: PropTypes.func.isRequired,
74
90
  unselectOnClick: PropTypes.bool,
75
91
  id: PropTypes.string,
92
+ toggleCollapsedValue: PropTypes.func.isRequired,
93
+ collapsibleState: PropTypes.string,
94
+ currentText: PropTypes.string
76
95
  };
@@ -40,6 +40,7 @@ export default function SingleSelect (props){
40
40
  onClose={props.onClose}
41
41
  options={props.options}
42
42
  optionSets={props.optionSets}
43
+ onChangeText={props.onChangeText}
43
44
  placeholder={props.placeholder}
44
45
  renderValueLabel={props.renderValueLabel}
45
46
  renderOptionLabel={props.renderOptionLabel}
@@ -63,6 +64,7 @@ SingleSelect.propTypes = {
63
64
  onChange: PropTypes.func,
64
65
  onClose: PropTypes.func,
65
66
  onOpen: PropTypes.func,
67
+ onChangeText: PropTypes.func,
66
68
  OptionComponent: PropTypes.func,
67
69
  renderOptionLabel: PropTypes.func,
68
70
  renderValueLabel: PropTypes.func,
@@ -22,6 +22,7 @@ export default function TagBoxMenuSelect (props) {
22
22
  closeOnSelect={false}
23
23
 
24
24
  ariaLabel={props.ariaLabel}
25
+ collapsibleOptions={props.collapsibleOptions}
25
26
  filterable={props.filterable}
26
27
  disabled={props.disabled}
27
28
  onChange={props.onChange}
@@ -39,6 +40,7 @@ export default function TagBoxMenuSelect (props) {
39
40
 
40
41
  TagBoxMenuSelect.propTypes = {
41
42
  ariaLabel: PropTypes.string,
43
+ collapsibleOptions: PropTypes.array,
42
44
  filterable: PropTypes.bool,
43
45
  disabled: PropTypes.bool,
44
46
  options: PropTypes.array.isRequired,
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import { shallow } from 'enzyme';
3
+
4
+ import { whereClassName } from 'test/utils/enzyme-utils';
5
+
6
+ import Collapse from '../Collapse';
7
+ import icons from '../styles/icons/icons.css';
8
+
9
+ import { OPTION_NOT_COLLAPSED, OPTION_COLLAPSED } from '../../constants';
10
+
11
+ it('passes onClick handler', function(){
12
+ const onClick = jest.fn();
13
+ const wrapper = shallow(<Collapse collapsibleState={OPTION_NOT_COLLAPSED} onClick={onClick} />);
14
+ wrapper.find('[role="button"]').simulate('click');
15
+ expect(onClick).toHaveBeenCalled();
16
+ });
17
+
18
+ it('renders correct class based on collapsibleState', function(){
19
+ const onClick = jest.fn();
20
+ const wrapper = shallow(<Collapse collapsibleState={OPTION_NOT_COLLAPSED} onClick={onClick} />);
21
+ expect(wrapper.findWhere(whereClassName(icons['pamicon-triangle-right'])).length).toEqual(0);
22
+ expect(wrapper.findWhere(whereClassName(icons['pamicon-triangle-down'])).length).toEqual(1);
23
+ wrapper.setProps({collapsibleState: OPTION_COLLAPSED});
24
+ expect(wrapper.findWhere(whereClassName(icons['pamicon-triangle-right'])).length).toEqual(1);
25
+ expect(wrapper.findWhere(whereClassName(icons['pamicon-triangle-down'])).length).toEqual(0);
26
+ });
@@ -16,7 +16,10 @@ const getDefaultProps = () => ({
16
16
  toggleValue: sinon.spy(),
17
17
  option: {value: 1, label: 'one'},
18
18
  index: 0,
19
- setIndex: sinon.spy()
19
+ setIndex: sinon.spy(),
20
+ toggleCollapsedValue: jest.fn(),
21
+ collapsibleOptions: null,
22
+ currentText: ''
20
23
  });
21
24
 
22
25
  test('it renders checkbox based on isSelected', () => {
@@ -45,7 +48,7 @@ test('it renders custom label if passed in', () => {
45
48
  expect(wrapper.findWhere(whereClassName(styles.label)).text()).toEqual(label);
46
49
  });
47
50
 
48
- test('it pases classes to SelectOption based on props', () => {
51
+ test('it passes classes to SelectOption based on props', () => {
49
52
  const props = getDefaultProps();
50
53
  const wrapper = shallow(<MultiSelectOption {...props} />);
51
54
  expect(wrapper.find(SelectOption).prop('className')).toEqual(styles['option']);
@@ -58,3 +61,40 @@ test('it pases classes to SelectOption based on props', () => {
58
61
  classnames(styles.option, styles.disabled, styles.highlighted)
59
62
  );
60
63
  });
64
+
65
+ describe('collapsibleOption classname', function(){
66
+ it('renders indented class when option has collapsedBy is a part of collapsibleOption', function(){
67
+ const props = getDefaultProps();
68
+ const wrapper = shallow(<MultiSelectOption {...props} />);
69
+
70
+ // no collapsedOptions and no collapsible -> no class
71
+ expect(wrapper.find(SelectOption).prop('className').includes(styles['option__indented'])).toBe(false);
72
+
73
+
74
+ // collapsedOptions set but no collapsible -> no class
75
+ wrapper.setProps({collapsibleOptions: [3, 4]});
76
+ expect(wrapper.find(SelectOption).prop('className').includes(styles['option__indented'])).toBe(false);
77
+
78
+ // collapsedOptions set and collapsedBy -> has class
79
+ wrapper.setProps({
80
+ option: { value: 1, label: 'One', collapsedBy: 3}
81
+ });
82
+ expect(wrapper.find(SelectOption).prop('className').includes(styles['option__indented'])).toBe(true);
83
+
84
+ // collapsedOptions set and collapsedBy, but options are filtered -> no class
85
+ wrapper.setProps({
86
+ currentText: 'asdf'
87
+ });
88
+
89
+ // collapsedOptions set but doesnt match collapsedBy -> no class
90
+ wrapper.setProps({
91
+ currentText: '',
92
+ option: { value: 1, label: 'One', collapsedBy: 6}
93
+ });
94
+ expect(wrapper.find(SelectOption).prop('className').includes(styles['option__indented'])).toBe(false);
95
+
96
+ // collapsedOptions empty -> no class
97
+ wrapper.setProps({collapsibleOptions: []});
98
+ expect(wrapper.find(SelectOption).prop('className').includes(styles['option__indented'])).toBe(false);
99
+ });
100
+ });
@@ -7,6 +7,7 @@ import SingleSelectOption from '../SingleSelectOption';
7
7
  import BasicMinLengthComponent from '../BasicMinLengthComponent';
8
8
  import BasicEmptyOptionComponent from '../BasicEmptyOptionComponent';
9
9
  import KEYS from '../../utils/keys';
10
+ import { OPTION_COLLAPSED, OPTION_NOT_COLLAPSED } from '../../constants';
10
11
 
11
12
  let defaultProps;
12
13
 
@@ -24,7 +25,9 @@ beforeEach(function(){
24
25
  toggleValue: jest.fn(),
25
26
  setIndex: jest.fn(),
26
27
  reference: React.createRef(),
27
- menuId: 'menu-1'
28
+ menuId: 'menu-1',
29
+ getCollapsibleState: () => null,
30
+ toggleCollapsedValue: jest.fn()
28
31
  };
29
32
  });
30
33
 
@@ -190,3 +193,23 @@ describe('move scrollbar position', function() {
190
193
 
191
194
  });
192
195
 
196
+
197
+ describe('collapsibleState', function(){
198
+ it('calls getCollapsibleState with the options and passes the result to the components', function(){
199
+ const getCollapsibleState = jest.fn().mockImplementation((value) => {
200
+ return value % 2 == 0 ? OPTION_COLLAPSED : OPTION_NOT_COLLAPSED;
201
+ });
202
+
203
+ const wrapper = mount(<OptionListSet {...defaultProps} getCollapsibleState={getCollapsibleState}/>);
204
+ expect(getCollapsibleState).toHaveBeenCalledTimes(2);
205
+ expect(getCollapsibleState).toHaveBeenCalledWith(basicOptions[0].value);
206
+ expect(getCollapsibleState).toHaveBeenCalledWith(basicOptions[1].value);
207
+
208
+ expect(
209
+ wrapper.find(SingleSelectOption).at(0).prop('collapsibleState')
210
+ ).toEqual(OPTION_NOT_COLLAPSED);
211
+ expect(
212
+ wrapper.find(SingleSelectOption).at(1).prop('collapsibleState')
213
+ ).toEqual(OPTION_COLLAPSED);
214
+ });
215
+ });
@@ -3,6 +3,8 @@ import { shallow, mount } from 'enzyme';
3
3
  import SelectCore, { PureSelectCore } from '../SelectCore';
4
4
  import SingleSelectValueAdapter from '../../adapters/SingleSelectValueAdapter';
5
5
  import MultiSelectValueAdapter from '../../adapters/MultiSelectValueAdapter';
6
+ import { OPTION_NOT_COLLAPSED, OPTION_COLLAPSED } from '../../constants';
7
+
6
8
  import { createFakeEvent } from 'test/utils/enzyme-utils';
7
9
 
8
10
  const MenuComponent = () => <ul></ul>;
@@ -490,6 +492,107 @@ describe('filtering options', function(){
490
492
  wrapper.instance()._getFilteredOptions();
491
493
  expect(mockFilterOption).toHaveBeenCalledTimes(4);
492
494
  });
495
+
496
+
497
+ it('calls onOptionsFiltered with filtered options', function(){
498
+ const onOptionsFiltered = jest.fn();
499
+ const wrapper = mount(<PureSelectCore {...defaultProps} filterOption={filterOption} onOptionsFiltered={onOptionsFiltered} />, {lifecycleExperimental: true});
500
+ wrapper.instance().changeText('Tw');
501
+ expect(onOptionsFiltered).toHaveBeenCalledTimes(1);
502
+ expect(onOptionsFiltered).toHaveBeenCalledWith([defaultProps.options[1]]);
503
+ wrapper.instance().changeText('');
504
+ expect(onOptionsFiltered).toHaveBeenCalledTimes(2);
505
+ expect(onOptionsFiltered).toHaveBeenCalledWith(defaultProps.options);
506
+ });
507
+
508
+ it('calls onChangeText with updated text', function(){
509
+ const onChangeText = jest.fn();
510
+ const wrapper = mount(<PureSelectCore {...defaultProps} filterOption={filterOption} onChangeText={onChangeText} />, {lifecycleExperimental: true});
511
+ wrapper.instance().changeText('Tw');
512
+ expect(onChangeText).toHaveBeenCalledTimes(1);
513
+ expect(onChangeText).toHaveBeenCalledWith('Tw');
514
+ wrapper.instance().changeText('');
515
+ expect(onChangeText).toHaveBeenCalledTimes(2);
516
+ expect(onChangeText).toHaveBeenCalledWith('');
517
+ });
518
+ });
519
+
520
+ describe('collapse behavior', function () {
521
+ const optionsWithCollapse = [{value: 1, label: 'One'}, {value: 2, label: 'Two', collapsedBy: 1}, {value: 3, label: 'Three', collapsedBy: 1}, {value: 4, label: 'Four'}, {value: 5, label: 'Five', collapsedBy: 4}]
522
+
523
+ describe('when there are no collapsibleOptions', function ( ) {
524
+ it('does not return collapsible state and does not filter options', function(){
525
+ const wrapper = shallow(<PureSelectCore {...defaultProps} options={optionsWithCollapse} />);
526
+ expect(wrapper.instance().getCollapsibleState(1)).toEqual(null)
527
+ expect(wrapper.instance().getCollapsibleState(2)).toEqual(null)
528
+ expect(wrapper.instance().getCollapsibleState(3)).toEqual(null)
529
+ expect(wrapper.instance().getCollapsibleState(4)).toEqual(null)
530
+ expect(wrapper.instance().getCollapsibleState(5)).toEqual(null)
531
+ expect(wrapper.instance()._getFilteredOptions()).toEqual(optionsWithCollapse);
532
+ wrapper.instance().toggleCollapsedValue(1);
533
+ expect(wrapper.instance().getCollapsibleState(1)).toEqual(null)
534
+ expect(wrapper.instance().getCollapsibleState(2)).toEqual(null)
535
+ expect(wrapper.instance().getCollapsibleState(3)).toEqual(null)
536
+ expect(wrapper.instance().getCollapsibleState(4)).toEqual(null)
537
+ expect(wrapper.instance().getCollapsibleState(5)).toEqual(null)
538
+ expect(wrapper.instance()._getFilteredOptions()).toEqual(optionsWithCollapse);
539
+ });
540
+ });
541
+
542
+ describe('when collapsibleOptions are provided but select is filtered', function () {
543
+ it('does not return collapsible state and does not filter options', function(){
544
+ const wrapper = shallow(<PureSelectCore {...defaultProps} options={optionsWithCollapse} collapsibleOptions={[1, 4]} />);
545
+ wrapper.instance().toggleCollapsedValue(1);
546
+ wrapper.instance().changeText('Tw');
547
+ expect(wrapper.instance().getCollapsibleState(1)).toEqual(null)
548
+ expect(wrapper.instance().getCollapsibleState(2)).toEqual(null)
549
+ expect(wrapper.instance().getCollapsibleState(3)).toEqual(null)
550
+ expect(wrapper.instance().getCollapsibleState(4)).toEqual(null)
551
+ expect(wrapper.instance().getCollapsibleState(5)).toEqual(null)
552
+ // becaused filtered, we are returning it even though its referenced by collapsedBy
553
+ expect(wrapper.instance()._getFilteredOptions()).toEqual([optionsWithCollapse[1]]);
554
+ });
555
+ });
556
+
557
+ describe('when collapsedValue is not one of the collapsibleOptions', function(){
558
+ it('returns null and not collapsed for others', function(){
559
+ const wrapper = shallow(<PureSelectCore {...defaultProps} options={optionsWithCollapse} collapsibleOptions={[1, 4]} />);
560
+ wrapper.instance().toggleCollapsedValue(2);
561
+ expect(wrapper.instance().getCollapsibleState(1)).toEqual(OPTION_NOT_COLLAPSED)
562
+ expect(wrapper.instance().getCollapsibleState(2)).toEqual(null)
563
+ expect(wrapper.instance().getCollapsibleState(3)).toEqual(null)
564
+ expect(wrapper.instance().getCollapsibleState(4)).toEqual(OPTION_NOT_COLLAPSED)
565
+ expect(wrapper.instance().getCollapsibleState(5)).toEqual(null)
566
+ expect(wrapper.instance()._getFilteredOptions()).toEqual(optionsWithCollapse);
567
+ });
568
+ })
569
+
570
+ describe('when value is collapsed', function() {
571
+ it('returns OPTION_COLLAPSED and filters out options with collapsedBy attribute', function () {
572
+ const wrapper = shallow(<PureSelectCore {...defaultProps} options={optionsWithCollapse} collapsibleOptions={[1, 4]} />);
573
+ wrapper.instance().toggleCollapsedValue(1);
574
+ expect(wrapper.instance().getCollapsibleState(1)).toEqual(OPTION_COLLAPSED)
575
+ expect(wrapper.instance().getCollapsibleState(2)).toEqual(null)
576
+ expect(wrapper.instance().getCollapsibleState(3)).toEqual(null)
577
+ expect(wrapper.instance().getCollapsibleState(4)).toEqual(OPTION_NOT_COLLAPSED)
578
+ expect(wrapper.instance().getCollapsibleState(5)).toEqual(null)
579
+ expect(wrapper.instance()._getFilteredOptions()).toEqual([optionsWithCollapse[0], optionsWithCollapse[3], optionsWithCollapse[4]]);
580
+ });
581
+ });
582
+
583
+ describe('when value is toggled', function() {
584
+ it('returns OPTION_NOT_COLLAPSED and does not filter out options with collapsedBy attribute', function(){
585
+ const wrapper = shallow(<PureSelectCore {...defaultProps} options={optionsWithCollapse} collapsibleOptions={[1, 4]} />);
586
+ wrapper.instance().toggleCollapsedValue(1);
587
+ wrapper.instance().toggleCollapsedValue(1);
588
+ expect(wrapper.instance().getCollapsibleState(1)).toEqual(OPTION_NOT_COLLAPSED)
589
+ expect(wrapper.instance().getCollapsibleState(2)).toEqual(null)
590
+ expect(wrapper.instance().getCollapsibleState(3)).toEqual(null)
591
+ expect(wrapper.instance().getCollapsibleState(4)).toEqual(OPTION_NOT_COLLAPSED)
592
+ expect(wrapper.instance().getCollapsibleState(5)).toEqual(null)
593
+ expect(wrapper.instance()._getFilteredOptions()).toEqual(optionsWithCollapse);
594
+ });
595
+ });
493
596
  });
494
597
 
495
598
  describe('render components', function(){