ContentAlert.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /*
  3. * Copyright 2019 Red Hat, Inc. and/or its affiliates.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import * as React from "../../../common/keycloak/web_modules/react.js";
  18. import { Alert, AlertActionCloseButton, AlertGroup, AlertVariant } from "../../../common/keycloak/web_modules/@patternfly/react-core.js";
  19. import { Msg } from "../widgets/Msg.js";
  20. export class ContentAlert extends React.Component {
  21. constructor(props) {
  22. super(props);
  23. _defineProperty(this, "hideAlert", key => {
  24. this.setState({
  25. alerts: [...this.state.alerts.filter(el => el.key !== key)]
  26. });
  27. });
  28. _defineProperty(this, "getUniqueId", () => new Date().getTime());
  29. _defineProperty(this, "postAlert", (variant, message, params) => {
  30. const alerts = this.state.alerts;
  31. const key = this.getUniqueId();
  32. alerts.push({
  33. key,
  34. message: Msg.localize(message, params),
  35. variant
  36. });
  37. this.setState({
  38. alerts
  39. });
  40. if (variant !== AlertVariant.danger) {
  41. setTimeout(() => this.hideAlert(key), 8000);
  42. }
  43. });
  44. this.state = {
  45. alerts: []
  46. };
  47. ContentAlert.instance = this;
  48. }
  49. /**
  50. * @param message A literal text message or localization key.
  51. */
  52. static success(message, params) {
  53. ContentAlert.instance.postAlert(AlertVariant.success, message, params);
  54. }
  55. /**
  56. * @param message A literal text message or localization key.
  57. */
  58. static danger(message, params) {
  59. ContentAlert.instance.postAlert(AlertVariant.danger, message, params);
  60. }
  61. /**
  62. * @param message A literal text message or localization key.
  63. */
  64. static warning(message, params) {
  65. ContentAlert.instance.postAlert(AlertVariant.warning, message, params);
  66. }
  67. /**
  68. * @param message A literal text message or localization key.
  69. */
  70. static info(message, params) {
  71. ContentAlert.instance.postAlert(AlertVariant.info, message, params);
  72. }
  73. render() {
  74. return React.createElement(AlertGroup, {
  75. isToast: true,
  76. "aria-live": "assertive"
  77. }, this.state.alerts.map(({
  78. key,
  79. variant,
  80. message
  81. }) => React.createElement(Alert, {
  82. "aria-details": message,
  83. isLiveRegion: true,
  84. variant: variant,
  85. title: message,
  86. action: React.createElement(AlertActionCloseButton, {
  87. title: message,
  88. variantLabel: `${variant} alert`,
  89. onClose: () => this.hideAlert(key)
  90. }),
  91. key: key
  92. })));
  93. }
  94. }
  95. _defineProperty(ContentAlert, "instance", void 0);
  96. //# sourceMappingURL=ContentAlert.js.map