MockChartPlugins.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* eslint-disable max-classes-per-file */
  20. import {
  21. QueryFormData,
  22. ChartMetadata,
  23. ChartPlugin,
  24. useTheme,
  25. } from '@superset-ui/core';
  26. const DIMENSION_STYLE = {
  27. fontSize: 36,
  28. fontWeight: 700,
  29. flex: '1 1 auto',
  30. display: 'flex',
  31. alignItems: 'center',
  32. };
  33. export const TestComponent = ({
  34. formData,
  35. message,
  36. width,
  37. height,
  38. }: {
  39. formData?: unknown;
  40. message?: string;
  41. width?: number;
  42. height?: number;
  43. }) => {
  44. const theme = useTheme();
  45. return (
  46. <div
  47. className="test-component"
  48. style={{
  49. width,
  50. height,
  51. backgroundColor: theme.colorPrimaryBg,
  52. color: theme.colorTextSecondary,
  53. display: 'flex',
  54. flexDirection: 'column',
  55. alignItems: 'center',
  56. borderRadius: 8,
  57. }}
  58. >
  59. <div className="message" style={{ padding: 10 }}>
  60. {message ?? 'custom component'}
  61. </div>
  62. <div className="dimension" style={DIMENSION_STYLE}>
  63. {[width, height].join('x')}
  64. </div>
  65. <div className="formData" style={{ padding: 10 }}>
  66. <code style={{ color: theme.colorTextSecondary }}>
  67. {JSON.stringify(formData)}
  68. </code>
  69. </div>
  70. </div>
  71. );
  72. };
  73. export const ChartKeys = {
  74. DILIGENT: 'diligent-chart',
  75. LAZY: 'lazy-chart',
  76. SLOW: 'slow-chart',
  77. BUGGY: 'buggy-chart',
  78. };
  79. export class DiligentChartPlugin extends ChartPlugin<QueryFormData> {
  80. constructor() {
  81. super({
  82. metadata: new ChartMetadata({
  83. name: ChartKeys.DILIGENT,
  84. thumbnail: '',
  85. }),
  86. Chart: TestComponent,
  87. transformProps: x => x,
  88. });
  89. }
  90. }
  91. function identity<T>(x: T) {
  92. return x;
  93. }
  94. export class LazyChartPlugin extends ChartPlugin<QueryFormData> {
  95. constructor() {
  96. super({
  97. metadata: new ChartMetadata({
  98. name: ChartKeys.LAZY,
  99. thumbnail: '',
  100. }),
  101. // this mirrors `() => import(module)` syntax
  102. loadChart: () => Promise.resolve({ default: TestComponent }),
  103. // promise without .default
  104. loadTransformProps: () => Promise.resolve(identity),
  105. });
  106. }
  107. }
  108. export class SlowChartPlugin extends ChartPlugin<QueryFormData> {
  109. constructor() {
  110. super({
  111. metadata: new ChartMetadata({
  112. name: ChartKeys.SLOW,
  113. thumbnail: '',
  114. }),
  115. loadChart: () =>
  116. new Promise(resolve => {
  117. setTimeout(() => {
  118. resolve(TestComponent);
  119. }, 1000);
  120. }),
  121. transformProps: x => x,
  122. });
  123. }
  124. }
  125. export class BuggyChartPlugin extends ChartPlugin<QueryFormData> {
  126. constructor() {
  127. super({
  128. metadata: new ChartMetadata({
  129. name: ChartKeys.BUGGY,
  130. thumbnail: '',
  131. }),
  132. Chart: () => {
  133. throw new Error('The component is too buggy to render.');
  134. },
  135. transformProps: x => x,
  136. });
  137. }
  138. }