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.

MeasureViewSelect.tsx 2.7KB

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 TreemapIcon from 'sonar-ui-common/components/icons/TreemapIcon';
  22. import ListIcon from 'sonar-ui-common/components/icons/ListIcon';
  23. import TreeIcon from 'sonar-ui-common/components/icons/TreeIcon';
  24. import { translate } from 'sonar-ui-common/helpers/l10n';
  25. import Select from 'sonar-ui-common/components/controls/Select';
  26. import { hasList, hasTree, hasTreemap, View } from '../utils';
  27. interface Props {
  28. className?: string;
  29. metric: T.Metric;
  30. handleViewChange: (view: View) => void;
  31. view: View;
  32. }
  33. export default class MeasureViewSelect extends React.PureComponent<Props> {
  34. getOptions = () => {
  35. const { metric } = this.props;
  36. const options = [];
  37. if (hasTree(metric.key)) {
  38. options.push({
  39. icon: <TreeIcon />,
  40. label: translate('component_measures.tab.tree'),
  41. value: 'tree'
  42. });
  43. }
  44. if (hasList(metric.key)) {
  45. options.push({
  46. icon: <ListIcon />,
  47. label: translate('component_measures.tab.list'),
  48. value: 'list'
  49. });
  50. }
  51. if (hasTreemap(metric.key, metric.type)) {
  52. options.push({
  53. icon: <TreemapIcon />,
  54. label: translate('component_measures.tab.treemap'),
  55. value: 'treemap'
  56. });
  57. }
  58. return options;
  59. };
  60. handleChange = (option: { value: string }) => {
  61. return this.props.handleViewChange(option.value as View);
  62. };
  63. renderOption = (option: { icon: JSX.Element; label: string }) => {
  64. return (
  65. <>
  66. {option.icon}
  67. <span className="little-spacer-left">{option.label}</span>
  68. </>
  69. );
  70. };
  71. render() {
  72. return (
  73. <Select
  74. autoBlur={true}
  75. className={this.props.className}
  76. clearable={false}
  77. onChange={this.handleChange}
  78. optionRenderer={this.renderOption}
  79. options={this.getOptions()}
  80. searchable={false}
  81. value={this.props.view}
  82. valueRenderer={this.renderOption}
  83. />
  84. );
  85. }
  86. }