You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SelectList.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import classNames from 'classnames';
  21. import * as key from 'keymaster';
  22. import { uniqueId } from 'lodash';
  23. import * as React from 'react';
  24. import SelectListItem from './SelectListItem';
  25. interface Props {
  26. className?: string;
  27. items: string[];
  28. currentItem: string;
  29. onSelect: (item: string) => void;
  30. }
  31. interface State {
  32. active: string;
  33. }
  34. export default class SelectList extends React.PureComponent<Props, State> {
  35. currentKeyScope?: string;
  36. previousFilter?: (event: any) => void;
  37. previousKeyScope?: string;
  38. constructor(props: Props) {
  39. super(props);
  40. this.state = {
  41. active: props.currentItem
  42. };
  43. }
  44. componentDidMount() {
  45. this.attachShortcuts();
  46. }
  47. componentDidUpdate(prevProps: Props) {
  48. if (
  49. prevProps.currentItem !== this.props.currentItem &&
  50. !this.props.items.includes(this.state.active)
  51. ) {
  52. this.setState({ active: this.props.currentItem });
  53. }
  54. }
  55. componentWillUnmount() {
  56. this.detachShortcuts();
  57. }
  58. attachShortcuts = () => {
  59. this.previousKeyScope = key.getScope();
  60. this.previousFilter = key.filter;
  61. this.currentKeyScope = uniqueId('key-scope');
  62. key.setScope(this.currentKeyScope);
  63. // sometimes there is a *focused* search field next to the SelectList component
  64. // we need to allow shortcuts in this case, but only for the used keys
  65. (key as any).filter = (event: KeyboardEvent & { target: HTMLElement }) => {
  66. const { tagName } = event.target || event.srcElement;
  67. const isInput = tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA';
  68. return [13, 38, 40].includes(event.keyCode) || !isInput;
  69. };
  70. key('down', this.currentKeyScope, () => {
  71. this.setState(this.selectNextElement);
  72. return false;
  73. });
  74. key('up', this.currentKeyScope, () => {
  75. this.setState(this.selectPreviousElement);
  76. return false;
  77. });
  78. key('return', this.currentKeyScope, () => {
  79. if (this.state.active != null) {
  80. this.handleSelect(this.state.active);
  81. }
  82. return false;
  83. });
  84. };
  85. detachShortcuts = () => {
  86. if (this.previousKeyScope) {
  87. key.setScope(this.previousKeyScope);
  88. }
  89. if (this.currentKeyScope) {
  90. key.deleteScope(this.currentKeyScope);
  91. }
  92. (key as any).filter = this.previousFilter;
  93. };
  94. handleSelect = (item: string) => {
  95. this.props.onSelect(item);
  96. };
  97. handleHover = (item: string) => {
  98. this.setState({ active: item });
  99. };
  100. selectNextElement = (state: State, props: Props) => {
  101. const idx = props.items.indexOf(state.active);
  102. if (idx < 0) {
  103. return { active: props.items[0] };
  104. }
  105. return { active: props.items[(idx + 1) % props.items.length] };
  106. };
  107. selectPreviousElement = (state: State, props: Props) => {
  108. const idx = props.items.indexOf(state.active);
  109. if (idx <= 0) {
  110. return { active: props.items[props.items.length - 1] };
  111. }
  112. return { active: props.items[idx - 1] };
  113. };
  114. renderChild = (child: any) => {
  115. if (child == null) {
  116. return null;
  117. }
  118. // do not pass extra props to children like `<li className="divider" />`
  119. if (child.type !== SelectListItem) {
  120. return child;
  121. }
  122. return React.cloneElement(child, {
  123. active: this.state.active,
  124. onHover: this.handleHover,
  125. onSelect: this.handleSelect
  126. });
  127. };
  128. render() {
  129. const { children } = this.props;
  130. const hasChildren = React.Children.count(children) > 0;
  131. return (
  132. <ul className={classNames('menu', this.props.className)}>
  133. {hasChildren && React.Children.map(children, this.renderChild)}
  134. {!hasChildren &&
  135. this.props.items.map(item => (
  136. <SelectListItem
  137. active={this.state.active}
  138. item={item}
  139. key={item}
  140. onHover={this.handleHover}
  141. onSelect={this.handleSelect}
  142. />
  143. ))}
  144. </ul>
  145. );
  146. }
  147. }