Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pie: support multiple active ids #2667

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/pie/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
CommonPieProps,
} from './types'

const idIsActive = (id, activeId) =>

Check failure on line 23 in packages/pie/src/hooks.ts

View workflow job for this annotation

GitHub Actions / build

Parameter 'id' implicitly has an 'any' type.

Check failure on line 23 in packages/pie/src/hooks.ts

View workflow job for this annotation

GitHub Actions / build

Parameter 'activeId' implicitly has an 'any' type.

Check failure on line 23 in packages/pie/src/hooks.ts

View workflow job for this annotation

GitHub Actions / end-to-end-tests

Parameter 'id' implicitly has an 'any' type.

Check failure on line 23 in packages/pie/src/hooks.ts

View workflow job for this annotation

GitHub Actions / end-to-end-tests

Parameter 'activeId' implicitly has an 'any' type.
Array.isArray(activeId) ? activeId.indexOf(id) > -1 : activeId === id

/**
* Format data so that we get a consistent data structure.
* It will also add the `formattedValue` and `color` property.
Expand Down Expand Up @@ -138,12 +141,10 @@
index: arc.index,
startAngle: arc.startAngle,
endAngle: arc.endAngle,
innerRadius:
activeId === arc.data.id
innerRadius: idIsActive(arc.data.id, activeId)
? innerRadius - activeInnerRadiusOffset
: innerRadius,
outerRadius:
activeId === arc.data.id
outerRadius: idIsActive(arc.data.id, activeId)
? outerRadius + activeOuterRadiusOffset
: outerRadius,
thickness: outerRadius - innerRadius,
Expand Down
70 changes: 70 additions & 0 deletions storybook/stories/pie/Pie.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,76 @@ export const ControlledActiveId: Story = {
render: () => <ControlledPies />,
}

const ControlledPiesMultipleActiveIds = () => {
const [activeId, setActiveId] = useState<string>(commonProperties.data[1].id)
const [additionalActiveIds, setAdditionalActiveIds] = useState<string>([])
function addActiveId(id) {
setAdditionalActiveIds(additionalActiveIds.concat(id))
}
function removeActiveId(id) {
setAdditionalActiveIds(additionalActiveIds.filter(x => x !== id))
}
function handleCheckboxChange(id, checked) {
if (checked) addActiveId(id)
if (!checked) removeActiveId(id)
}
function handlePieClick(id) {
if (additionalActiveIds.indexOf(id) > -1) {
removeActiveId(id)
} else {
addActiveId(id)
}
}
const allActiveIds = [activeId, ...additionalActiveIds]
return (
<div>
<div
style={{
marginBottom: '10px',
}}
>
<h3>Active IDs:</h3>
{commonProperties.data.map(x => (
<div key={x.id}>
<label>
<input
type="checkbox"
checked={additionalActiveIds.indexOf(x.id) > -1}
onChange={evt => handleCheckboxChange(x.id, evt.target.checked)}
/>
{x.id}
</label>
</div>
))}
</div>
<div
style={{
width: '800px',
display: 'grid',
gridTemplateColumns: 'repeat(2, 1fr)',
}}
>
<Pie
{...controlledPieProps}
activeId={allActiveIds}
onActiveIdChange={setActiveId}
onClick={slice => handlePieClick(slice.id)}
/>
<Pie
{...controlledPieProps}
activeId={allActiveIds}
onActiveIdChange={setActiveId}
onClick={slice => handlePieClick(slice.id)}
/>
</div>
</div>
)
}

export const ControlledMultipleActiveIds: Story = {
render: () => <ControlledPiesMultipleActiveIds />,
}

const PieWithCustomLegend = () => {
const [customLegends, setCustomLegends] = useState<LegendDatum<SampleDatum>[]>([])

Expand Down
Loading