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

fix(xychart): support fragment and array type children of stacks/groups #1259

Merged
merged 2 commits into from
Jul 7, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useContext, useMemo, useEffect, useCallback } from 'react';
import { PositionScale } from '@visx/shape/lib/types';
import { scaleBand } from '@visx/scale';
import isChildWithProps from '../../../typeguards/isChildWithProps';
import { BaseBarSeriesProps } from './BaseBarSeries';
import {
Bar,
Expand All @@ -18,6 +17,7 @@ import isValidNumber from '../../../typeguards/isValidNumber';
import { BARGROUP_EVENT_SOURCE, XYCHART_EVENT_SOURCE } from '../../../constants';
import useSeriesEvents from '../../../hooks/useSeriesEvents';
import findNearestGroupDatum from '../../../utils/findNearestGroupDatum';
import getChildrenAndGrandchildrenWithProps from '../../../utils/getChildrenAndGrandchildrenWithProps';

export type BaseBarGroupProps<
XScale extends PositionScale,
Expand Down Expand Up @@ -64,12 +64,9 @@ export default function BaseBarGroup<
} = (useContext(DataContext) as unknown) as DataContextType<XScale, YScale, Datum>;

const barSeriesChildren = useMemo(
() =>
React.Children.toArray(children).filter(child =>
isChildWithProps<BaseBarSeriesProps<XScale, YScale, Datum>>(child),
),
() => getChildrenAndGrandchildrenWithProps<BaseBarSeriesProps<XScale, YScale, Datum>>(children),
[children],
) as React.ReactElement<BaseBarSeriesProps<XScale, YScale, Datum>>[];
);

// extract data keys from child series
const dataKeys: string[] = useMemo(
Expand Down
8 changes: 4 additions & 4 deletions packages/visx-xychart/src/hooks/useStackedData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useMemo } from 'react';
import { useContext, useEffect, useMemo } from 'react';
import { SeriesPoint, stack as d3stack } from 'd3-shape';
import stackOffset from '@visx/shape/lib/util/stackOffset';
import stackOrder from '@visx/shape/lib/util/stackOrder';
Expand All @@ -9,7 +9,7 @@ import DataContext from '../context/DataContext';
import { CombinedStackData, DataContextType, SeriesProps } from '../types';
import getBarStackRegistryData from '../utils/getBarStackRegistryData';
import combineBarStackData from '../utils/combineBarStackData';
import isChildWithProps from '../typeguards/isChildWithProps';
import getChildrenAndGrandchildrenWithProps from '../utils/getChildrenAndGrandchildrenWithProps';

type UseStackedData<Datum extends object> = {
children: JSX.Element | JSX.Element[];
Expand All @@ -30,9 +30,9 @@ export default function useStackedData<
// find series children
// @TODO: memoization doesn't work well if at all for this
const seriesChildren = useMemo(
() => React.Children.toArray(children).filter(child => isChildWithProps<ChildrenProps>(child)),
() => getChildrenAndGrandchildrenWithProps<ChildrenProps>(children),
[children],
) as React.ReactElement<ChildrenProps>[];
);

// extract data keys from child series
const dataKeys: string[] = useMemo(
Expand Down
8 changes: 0 additions & 8 deletions packages/visx-xychart/src/typeguards/isChildWithProps.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

/** Returns whether the React.ReactNode has props (and therefore is an `Element` versus primitive type) */
function isChildWithProps<P extends object>(
child: React.ReactNode,
): child is React.ComponentType<P> {
return !!child && typeof child === 'object' && 'props' in child && child.props != null;
}

/**
* Returns children and grandchildren of type React.ReactNode.
* Flattens children one level to support React.Fragments and Array type children.
*/
export default function getChildrenAndGrandchildrenWithProps<P extends object>(
children: JSX.Element | JSX.Element[],
): React.ReactElement<P>[] {
return React.Children.toArray(children)
.flatMap(child => {
if (typeof child === 'object' && 'props' in child && child.props.children) {
return child.props.children;
}
return child;
})
.filter(child => isChildWithProps<P>(child));
}