Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karolisgrinkevicius-home24/75e1e6ebaee6824f028b47c23d9dfcee to your computer and use it in GitHub Desktop.
Save karolisgrinkevicius-home24/75e1e6ebaee6824f028b47c23d9dfcee to your computer and use it in GitHub Desktop.
import React from 'react';
import { map, groupBy } from 'lodash';
interface SelectableOption {
key: string;
items: unknown[];
}
interface WithGroupedAttributes {
selectableOptions: SelectableOption[];
}
const withGroupedAttributes = <P extends object>(
Component: React.ComponentType<P>
): React.FC<P> => ({
selectableOptions,
...restProps
}: P & WithGroupedAttributes) => (
<Component
{...({
...restProps,
selectableOptions: map(groupBy(selectableOptions, 'key'), (obj, key) => ({
key,
items: obj,
})),
} as P)}
/>
);
export default withGroupedAttributes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment