dropdown

v1.0.0arrow_drop_down
v1.0.0
STATUS
Passing
DOWNLOADS
857
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
4 months ago
subject
Determines the direction and location of the Menu in relation to it's Toggle.
1 contributor
Like
Use dropdown in your project ?
Copied
npm i @bit/react-bootstrap.react-bootstrap.dropdown
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
src/Dropdown.js
166 Lines(102 sloc)
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
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import mapContextToProps from 'react-context-toolbox/mapContextToProps';
import { elementType } from 'prop-types-extra';
import BaseDropdown from 'react-overlays/Dropdown';

import chain from './utils/createChainedFunction';
import { createBootstrapComponent } from './ThemeProvider';
import DropdownMenu from './DropdownMenu';
import DropdownToggle from './DropdownToggle';
import DropdownItem from './DropdownItem';
import SelectableContext from './SelectableContext';
import createWithBsPrefix from './utils/createWithBsPrefix';

const propTypes = {
  /** @default 'dropdown' */
  bsPrefix: PropTypes.string,
  /**
   * Determines the direction and location of the Menu in relation to it's Toggle.
   */
  drop: PropTypes.oneOf(['up', 'left', 'right', 'down']),

  as: elementType,

  /**
   * Align the menu to the right side of the Dropdown toggle
   */
  alignRight: PropTypes.bool,

  /**
   * Whether or not the Dropdown is visible.
   *
   * @controllable onToggle
   */
  show: PropTypes.bool,

  /**
   * Allow Dropdown to flip in case of an overlapping on the reference element. For more information refer to
   * Popper.js's flip [docs](https://popper.js.org/popper-documentation.html#modifiers..flip.enabled).
   *
   */
  flip: PropTypes.bool,

  /**
   * A callback fired when the Dropdown wishes to change visibility. Called with the requested
   * `show` value, the DOM event, and the source that fired it: `'click'`,`'keydown'`,`'rootClose'`, or `'select'`.
   *
   * ```js
   * function(
   *   isOpen: boolean,
   *   event: SyntheticEvent,
   *   metadata: {
   *     source: 'select' | 'click' | 'rootClose' | 'keydown'
   *   }
   * ): void
   * ```
   *
   * @controllable show
   */
  onToggle: PropTypes.func,

  /**
   * A callback fired when a menu item is selected.
   *
   * ```js
   * (eventKey: any, event: Object) => any
   * ```
   */
  onSelect: PropTypes.func,

  /** @private */
  navbar: PropTypes.bool,
};

const defaultProps = {
  as: 'div',
  navbar: false,
};

class Dropdown extends React.Component {
  handleSelect = (key, event) => {
    if (this.props.onSelect) this.props.onSelect(key, event);

    this.handleToggle(false, event, 'select');
  };

  handleToggle = (show, event, source = event.type) => {
    if (event.currentTarget === document) source = 'rootClose';

    this.props.onToggle(show, event, { source });
  };

  render() {
    const {
      bsPrefix,
      drop,
      show,
      className,
      as: Component,
      alignRight,
      onSelect: _1,
      onToggle: _3,
      navbar: _4,
      ...props
    } = this.props;

    delete props.onToggle;

    return (
      <SelectableContext.Provider value={this.handleSelect}>
        <BaseDropdown.ControlledComponent
          drop={drop}
          show={show}
          alignEnd={alignRight}
          onToggle={this.handleToggle}
          itemSelector={`.${bsPrefix}-item:not(.disabled):not(:disabled)`}
        >
          {({ props: dropdownProps }) => (
            <Component
              {...props}
              {...dropdownProps}
              className={classNames(
                className,
                show && 'show',
                (!drop || drop === 'down') && bsPrefix,
                drop === 'up' && 'dropup',
                drop === 'right' && 'dropright',
                drop === 'left' && 'dropleft',
              )}
            />
          )}
        </BaseDropdown.ControlledComponent>
      </SelectableContext.Provider>
    );
  }
}

Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;

const UncontrolledDropdown = createBootstrapComponent(
  BaseDropdown.deferControlTo(Dropdown),
  'dropdown',
);

const DecoratedDropdown = mapContextToProps(
  SelectableContext,
  (onSelect, props) => ({
    onSelect: chain(props.onSelect, onSelect),
  }),
  UncontrolledDropdown,
);

DecoratedDropdown.Toggle = DropdownToggle;
DecoratedDropdown.Menu = DropdownMenu;
DecoratedDropdown.Item = DropdownItem;

DecoratedDropdown.Header = createWithBsPrefix('dropdown-header', {
  defaultProps: { role: 'heading' },
});
DecoratedDropdown.Divider = createWithBsPrefix('dropdown-divider', {
  defaultProps: { role: 'separator' },
});

export default DecoratedDropdown;