diff --git a/src/components/Legend/LegendIcon/demos/custom.tsx b/src/components/Legend/LegendIcon/demos/custom.tsx new file mode 100644 index 00000000..af1da387 --- /dev/null +++ b/src/components/Legend/LegendIcon/demos/custom.tsx @@ -0,0 +1,17 @@ +import { LegendIcon } from '@antv/larkmap'; +import React from 'react'; + +export default () => { + return ( +
+ , + , + , + ]} + /> +
+ ); +}; diff --git a/src/components/Legend/LegendIcon/index.less b/src/components/Legend/LegendIcon/index.less index 804ddc6d..461579f8 100644 --- a/src/components/Legend/LegendIcon/index.less +++ b/src/components/Legend/LegendIcon/index.less @@ -6,7 +6,7 @@ align-items: center; line-height: 2; - &__icon { + > *:first-child { width: 16px; height: 16px; margin-right: 10px diff --git a/src/components/Legend/LegendIcon/index.md b/src/components/Legend/LegendIcon/index.md index fa402ce5..e503f5a1 100644 --- a/src/components/Legend/LegendIcon/index.md +++ b/src/components/Legend/LegendIcon/index.md @@ -21,8 +21,18 @@ nav: +#### 自定义 icons + + + #### 在地图中展示 + +#### IconType + +```ts +type IconType = string | React.ReactElement; +``` diff --git a/src/components/Legend/LegendIcon/index.tsx b/src/components/Legend/LegendIcon/index.tsx index ea1f9b61..f6798438 100644 --- a/src/components/Legend/LegendIcon/index.tsx +++ b/src/components/Legend/LegendIcon/index.tsx @@ -1,17 +1,26 @@ -import React from 'react'; import classnames from 'classnames'; -import type { LegendIconProps } from './types'; +import React from 'react'; import './index.less'; +import type { IconType, LegendIconProps } from './types'; export const CLS_PREFIX = 'larkmap-legend-icon'; export const LegendIcon = (props: LegendIconProps) => { const { labels, icons, className: cls, style } = props; + + const renderIcon = (icon: IconType) => { + if (React.isValidElement(icon)) { + return icon; + } else if (typeof icon === 'string') { + return ; + } + }; + return (
{labels.map((item, index) => (
- + {renderIcon(icons[index])}
{item}
))} diff --git a/src/components/Legend/LegendIcon/types.ts b/src/components/Legend/LegendIcon/types.ts index a92a5cb2..706ab6bd 100644 --- a/src/components/Legend/LegendIcon/types.ts +++ b/src/components/Legend/LegendIcon/types.ts @@ -1,8 +1,10 @@ import type { CommonProps } from '../../../types/common'; +export type IconType = string | React.ReactElement; + export interface LegendIconProps extends CommonProps { /** 图例项名称 */ labels: string[]; /** 图例项图标 */ - icons: string[]; + icons: IconType[]; }