From e70f700486c21cd628187f6bc6bac9f131ded0ce Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Tue, 11 Feb 2020 20:31:55 +0800 Subject: [PATCH] feat(react): add react lib --- .storybook/config.ts | 2 +- .../core/src/services/layer/ILayerService.ts | 2 +- packages/react/README.md | 11 ++++ packages/react/__tests__/react.test.js | 7 +++ packages/react/package.json | 36 +++++++++++ packages/react/src/component/Layer.ts | 17 ++++++ .../src/component/LayerAttribute/Color.tsx | 18 ++++++ .../src/component/LayerAttribute/Layer.tsx | 51 ++++++++++++++++ .../src/component/LayerAttribute/Scales.tsx | 21 +++++++ .../src/component/LayerAttribute/Shape.tsx | 18 ++++++ .../src/component/LayerAttribute/Size.tsx | 18 ++++++ .../src/component/LayerAttribute/Source.tsx | 19 ++++++ .../src/component/LayerAttribute/Style.tsx | 16 +++++ .../src/component/LayerAttribute/index.ts | 38 ++++++++++++ packages/react/src/component/Scene.tsx | 42 +++++++++++++ packages/react/src/component/SceneContext.tsx | 7 +++ packages/react/src/index.ts | 3 + packages/react/tsconfig.build.json | 9 +++ stories/React/React.stories.tsx | 6 ++ stories/React/components/Scene.tsx | 60 +++++++++++++++++++ 20 files changed, 399 insertions(+), 2 deletions(-) create mode 100644 packages/react/README.md create mode 100644 packages/react/__tests__/react.test.js create mode 100644 packages/react/package.json create mode 100644 packages/react/src/component/Layer.ts create mode 100644 packages/react/src/component/LayerAttribute/Color.tsx create mode 100644 packages/react/src/component/LayerAttribute/Layer.tsx create mode 100644 packages/react/src/component/LayerAttribute/Scales.tsx create mode 100644 packages/react/src/component/LayerAttribute/Shape.tsx create mode 100644 packages/react/src/component/LayerAttribute/Size.tsx create mode 100644 packages/react/src/component/LayerAttribute/Source.tsx create mode 100644 packages/react/src/component/LayerAttribute/Style.tsx create mode 100644 packages/react/src/component/LayerAttribute/index.ts create mode 100644 packages/react/src/component/Scene.tsx create mode 100644 packages/react/src/component/SceneContext.tsx create mode 100644 packages/react/src/index.ts create mode 100644 packages/react/tsconfig.build.json create mode 100644 stories/React/React.stories.tsx create mode 100644 stories/React/components/Scene.tsx diff --git a/.storybook/config.ts b/.storybook/config.ts index 8d0e91a35c..6169a25011 100644 --- a/.storybook/config.ts +++ b/.storybook/config.ts @@ -1,5 +1,5 @@ // tslint:disable-next-line:no-submodule-imports -import '!style-loader!css-loader!sass-loader!./iframe.scss'; +// import '!style-loader!css-loader!sass-loader!./iframe.scss'; import { addParameters, configure } from '@storybook/react'; import { create } from '@storybook/theming'; diff --git a/packages/core/src/services/layer/ILayerService.ts b/packages/core/src/services/layer/ILayerService.ts index b1575a0d07..8c164f1b56 100644 --- a/packages/core/src/services/layer/ILayerService.ts +++ b/packages/core/src/services/layer/ILayerService.ts @@ -116,7 +116,7 @@ export interface ILayer { Partial, ): IModel; init(): ILayer; - scale(field: string | IScaleOptions, cfg: IScale): ILayer; + scale(field: string | IScaleOptions, cfg?: IScale): ILayer; size(field: StyleAttrField, value?: StyleAttributeOption): ILayer; color(field: StyleAttrField, value?: StyleAttributeOption): ILayer; shape(field: StyleAttrField, value?: StyleAttributeOption): ILayer; diff --git a/packages/react/README.md b/packages/react/README.md new file mode 100644 index 0000000000..e3aedcdb13 --- /dev/null +++ b/packages/react/README.md @@ -0,0 +1,11 @@ +# `react` + +> TODO: description + +## Usage + +``` +const react = require('react'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/react/__tests__/react.test.js b/packages/react/__tests__/react.test.js new file mode 100644 index 0000000000..b675145f41 --- /dev/null +++ b/packages/react/__tests__/react.test.js @@ -0,0 +1,7 @@ +'use strict'; + +const react = require('..'); + +describe('react', () => { + it('needs tests'); +}); diff --git a/packages/react/package.json b/packages/react/package.json new file mode 100644 index 0000000000..18c422c3a0 --- /dev/null +++ b/packages/react/package.json @@ -0,0 +1,36 @@ +{ + "name": "@antv/l7-react", + "version": "2.0.18", + "description": "", + "main": "lib/index.js", + "module": "es/index.js", + "types": "es/index.d.ts", + "sideEffects": true, + "files": [ + "lib", + "es", + "README.md" + ], + "scripts": { + "tsc": "tsc --project tsconfig.build.json", + "clean": "rimraf dist; rimraf es; rimraf lib;", + "build": "run-p build:*", + "build:cjs": "BABEL_ENV=cjs babel src --root-mode upward --out-dir lib --source-maps --extensions .ts,.tsx --delete-dir-on-start --no-comments", + "build:esm": "BABEL_ENV=esm babel src --root-mode upward --out-dir es --source-maps --extensions .ts,.tsx --delete-dir-on-start --no-comments", + "watch": "BABEL_ENV=cjs babel src --watch --root-mode upward --out-dir lib --source-maps --extensions .ts,.tsx --delete-dir-on-start --no-comments", + "lint:ts": "run-p -c lint:ts-*", + "test": "jest" + }, + "author": "lzxue", + "license": "ISC", + "dependencies": { + "@antv/l7": "^2.0.18", + "@antv/l7-maps": "^2.0.18", + "react": "^16.8.6", + "@babel/runtime": "^7.7.7" + }, + "gitHead": "f2bd3c6473df79d815467b1677c6f985cf68800e", + "publishConfig": { + "access": "public" + } +} diff --git a/packages/react/src/component/Layer.ts b/packages/react/src/component/Layer.ts new file mode 100644 index 0000000000..4deebbc545 --- /dev/null +++ b/packages/react/src/component/Layer.ts @@ -0,0 +1,17 @@ +import * as React from 'react'; +import { ILayerProps } from './LayerAttribute'; +import BaseLayer from './LayerAttribute/Layer'; + +const PolygonLayer = React.memo(function Layer(props: ILayerProps) { + return BaseLayer('polygonLayer', props); +}); + +const LineLayer = React.memo(function Layer(props: ILayerProps) { + return BaseLayer('polygonLayer', props); +}); + +const PointLayer = React.memo(function Layer(props: ILayerProps) { + return BaseLayer('pointLayer', props); +}); + +export { PolygonLayer, LineLayer, PointLayer }; diff --git a/packages/react/src/component/LayerAttribute/Color.tsx b/packages/react/src/component/LayerAttribute/Color.tsx new file mode 100644 index 0000000000..f61cb9d161 --- /dev/null +++ b/packages/react/src/component/LayerAttribute/Color.tsx @@ -0,0 +1,18 @@ +import { ILayer, StyleAttrField } from '@antv/l7'; +import * as React from 'react'; +import { IAttributeOptions } from './'; + +const { useEffect } = React; +interface ILayerProps { + layer: ILayer; + color: Partial; +} +export default React.memo(function Chart(props: ILayerProps) { + const { layer, color } = props; + useEffect(() => { + color.field + ? layer.color(color.field as StyleAttrField, color.values) + : layer.color(color.value as StyleAttrField); + }, [color.value, color.field, JSON.stringify(color.values)]); + return null; +}); diff --git a/packages/react/src/component/LayerAttribute/Layer.tsx b/packages/react/src/component/LayerAttribute/Layer.tsx new file mode 100644 index 0000000000..983a69596d --- /dev/null +++ b/packages/react/src/component/LayerAttribute/Layer.tsx @@ -0,0 +1,51 @@ +import { ILayer, LineLayer, PointLayer, PolygonLayer, Scene } from '@antv/l7'; +import * as React from 'react'; +import { useSceneValue } from '../SceneContext'; +import { Color, ILayerProps, Scales, Shape, Size, Source, Style } from './'; + +const { useEffect, useState } = React; + +export default function BaseLayer(type: string, props: ILayerProps) { + const { source, color, shape, style, size, scales, options } = props; + const mapScene = (useSceneValue() as unknown) as Scene; + const [layer, setLayer] = useState(); + if (!layer) { + let l: ILayer; + switch (type) { + case 'polygonLayer': + l = new PolygonLayer(options); + break; + case 'lineLayer': + l = new LineLayer(options); + break; + case 'pointLayer': + l = new PointLayer(options); + break; + default: + l = new PolygonLayer(options); + } + setLayer(l); + } + + useEffect(() => { + mapScene.addLayer(layer); + return () => { + mapScene.removeLayer(layer); + }; + }, []); + useEffect(() => { + if (layer) { + mapScene.render(); + } + }); + return ( + <> + + {scales && } + + {size && } + + {style &&