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.

PropertySetInput.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 * as React from 'react';
  21. import { DeleteButton } from '../../../../components/controls/buttons';
  22. import {
  23. DefaultSpecializedInputProps,
  24. getEmptyValue,
  25. getUniqueName,
  26. isCategoryDefinition
  27. } from '../../utils';
  28. import PrimitiveInput from './PrimitiveInput';
  29. export default class PropertySetInput extends React.PureComponent<DefaultSpecializedInputProps> {
  30. ensureValue() {
  31. return this.props.value || [];
  32. }
  33. handleDeleteValue = (index: number) => {
  34. const newValue = [...this.ensureValue()];
  35. newValue.splice(index, 1);
  36. this.props.onChange(newValue);
  37. };
  38. handleInputChange = (index: number, fieldKey: string, value: any) => {
  39. const emptyValue = getEmptyValue(this.props.setting.definition)[0];
  40. const newValue = [...this.ensureValue()];
  41. const newFields = { ...emptyValue, ...newValue[index], [fieldKey]: value };
  42. newValue.splice(index, 1, newFields);
  43. return this.props.onChange(newValue);
  44. };
  45. renderFields(fieldValues: any, index: number, isLast: boolean) {
  46. const { setting, isDefault } = this.props;
  47. const { definition } = setting;
  48. return (
  49. <tr key={index}>
  50. {isCategoryDefinition(definition) &&
  51. definition.fields.map(field => {
  52. const newSetting = {
  53. ...setting,
  54. definition: field,
  55. value: fieldValues[field.key]
  56. };
  57. return (
  58. <td key={field.key}>
  59. <PrimitiveInput
  60. isDefault={isDefault}
  61. hasValueChanged={this.props.hasValueChanged}
  62. name={getUniqueName(definition, field.key)}
  63. onChange={value => this.handleInputChange(index, field.key, value)}
  64. setting={newSetting}
  65. value={fieldValues[field.key]}
  66. />
  67. </td>
  68. );
  69. })}
  70. <td className="thin nowrap text-middle">
  71. {!isLast && (
  72. <DeleteButton
  73. className="js-remove-value"
  74. onClick={() => this.handleDeleteValue(index)}
  75. />
  76. )}
  77. </td>
  78. </tr>
  79. );
  80. }
  81. render() {
  82. const { definition } = this.props.setting;
  83. const displayedValue = [...this.ensureValue(), ...getEmptyValue(definition)];
  84. return (
  85. <div>
  86. <table
  87. className="data zebra-hover no-outer-padding"
  88. style={{ width: 'auto', minWidth: 480, marginTop: -12 }}>
  89. <thead>
  90. <tr>
  91. {isCategoryDefinition(definition) &&
  92. definition.fields.map(field => (
  93. <th key={field.key}>
  94. {field.name}
  95. {field.description != null && (
  96. <span className="spacer-top small">{field.description}</span>
  97. )}
  98. </th>
  99. ))}
  100. <th>&nbsp;</th>
  101. </tr>
  102. </thead>
  103. <tbody>
  104. {displayedValue.map((fieldValues, index) =>
  105. this.renderFields(fieldValues, index, index === displayedValue.length - 1)
  106. )}
  107. </tbody>
  108. </table>
  109. </div>
  110. );
  111. }
  112. }