Skip to content

Commit

Permalink
feat(map): add className、containerStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 16, 2024
1 parent d0e05c0 commit c052112
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .storybook/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addons } from '@storybook/addons';
import { create } from '@storybook/theming/create';

const theme = create({
base: 'light',
base: 'dark',

// Brand assets
brandTitle: 'ReactMaoboxGl',
Expand All @@ -11,6 +11,6 @@ const theme = create({

addons.setConfig({
theme,
showPanel: true, // show addons panel by default
panelPosition: 'right', // position addons panel on the right by default
showPanel: true,
panelPosition: 'right',
});
7 changes: 7 additions & 0 deletions src/map/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import React from 'react';
import type { EventMapping } from './types';
import type { KeysOfUnion } from '../types';

export const defaultContainerStyle: React.CSSProperties = {
width: '100%',
height: '100%',
position: 'relative',
};

export const MapEventMap: EventMapping = {
onError: 'error',

Expand Down
1 change: 1 addition & 0 deletions src/map/map.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
a.mapboxgl-ctrl-logo,
.mapboxgl-ctrl.mapboxgl-ctrl-attrib,
.mapboxgl-ctrl-attrib.mapboxgl-compact {
display: none;
}
13 changes: 10 additions & 3 deletions src/map/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRef, useState, useEffect, forwardRef, useImperativeHandle } from 're
import { MapContext } from './context';
import { useReact } from '@/hooks/useReact';
import { allProps, setterMap, converterMap } from './config';
import { MapEventMap, MapEventList } from './constant';
import { defaultContainerStyle, MapEventMap, MapEventList } from './constant';
import { useEvents } from '@/hooks/useEvents';

import 'mapbox-gl/dist/mapbox-gl.css';
Expand All @@ -16,7 +16,7 @@ Mapbox.accessToken =
'pk.eyJ1IjoienQyMDIzMTEwOSIsImEiOiJjbG9xdGgxcDMwbDAyMmpwODVrNG5seXphIn0.1xKSk8Ll-80kkEwtzfLWhw';

export const Map = forwardRef<Mapbox.Map, MapProps>((props, ref) => {
const { loading, children } = props;
const { className, loading, containerStyle, children } = props;
const containerRef = useRef<HTMLDivElement>(null);
const [mapInstance, setMapInstance] = useState<Mapbox.Map>();

Expand Down Expand Up @@ -72,7 +72,14 @@ export const Map = forwardRef<Mapbox.Map, MapProps>((props, ref) => {
};

return (
<div ref={containerRef} style={{ width: 500, height: 500 }}>
<div
ref={containerRef}
style={{
...defaultContainerStyle,
...containerStyle,
}}
className={className}
>
{!mapInstance && loading}
{mapInstance && <MapContext.Provider value={contextValue}>{children}</MapContext.Provider>}
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/map/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export type MapboxOptionKeys = KeysOfUnion<MapboxOptions>;
export interface MapProps extends Omit<MapOptions, 'container'>, Partial<MapEvents> {
/** 地图加载前的加载效果 */
loading?: React.ReactNode;
/** 额外的样式类 */
className?: string;
/** 地图挂载节点样式 */
containerStyle?: React.CSSProperties;
children?: React.ReactNode;
}
7 changes: 6 additions & 1 deletion stories/Map.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ const meta = {
title: '组件/Map',
component: Map,
parameters: {
layout: 'centered',
layout: 'fullscreen',
},
tags: ['autodocs'],
argTypes: {},
args: {
containerStyle: {
height: 500,
},
},
} satisfies Meta<typeof Map>;

export default meta;
Expand Down

0 comments on commit c052112

Please sign in to comment.