-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components):Add Legend component
- Loading branch information
1 parent
9fe7333
commit 3f45237
Showing
5 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/graphin-components/src/components/legend/__tests__/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import Graphin from '@antv/graphin/src/Graphin'; | ||
import { render, act, fireEvent, cleanup } from '@testing-library/react'; | ||
|
||
import '@testing-library/jest-dom/extend-expect'; | ||
|
||
import Legend, { LegendProps, LegendOption } from '../index'; | ||
|
||
describe('<Legend />', () => { | ||
it('You shall pass', () => { | ||
const data = { | ||
nodes: [], | ||
edges: [], | ||
}; | ||
const layout = { | ||
name: 'force', | ||
}; | ||
const options: LegendOption[] = [ | ||
{ | ||
label: '人群', | ||
value: 'person', | ||
color: 'red', | ||
}, | ||
{ | ||
label: '公司', | ||
value: 'company', | ||
color: 'blue', | ||
}, | ||
]; | ||
|
||
const { asFragment, queryAllByTestId } = render( | ||
<Graphin data={data} layout={layout}> | ||
<Legend options={options} onChange={() => {}} /> | ||
</Graphin>, | ||
); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/graphin-components/src/components/legend/index.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
ul.graphin-components-legend { | ||
display: block; | ||
position: relative; | ||
margin: 0px; | ||
padding: 0px; | ||
li.item { | ||
display: inline-block; | ||
vertical-align: top; | ||
margin: 5px 10px; | ||
cursor: pointer; | ||
& > .dot { | ||
width: 10px; | ||
height: 10px; | ||
border-radius: 50%; | ||
display: inline-block; | ||
} | ||
& > .label { | ||
padding: 0px 5px; | ||
font-size: 12px; | ||
display: inline-block; | ||
vertical-align: baseline; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/graphin-components/src/components/legend/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import './index.less'; | ||
|
||
export interface LegendOption { | ||
/** 标签 */ | ||
label: string; | ||
/** 颜色 */ | ||
color: string; | ||
/** 值 */ | ||
value: string; | ||
/** 是否选中 */ | ||
checked?: boolean; | ||
} | ||
export interface LegendProps { | ||
options: LegendOption[]; | ||
onChange?: (checked: LegendOption, newOptions: LegendOption[], props: any) => any; | ||
} | ||
|
||
const Legend: React.FunctionComponent<LegendProps> = props => { | ||
const { onChange = () => {} } = props; | ||
const mergedOptions = props.options.map(c => { | ||
const { checked } = c; | ||
return { | ||
...c, | ||
checked: typeof checked === 'boolean' ? checked : true, | ||
}; | ||
}); | ||
const [options, setOptions] = React.useState(mergedOptions); | ||
|
||
const handleClick = (option: LegendOption) => { | ||
const checkedValue = { ...option, checked: !option.checked }; | ||
const result = options.map(c => { | ||
const matched = c.value === option.value; | ||
return matched ? checkedValue : c; | ||
}); | ||
setOptions(result); | ||
onChange(checkedValue, result, props); | ||
}; | ||
|
||
return ( | ||
<ul className="graphin-components-legend"> | ||
{options.map((option: LegendOption) => { | ||
const { label, checked, color } = option; | ||
return ( | ||
<li | ||
key={option.value} | ||
onClick={() => { | ||
handleClick(option); | ||
}} | ||
className="item" | ||
> | ||
<span className="dot" style={{ background: checked ? color : '#ddd' }}></span> | ||
<span className="label" style={{ color: checked ? '#000000d9' : '#ddd' }}> | ||
{label} | ||
</span> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
); | ||
}; | ||
|
||
export default Legend; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as Toolbar, ToolbarProps, RenderProps } from './components/toolbar'; | ||
export { default as MiniMap } from './components/mini-map'; | ||
export { default as ContextMenu } from './components/context-menu'; | ||
export { default as Legend, LegendProps } from './components/legend'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters