選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ProfilePermissionsFormSelect.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 * as React from 'react';
  21. import GroupIcon from 'sonar-ui-common/components/icons/GroupIcon';
  22. import { debounce, identity } from 'lodash';
  23. import { translate, translateWithParameters } from 'sonar-ui-common/helpers/l10n';
  24. import Select from 'sonar-ui-common/components/controls/Select';
  25. import { Group } from './ProfilePermissions';
  26. import Avatar from '../../../components/ui/Avatar';
  27. type Option = T.UserSelected | Group;
  28. type OptionWithValue = Option & { value: string };
  29. interface Props {
  30. onChange: (option: OptionWithValue) => void;
  31. onSearch: (query: string) => Promise<Option[]>;
  32. selected?: Option;
  33. }
  34. interface State {
  35. loading: boolean;
  36. query: string;
  37. searchResults: Option[];
  38. }
  39. export default class ProfilePermissionsFormSelect extends React.PureComponent<Props, State> {
  40. mounted = false;
  41. constructor(props: Props) {
  42. super(props);
  43. this.handleSearch = debounce(this.handleSearch, 250);
  44. this.state = { loading: false, query: '', searchResults: [] };
  45. }
  46. componentDidMount() {
  47. this.mounted = true;
  48. this.handleSearch(this.state.query);
  49. }
  50. componentWillUnmount() {
  51. this.mounted = false;
  52. }
  53. handleSearch = (query: string) => {
  54. this.setState({ loading: true });
  55. this.props.onSearch(query).then(
  56. searchResults => {
  57. if (this.mounted) {
  58. this.setState({ loading: false, searchResults });
  59. }
  60. },
  61. () => {
  62. if (this.mounted) {
  63. this.setState({ loading: false });
  64. }
  65. }
  66. );
  67. };
  68. handleInputChange = (query: string) => {
  69. this.setState({ query });
  70. if (query.length > 1) {
  71. this.handleSearch(query);
  72. }
  73. };
  74. render() {
  75. const noResultsText =
  76. this.state.query.length === 1
  77. ? translateWithParameters('select2.tooShort', 2)
  78. : translate('no_results');
  79. // create a uniq string both for users and groups
  80. const options = this.state.searchResults.map(r => ({ ...r, value: getStringValue(r) }));
  81. return (
  82. <Select
  83. autoFocus={true}
  84. className="Select-big"
  85. clearable={false}
  86. // disable default react-select filtering
  87. filterOptions={identity}
  88. isLoading={this.state.loading}
  89. noResultsText={noResultsText}
  90. onChange={this.props.onChange}
  91. onInputChange={this.handleInputChange}
  92. optionRenderer={optionRenderer}
  93. options={options}
  94. placeholder=""
  95. searchable={true}
  96. value={this.props.selected && getStringValue(this.props.selected)}
  97. valueRenderer={optionRenderer}
  98. />
  99. );
  100. }
  101. }
  102. function isUser(option: Option): option is T.UserSelected {
  103. return (option as T.UserSelected).login !== undefined;
  104. }
  105. function getStringValue(option: Option) {
  106. return isUser(option) ? `user:${option.login}` : `group:${option.name}`;
  107. }
  108. function optionRenderer(option: OptionWithValue) {
  109. return isUser(option) ? (
  110. <>
  111. <Avatar hash={option.avatar} name={option.name} size={16} />
  112. <strong className="spacer-left">{option.name}</strong>
  113. <span className="note little-spacer-left">{option.login}</span>
  114. </>
  115. ) : (
  116. <>
  117. <GroupIcon size={16} />
  118. <strong className="spacer-left">{option.name}</strong>
  119. </>
  120. );
  121. }