Skip to content

Commit

Permalink
refactor: 调整 stories 实现
Browse files Browse the repository at this point in the history
懒渲染 amap 实例,避免 docs 下示例过多导致卡顿;
不再使用基于根目录引用的方式,ide 插件兼容性参差不齐;
使用单独目录放置storybook装饰器;
  • Loading branch information
xyy94813 committed Oct 24, 2023
1 parent ef8dee9 commit 2cf80ad
Show file tree
Hide file tree
Showing 23 changed files with 281 additions and 100 deletions.
18 changes: 9 additions & 9 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require('path');
const path = require("path");

module.exports = {
logLevel: 'debug',
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
addons: ['@storybook/addon-essentials'],
logLevel: "debug",
stories: ["../src/**/*.stories.@(js|jsx|ts|tsx|mdx)"],
addons: ["@storybook/addon-essentials"],
/**
* storybook with typescript
* see https://storybook.js.org/docs/configurations/typescript-config/
Expand All @@ -13,19 +13,19 @@ module.exports = {
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('ts-loader'),
loader: require.resolve("ts-loader"),
},
// Optional
{
loader: require.resolve('react-docgen-typescript-loader'),
loader: require.resolve("react-docgen-typescript-loader"),
},
],
});
config.resolve.extensions.unshift('.ts', '.tsx', '.mdx');
config.resolve.modules = [...config.resolve.modules, path.resolve(__dirname, '..', 'src')];
config.resolve.extensions.unshift(".ts", ".tsx", ".mdx");
// config.resolve.modules = [...config.resolve.modules, path.resolve(__dirname, '..', 'src')];
config.resolve.alias = {
...config.resolve.alias,
'@': path.resolve(__dirname, '..', 'src'),
// '@': path.resolve(__dirname, '..', 'src'),
};
return config;
},
Expand Down
11 changes: 6 additions & 5 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { Suspense } from "react";

export const decorators = [
(renderStory, context) => (
<Suspense fallback="loading">{renderStory({}, context)}</Suspense>
),
];
const withSuspense = (renderStory, context) => (
<Suspense fallback="loading">{renderStory({}, context)}</Suspense>
);

export const decorators = [withSuspense];

export const parameters = {
options: {
storySort: {
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"lint-staged": "^13.0.3",
"prettier": "^2.0.5",
"react-docgen-typescript-loader": "^3.7.2",
"react-intersection-observer": "^9.5.2",
"rollup": "^2.40.0",
"rollup-plugin-filesize": "^10.0.0",
"rollup-plugin-terser": "^7.0.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { useRef, useEffect, Suspense } from 'react';
import { Meta, Story } from '@storybook/react';
import type { Meta, Story } from '@storybook/react';

import { createAMapAPIContainer, useAMapAPI } from 'index';
import { createAMapAPIContainer, useAMapAPI } from '../../../index';

const AMAP_API_KEY: string = process.env.STORYBOOK_AMAP_API_KEY || '';

export default {
title: '组件(Components)/基础组件(Basic)/AMapAPIContainer',
excludeStories: ['AsyncPluginAPIContainer'],
} as Meta;

const SyncPluginAPIContainer = createAMapAPIContainer({
Expand All @@ -22,7 +21,7 @@ const SyncPluginAPIContainer = createAMapAPIContainer({
},
});

export const AsyncPluginAPIContainer = createAMapAPIContainer({
const AsyncPluginAPIContainer = createAMapAPIContainer({
key: AMAP_API_KEY,
version: '2.0',
AMapUI: {
Expand Down
18 changes: 13 additions & 5 deletions src/components/AMapCircle/stories/AMapCircle.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import { Meta, Story } from '@storybook/react';
import type { Meta, Story } from '@storybook/react';
import { actions } from '@storybook/addon-actions';

import { AMapCircle, AMapCircleProps } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';
import withAutoFitView from '../../AMapAutoFitView/stories/withAutoFitView';
import { AMapCircle, AMapCircleProps } from '../../../index';

import withAutoFitView from '../../../storybook-decorators/withAutoFitView';
import withAMap from '../../../storybook-decorators/withAMap';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

const eventHandler = actions(
'onShow',
Expand All @@ -23,7 +26,12 @@ const eventHandler = actions(

export default {
title: '组件(Components)/覆盖物(Overlay)/AMapCircle',
decorators: [withAutoFitView, withAMapContainer],
decorators: [
withAutoFitView,
withAMap,
withAMapContainer,
withAPIContainer,
],
args: {
center: [116.39, 39.9],
radius: 10_000,
Expand Down
15 changes: 11 additions & 4 deletions src/components/AMapControlBar/stories/AMapControlBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import React from 'react';
import { Story, Meta } from '@storybook/react';
import type { Story, Meta } from '@storybook/react';
import { actions } from '@storybook/addon-actions';

import { AMapControlBar, AMapControlBarProps } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';
import { AMapControlBar, AMapControlBarProps } from '../../../index';

import withAMap from '../../../storybook-decorators/withAMap';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

const eventHandler = actions('onShow', 'onHide');

export default {
title: '组件(Components)/地图控件(Control)/AMapControlBar',
component: AMapControlBar,
decorators: [withAMapContainer],
decorators: [
withAMap,
withAMapContainer,
withAPIContainer,
],
args: {
position: 'LT',
offset: [10, 10],
Expand Down
18 changes: 13 additions & 5 deletions src/components/AMapEllipse/stories/AMapEllipse.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import { Meta, Story } from '@storybook/react';
import type { Meta, Story } from '@storybook/react';
import { actions } from '@storybook/addon-actions';

import { AMapEllipse, AMapEllipseProps } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';
import withAutoFitView from '../../AMapAutoFitView/stories/withAutoFitView';
import { AMapEllipse, AMapEllipseProps } from '../../../index';

import withAutoFitView from '../../../storybook-decorators/withAutoFitView';
import withAMap from '../../../storybook-decorators/withAMap';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

const eventHandler = actions(
'onShow',
Expand All @@ -23,7 +26,12 @@ const eventHandler = actions(

export default {
title: '组件(Components)/覆盖物(Overlay)/AMapEllipse',
decorators: [withAutoFitView, withAMapContainer],
decorators: [
withAutoFitView,
withAMap,
withAMapContainer,
withAPIContainer,
],
args: {
center: [116.39, 39.9],
radius: [10_000, 5_000],
Expand Down
20 changes: 14 additions & 6 deletions src/components/AMapGeoJSON/stories/AMapGeoJSON.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import { Meta, Story } from '@storybook/react';
import type { Meta, Story } from '@storybook/react';

import type { AMapGeoJSONGetOverlayCallback, AMapGeoJSONProps } from 'index';
import { AMapGeoJSON, coordsOfGeoJSON2AMapPolygonPath } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';
import withAutoFitView from '../../AMapAutoFitView/stories/withAutoFitView';
import type { AMapGeoJSONGetOverlayCallback, AMapGeoJSONProps } from '../../../index';
import { AMapGeoJSON, coordsOfGeoJSON2AMapPolygonPath } from '../../../index';

import withAutoFitView from '../../../storybook-decorators/withAutoFitView';
import withAMap from '../../../storybook-decorators/withAMap';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

const point: GeoJSON.Point = {
type: 'Point',
Expand Down Expand Up @@ -97,7 +100,12 @@ const mockData: GeoJSON.FeatureCollection = {

export default {
title: '组件(Components)/覆盖物(Overlay)/AMapGeoJSON',
decorators: [withAutoFitView, withAMapContainer],
decorators: [
withAutoFitView,
withAMap,
withAMapContainer,
withAPIContainer,
],
args: {
geoJSON: mockData,
visible: true,
Expand Down
15 changes: 11 additions & 4 deletions src/components/AMapHawkEye/stories/AMapHawkEye.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import React from 'react';
import { Story, Meta } from '@storybook/react';
import type { Story, Meta } from '@storybook/react';
import { actions } from '@storybook/addon-actions';

import { AMapHawkEye, AMapHawkEyeProps } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';
import { AMapHawkEye, AMapHawkEyeProps } from '../../../index';

import withAMap from '../../../storybook-decorators/withAMap';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

const eventHandler = actions('onShow', 'onHide', 'onOpen', 'onClose');

export default {
title: '组件(Components)/地图控件(Control)/AMapHawkEye',
component: AMapHawkEye,
decorators: [withAMapContainer],
decorators: [
withAMap,
withAMapContainer,
withAPIContainer,
],
args: {
autoMove: true,
showRectangle: true,
Expand Down
27 changes: 8 additions & 19 deletions src/components/AMapMap/stories/AMapMap.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import React from 'react';
import { Meta, Story } from '@storybook/react';
import type { Meta, Story } from '@storybook/react';

import { AMapMap, AMapMapProps } from 'index';
import { AMapMap, AMapMapProps } from '../../../index';

import { AsyncPluginAPIContainer as APIContainer } from '../../AMapAPIContainer/stories/createAMapAPIContainer.stories';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

AMapMap.displayName = 'AMapMap';

const withAPIContainer = (renderStory: Story, context: any) => (
<APIContainer>
<div style={{ height: '400px' }}>{renderStory({}, context)}</div>
</APIContainer>
);

export default {
title: '组件(Components)/基础组件(Basic)/AMapMap',
component: AMapMap,
decorators: [withAPIContainer],
decorators: [
withAMapContainer,
withAPIContainer,
],
args: {
center: undefined,
cityName: undefined,
Expand Down Expand Up @@ -46,7 +44,6 @@ export default {
},
},
},
excludeStories: ['withAMapContainer'],
} as Meta;

const Template: Story<AMapMapProps> = (args) => <AMapMap {...args} />;
Expand All @@ -68,11 +65,3 @@ SetZoom.args = {
zoom: 12,
};
SetZoom.storyName = '设置缩放比例';

export const withAMapContainer = (renderStory: Story, context: any) => (
<APIContainer>
<div style={{ height: 'calc(100vh - 70px)' }}>
<AMapMap>{renderStory({}, context)}</AMapMap>
</div>
</APIContainer>
);
15 changes: 11 additions & 4 deletions src/components/AMapMapType/stories/AMapMapType.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import React from 'react';
import { Story, Meta } from '@storybook/react';
import type { Story, Meta } from '@storybook/react';
import { actions } from '@storybook/addon-actions';

import { AMapMapType, AMapMapTypeProps } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';
import { AMapMapType, AMapMapTypeProps } from '../../../index';

import withAMap from '../../../storybook-decorators/withAMap';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

const eventHandler = actions('onShow', 'onHide');

export default {
title: '组件(Components)/地图控件(Control)/AMapMapType',
component: AMapMapType,
decorators: [withAMapContainer],
decorators: [
withAMap,
withAMapContainer,
withAPIContainer,
],
args: {
defaultType: 0,
visible: true,
Expand Down
21 changes: 17 additions & 4 deletions src/components/AMapMarker/stories/AMapMarker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React from 'react';
import { renderToString } from 'react-dom/server';
import { Story, Meta } from '@storybook/react';
import type { Story, Meta } from '@storybook/react';
import { actions } from '@storybook/addon-actions';

import { AMapToolBar, AMapMarker, AMapMarkerProps } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';
import {
AMapToolBar,
AMapMarker,
AMapMarkerProps,
} from '../../../index';

import withAutoFitView from '../../../storybook-decorators/withAutoFitView';
import withAMap from '../../../storybook-decorators/withAMap';
import withAMapContainer from '../../../storybook-decorators/withAMapContainer';
import withAPIContainer from '../../../storybook-decorators/withAPIContainer';

const eventHandler = actions(
'onHide',
Expand All @@ -31,7 +39,12 @@ const eventHandler = actions(
export default {
title: '组件(Components)/覆盖物(Overlay)/AMapMarker',
component: AMapMarker,
decorators: [withAMapContainer],
decorators: [
withAutoFitView,
withAMap,
withAMapContainer,
withAPIContainer,
],
args: {
position: [116.39, 39.9],
title: '自定义的 Title',
Expand Down
Loading

0 comments on commit 2cf80ad

Please sign in to comment.