PermissionSelect.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  2. import * as React from "../../../../common/keycloak/web_modules/react.js";
  3. import { Select, SelectOption, SelectVariant } from "../../../../common/keycloak/web_modules/@patternfly/react-core.js";
  4. class ScopeValue {
  5. constructor(value) {
  6. _defineProperty(this, "value", void 0);
  7. this.value = value;
  8. }
  9. toString() {
  10. return this.value.displayName ? this.value.displayName : this.value.name;
  11. }
  12. compareTo(selectOption) {
  13. return selectOption.name === this.value.name;
  14. }
  15. }
  16. export class PermissionSelect extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. _defineProperty(this, "onSelect", (_event, selection) => {
  20. const {
  21. selected
  22. } = this.state;
  23. const {
  24. onSelect
  25. } = this.props;
  26. if (selected.includes(selection)) {
  27. this.setState(prevState => ({
  28. selected: prevState.selected.filter(item => item !== selection)
  29. }), () => onSelect(this.state.selected.map(sv => sv.value)));
  30. } else {
  31. this.setState(prevState => ({
  32. selected: [...prevState.selected, selection]
  33. }), () => onSelect(this.state.selected.map(sv => sv.value)));
  34. }
  35. });
  36. _defineProperty(this, "onToggle", isExpanded => {
  37. this.setState({
  38. isExpanded
  39. });
  40. });
  41. _defineProperty(this, "clearSelection", () => {
  42. this.setState({
  43. selected: [],
  44. isExpanded: false
  45. });
  46. this.props.onSelect([]);
  47. });
  48. let values = [];
  49. if (this.props.selected) {
  50. values = this.props.selected.map(s => new ScopeValue(s));
  51. }
  52. this.state = {
  53. isExpanded: false,
  54. selected: values,
  55. scopes: this.props.scopes.map((option, index) => React.createElement(SelectOption, {
  56. key: index,
  57. value: values.find(s => s.compareTo(option)) || new ScopeValue(option)
  58. }))
  59. };
  60. }
  61. render() {
  62. const {
  63. isExpanded,
  64. selected
  65. } = this.state;
  66. const titleId = 'permission-id';
  67. return React.createElement("div", null, React.createElement("span", {
  68. id: titleId,
  69. hidden: true
  70. }, "Select the permissions"), React.createElement(Select, {
  71. direction: this.props.direction || 'down',
  72. variant: SelectVariant.typeaheadMulti,
  73. ariaLabelTypeAhead: "Select the permissions",
  74. onToggle: this.onToggle,
  75. onSelect: this.onSelect,
  76. onClear: this.clearSelection,
  77. selections: selected,
  78. isExpanded: isExpanded,
  79. ariaLabelledBy: titleId,
  80. placeholderText: "Select the permissions"
  81. }, this.state.scopes));
  82. }
  83. }
  84. //# sourceMappingURL=PermissionSelect.js.map