All files / lib/Select index.js

95% Statements 38/40
97.22% Branches 35/36
92.31% Functions 12/13
97.37% Lines 37/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259                                                                    21x                       8x 8x 8x 8x   8x   8x   8x 1x   1x   1x   7x 2x         5x               6x   6x       6x     6x       6x   6x                       31x                 31x   31x 31x   29x 3x 26x 2x         31x             31x       48x                             31x                     32x                         31x                 89x                                     89x                     89x                                                                                        
/**
 * @category controls
 * @component time-picker
 * @variations collab-ui-react
 */
 
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import {
  Button,
  EventOverlay,
  Icon,
  List,
 } from '@collab-ui/react';
import {
  isEqual,
  filter,
  find,
  uniqueId,
} from 'lodash';
 
class Select extends React.Component {
 
  state = {
    isOpen: false,
    selected: [],
    selectedIndex: [],
    anchorNode: null,
    anchorWidth: null,
    id: this.props.id || uniqueId('cui-select-')
  };
 
  componentDidUpdate(prevProps, prevState) {
    prevState.selected !== this.state.selected
      && this.props.onSelect
      && this.props.onSelect(this.state.selected);
  }
 
  hidePopover = () => {
    this.setState({
      isOpen: false
    });
  };
 
  handleSelect = (e, value, index, label) => {
    e.preventDefault();
    const { selected, selectedIndex } = this.state;
    const { isMulti } = this.props;
    const isActive = find(selected, {value, label});
 
    !isMulti && this.setState({ isOpen: false });
 
    Iif (isActive && !isMulti) return;
 
    if (isActive && isMulti) {
      return this.setState({
        selected: filter(selected, item =>
          !isEqual(item, {value, label})
        ),
        selectedIndex: selectedIndex.filter(i => i !== index)
      });
    } else if (!isActive && !isMulti) {
      return this.setState({
        selected: [{value, label}],
        selectedIndex: [index]
      });
    } else {
      return this.setState({
        selected: [...selected, {value, label}],
        selectedIndex: [...selectedIndex, index]
      });
    }
  }
 
  choosePosition = () => {
    const { isOpen, anchorNode } = this.state;
 
    isOpen && this.setAnchorWidth(anchorNode);
  };
 
  handleToggle = () => {
    this.setState({
      isOpen: !this.state.isOpen,
      anchorNode: ReactDOM.findDOMNode(this.clickTextField).parentNode
    }, () => this.choosePosition());
  };
 
  setAnchorWidth = elementAnchor => {
    const anchor = elementAnchor && elementAnchor.getBoundingClientRect();
 
    this.setState({ anchorWidth: anchor.width });
  };
 
  render() {
    const {
      children,
      className,
      isDynamic,
      isMulti,
      defaultValue,
      overlayProps,
      ...props
    } = this.props;
 
    const {
      selected,
      selectedIndex,
      isOpen,
      anchorNode,
      anchorWidth,
      id
    } = this.state;
 
    const currentValue = () => {
      if(!isMulti && selected.length) return selected[0].label;
 
      if(selected.length === 1) {
        return `${selected.length} Item Selected`;
      } else if(selected.length) {
        return `${selected.length} Items Selected`;
      }
    };
 
    const label = (
      <div className='cui-select__label' id={`${id}__label`}>
        {currentValue() || defaultValue}
        <Icon name={`arrow-down_16`} />
      </div>
    );
 
    const text = (
      <Button
        name={id}
        id={id}
        onClick={this.handleToggle}
        ref={ref => this.clickTextField = ref}
        className={
          'cui-button--input' +
          `${className && ` ${className}` || ''}`
        }
        aria-haspopup='listbox'
        ariaLabelledBy={`${id}__label`}
        active={isOpen}
        {...props}
      >
        {label}
      </Button>
    );
 
    const dropdownElement = (
      <EventOverlay
        allowClickAway
        anchorNode={anchorNode}
        close={this.hidePopover}
        isOpen={isOpen}
        isDynamic={isDynamic}
        {...overlayProps}
      >
        <List
          onSelect={this.handleSelect}
          style={{ width: anchorWidth }}
          ref={ref => this.list = ref}
          role='listbox'
          itemRole='option'
          active={selectedIndex}
          isMulti={isMulti}
          aria-labelledby={`${id}__label`}
          aria-multiselectable={isMulti}
        >
          {children}
        </List>
      </EventOverlay>
    );
 
    return (
      <div className='cui-input-group cui-select'>
        {text}
        {dropdownElement}
      </div>
    );
  }
}
 
Select.propTypes = {
  /** @prop Children nodes to render inside Select component | null */
  children: PropTypes.node,
  /** @prop Optional CSS class name | '' */
  className: PropTypes.string,
  /** @prop Set the default selected option | '' */
  defaultValue: PropTypes.string,
  /** @prop Set ID for Select Component | null */
  id: PropTypes.string,
  /** @prop Sets the Select EventOverlay to be dynamic | true */
  isDynamic: PropTypes.bool,
  /** @prop Optional prop to know if multiple Select children can be active | false */
  isMulti: PropTypes.bool,
  /** @prop Callback function invoked when user selects an item | null */
  onSelect: PropTypes.func,
  /** @prop Sets the EventOverlay props | null */
  overlayProps: PropTypes.shape({}),
};
 
Select.defaultProps = {
  children: null,
  className: '',
  defaultValue: '',
  id: null,
  isDynamic: true,
  isMulti: false,
  onSelect: null,
  overlayProps: null,
};
 
Select.displayName = 'Select';
 
export default Select;
 
/**
* @component select
* @section default
* @react
import { Select, SelectOption } from '@collab-ui/react';
 
export default class SelectDefault extends React.PureComponent {
  render() {
    return (
      <Select defaultValue='Select Item Here' >
        <SelectOption value='test1' label='test1' />
        <SelectOption value='test2' label='test2' />
        <SelectOption value='test3' label='test3' />
        <SelectOption value='test4' label='test4' />
      </Select>
    );
  }
}
 
**/
 
/**
* @component select
* @section multi-select
* @react
import { Select, SelectOption } from '@collab-ui/react';
 
export default class SelectMultiSelect extends React.PureComponent {
  render() {
    return (
      <Select isMulti defaultValue='Select Item Here' >
        <SelectOption value='test1' label='test1' />
        <SelectOption value='test2' label='test2' />
        <SelectOption value='test3' label='test3' />
        <SelectOption value='test4' label='test4' />
      </Select>
    );
  }
}
 
**/