about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/composer/options/dropdown/content/item/index.js
blob: 68a52083f6bd58ed8328ff04aac6575525f5697a (plain) (blame)
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
//  Package imports.
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import Toggle from 'react-toggle';

//  Components.
import Icon from 'flavours/glitch/components/icon';

//  Utils.
import { assignHandlers } from 'flavours/glitch/util/react_helpers';

//  Handlers.
const handlers = {

  //  This function activates the dropdown item.
  handleActivate (e) {
    const {
      name,
      onChange,
      onClose,
      options: { on },
    } = this.props;

    //  If the escape key was pressed, we close the dropdown.
    if (e.key === 'Escape' && onClose) {
      onClose();

    //  Otherwise, we both close the dropdown and change the value.
    } else if (onChange && (!e.key || e.key === 'Enter')) {
      e.preventDefault();  //  Prevents change in focus on click
      if ((on === null || typeof on === 'undefined') && onClose) {
        onClose();
      }
      onChange(name);
    }
  },
};

//  The component.
export default class ComposerOptionsDropdownContentItem extends React.PureComponent {

  //  Constructor.
  constructor (props) {
    super(props);
    assignHandlers(this, handlers);
  }

  //  Rendering.
  render () {
    const { handleActivate } = this.handlers;
    const {
      active,
      options: {
        icon,
        meta,
        on,
        text,
      },
    } = this.props;
    const computedClass = classNames('composer--options--dropdown--content--item', {
      active,
      lengthy: meta,
      'toggled-off': !on && on !== null && typeof on !== 'undefined',
      'toggled-on': on,
      'with-icon': icon,
    });

    //  The result.
    return (
      <div
        className={computedClass}
        onClick={handleActivate}
        onKeyDown={handleActivate}
        role='button'
        tabIndex='0'
      >
        {function () {

          //  We render a `<Toggle>` if we were provided an `on`
          //  property, and otherwise show an `<Icon>` if available.
          switch (true) {
          case on !== null && typeof on !== 'undefined':
            return (
              <Toggle
                checked={on}
                onChange={handleActivate}
              />
            );
          case !!icon:
            return (
              <Icon
                className='icon'
                fullwidth
                icon={icon}
              />
            );
          default:
            return null;
          }
        }()}
        {meta ? (
          <div className='content'>
            <strong>{text}</strong>
            {meta}
          </div>
        ) :
          <div className='content'>
            <strong>{text}</strong>
          </div>}
      </div>
    );
  }

};

//  Props.
ComposerOptionsDropdownContentItem.propTypes = {
  active: PropTypes.bool,
  name: PropTypes.string,
  onChange: PropTypes.func,
  onClose: PropTypes.func,
  options: PropTypes.shape({
    icon: PropTypes.string,
    meta: PropTypes.node,
    on: PropTypes.bool,
    text: PropTypes.node,
  }),
};