Skip to content

Commit

Permalink
feat: LegendIcons自定义icons (#225)
Browse files Browse the repository at this point in the history
* feat: 自定义icons

* feat: legendicon icon名称调整

---------

Co-authored-by: shengzheng <[email protected]>
  • Loading branch information
hsp-sz and shengzheng authored Apr 10, 2024
1 parent 9798189 commit 2c7f7db
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/components/Legend/LegendIcon/demos/custom.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LegendIcon } from '@antv/larkmap';
import React from 'react';

export default () => {
return (
<div>
<LegendIcon
labels={['枫叶图标', '火车图标', '小汽车图标']}
icons={[
<img src="https://gw.alipayobjects.com/mdn/rms_5e897d/afts/img/A*6ONoRKNECC0AAAAAAAAAAAAAARQnAQ" />,
<img src="https://gw.alipayobjects.com/zos/bmw-prod/e21321e0-8f4a-474f-a0ee-2176492bb824.svg" />,
<img src="https://gw.alipayobjects.com/zos/bmw-prod/7fb22e05-4488-4597-8e33-fc03716d2b0a.svg" />,
]}
/>
</div>
);
};
2 changes: 1 addition & 1 deletion src/components/Legend/LegendIcon/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
align-items: center;
line-height: 2;

&__icon {
> *:first-child {
width: 16px;
height: 16px;
margin-right: 10px
Expand Down
10 changes: 10 additions & 0 deletions src/components/Legend/LegendIcon/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ nav:

<code src="./demos/default.tsx" defaultShowCode></code>

#### 自定义 icons

<code src="./demos/custom.tsx" defaultShowCode></code>

#### 在地图中展示

<code src="./demos/map-default.tsx" compact defaultShowCode></code>

<API></API>

#### IconType

```ts
type IconType = string | React.ReactElement;
```
15 changes: 12 additions & 3 deletions src/components/Legend/LegendIcon/index.tsx
Original file line number Diff line number Diff line change
@@ -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 <img src={icon} />;
}
};

return (
<div className={classnames(`${CLS_PREFIX}`, cls)} style={style}>
{labels.map((item, index) => (
<div key={item} className={`${CLS_PREFIX}__content`}>
<img src={icons[index]} className={`${CLS_PREFIX}__content__icon`} />
{renderIcon(icons[index])}
<div>{item}</div>
</div>
))}
Expand Down
4 changes: 3 additions & 1 deletion src/components/Legend/LegendIcon/types.ts
Original file line number Diff line number Diff line change
@@ -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[];
}

0 comments on commit 2c7f7db

Please sign in to comment.