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.

MetricSelect.tsx 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 { sortBy } from 'lodash';
  22. import {
  23. translate,
  24. getLocalizedMetricName,
  25. getLocalizedMetricDomain
  26. } from 'sonar-ui-common/helpers/l10n';
  27. import Select from 'sonar-ui-common/components/controls/Select';
  28. interface Props {
  29. metrics: T.Metric[];
  30. onMetricChange: (metric: T.Metric) => void;
  31. }
  32. interface State {
  33. value: number;
  34. }
  35. interface Option {
  36. disabled?: boolean;
  37. domain?: string;
  38. label: string;
  39. value: number;
  40. }
  41. export default class MetricSelect extends React.PureComponent<Props, State> {
  42. state = { value: -1 };
  43. handleChange = (option: Option | null) => {
  44. const value = option ? option.value : -1;
  45. this.setState({ value });
  46. this.props.onMetricChange(this.props.metrics[value]);
  47. };
  48. render() {
  49. const { metrics } = this.props;
  50. const options: Option[] = sortBy(
  51. metrics.map((metric, index) => ({
  52. value: index,
  53. label: getLocalizedMetricName(metric),
  54. domain: metric.domain
  55. })),
  56. 'domain'
  57. );
  58. // use "disabled" property to emulate optgroups
  59. const optionsWithDomains: Option[] = [];
  60. options.forEach((option, index, options) => {
  61. const previous = index > 0 ? options[index - 1] : null;
  62. if (option.domain && (!previous || previous.domain !== option.domain)) {
  63. optionsWithDomains.push({
  64. value: 0,
  65. label: getLocalizedMetricDomain(option.domain),
  66. disabled: true
  67. });
  68. }
  69. optionsWithDomains.push(option);
  70. });
  71. return (
  72. <Select
  73. className="text-middle"
  74. id="condition-metric"
  75. onChange={this.handleChange}
  76. options={optionsWithDomains}
  77. placeholder={translate('search.search_for_metrics')}
  78. value={this.state.value}
  79. />
  80. );
  81. }
  82. }