ResizableChartDemo.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. import { useState, ReactNode, SyntheticEvent } from 'react';
  20. import { styled } from '@superset-ui/core';
  21. import type { Decorator } from '@storybook/react';
  22. import { ResizeCallbackData } from 'react-resizable';
  23. import ResizablePanel, { Size } from './ResizablePanel';
  24. export const SupersetBody = styled.div`
  25. background: ${({ theme }) => theme.colorBgLayout};
  26. padding: 16px;
  27. min-height: 100%;
  28. .panel {
  29. margin-bottom: 0;
  30. }
  31. `;
  32. export default function ResizableChartDemo({
  33. children,
  34. panelPadding = 30,
  35. initialSize = { width: 500, height: 300 },
  36. }: {
  37. children: (innerSize: Size) => ReactNode;
  38. panelPadding?: number;
  39. initialSize?: Size;
  40. }) {
  41. // size are all inner size
  42. const [size, setSize] = useState(initialSize);
  43. return (
  44. <SupersetBody>
  45. <ResizablePanel
  46. initialSize={initialSize}
  47. onResize={(e: SyntheticEvent, data: ResizeCallbackData) =>
  48. setSize(data.size)
  49. }
  50. >
  51. {children({
  52. width: size.width - panelPadding,
  53. height: size.height - panelPadding,
  54. })}
  55. </ResizablePanel>
  56. </SupersetBody>
  57. );
  58. }
  59. export const withResizableChartDemo: Decorator<{
  60. width: number;
  61. height: number;
  62. }> = (storyFn, context) => {
  63. const {
  64. parameters: { initialSize, panelPadding },
  65. } = context;
  66. return (
  67. <ResizableChartDemo
  68. initialSize={initialSize as Size | undefined}
  69. panelPadding={panelPadding}
  70. >
  71. {innerSize => storyFn({ ...context, ...context.args, ...innerSize })}
  72. </ResizableChartDemo>
  73. );
  74. };