|
1 | | -import React from 'react'; |
| 1 | +import React, { useId, useState } from 'react'; |
| 2 | +import { Button, Popover } from '@ui5/webcomponents-react'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { ColorBy } from './types'; |
2 | 5 | import styles from './Legend.module.css'; |
| 6 | + |
3 | 7 | export interface LegendItem { |
4 | 8 | name: string; |
5 | 9 | color: string; |
6 | 10 | } |
7 | 11 |
|
8 | 12 | interface LegendProps { |
9 | 13 | legendItems: LegendItem[]; |
| 14 | + colorBy: ColorBy; |
| 15 | + onColorByChange: (colorBy: ColorBy) => void; |
10 | 16 | } |
11 | 17 |
|
12 | | -export const Legend: React.FC<LegendProps> = ({ legendItems }) => { |
| 18 | +export const Legend: React.FC<LegendProps> = ({ legendItems, colorBy, onColorByChange }) => { |
| 19 | + const { t } = useTranslation(); |
| 20 | + const [colorPopoverOpen, setColorPopoverOpen] = useState(false); |
| 21 | + const colorButtonId = useId(); |
| 22 | + |
13 | 23 | return ( |
14 | | - <div className={styles.legendContainer}> |
15 | | - {legendItems.map(({ name, color }) => ( |
16 | | - <div key={name} className={styles.legendRow}> |
17 | | - <div className={styles.legendColorBox} style={{ backgroundColor: color }} /> |
18 | | - <span>{name}</span> |
19 | | - </div> |
20 | | - ))} |
| 24 | + <div className={styles.legendWrapper}> |
| 25 | + <div className={styles.legendContainer}> |
| 26 | + {legendItems.map(({ name, color }) => ( |
| 27 | + <div key={name} className={styles.legendRow}> |
| 28 | + <div className={styles.legendColorBox} style={{ backgroundColor: color }} /> |
| 29 | + <span>{name}</span> |
| 30 | + </div> |
| 31 | + ))} |
| 32 | + </div> |
| 33 | + <div className={styles.colorFilterContainer}> |
| 34 | + <Popover |
| 35 | + opener={colorButtonId} |
| 36 | + open={colorPopoverOpen} |
| 37 | + placement="Bottom" |
| 38 | + onClose={() => setColorPopoverOpen(false)} |
| 39 | + > |
| 40 | + <div className={styles.popoverContent}> |
| 41 | + <h4 className={styles.popoverHeader}>{t('Graphs.colorBy')}</h4> |
| 42 | + <div className={styles.popoverButtonContainer}> |
| 43 | + <Button |
| 44 | + design={colorBy === 'source' ? 'Emphasized' : 'Default'} |
| 45 | + onClick={() => { |
| 46 | + onColorByChange('source'); |
| 47 | + setColorPopoverOpen(false); |
| 48 | + }} |
| 49 | + > |
| 50 | + {t('Graphs.colorsProvider')} |
| 51 | + </Button> |
| 52 | + <Button |
| 53 | + design={colorBy === 'provider' ? 'Emphasized' : 'Default'} |
| 54 | + onClick={() => { |
| 55 | + onColorByChange('provider'); |
| 56 | + setColorPopoverOpen(false); |
| 57 | + }} |
| 58 | + > |
| 59 | + {t('Graphs.colorsProviderConfig')} |
| 60 | + </Button> |
| 61 | + <Button |
| 62 | + design={colorBy === 'flux' ? 'Emphasized' : 'Default'} |
| 63 | + onClick={() => { |
| 64 | + onColorByChange('flux'); |
| 65 | + setColorPopoverOpen(false); |
| 66 | + }} |
| 67 | + > |
| 68 | + {t('Graphs.colorsFlux')} |
| 69 | + </Button> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + </Popover> |
| 73 | + <Button |
| 74 | + id={colorButtonId} |
| 75 | + design="Transparent" |
| 76 | + icon="palette" |
| 77 | + tooltip={t('Graphs.colorBy')} |
| 78 | + onClick={() => setColorPopoverOpen(!colorPopoverOpen)} |
| 79 | + /> |
| 80 | + </div> |
21 | 81 | </div> |
22 | 82 | ); |
23 | 83 | }; |
0 commit comments