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

feat: allow use of any tag in listItems helper #3147

Merged
merged 5 commits into from
Nov 8, 2021
Merged
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
48 changes: 35 additions & 13 deletions js/src/common/helpers/listItems.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type Mithril from 'mithril';
import Separator from '../components/Separator';
import classList from '../utils/classList';
import type * as Component from '../Component';

function isSeparator(item): boolean {
function isSeparator(item: Mithril.Children): boolean {
return item.tag === Separator;
}

function withoutUnnecessarySeparators(items: Array<Mithril.Vnode>): Array<Mithril.Vnode> {
const newItems = [];
let prevItem;
function withoutUnnecessarySeparators(items: Mithril.Children): Mithril.Children {
const newItems: Mithril.Children = [];
let prevItem: Mithril.Child;

items.filter(Boolean).forEach((item: Mithril.Vnode, i: number) => {
if (!isSeparator(item) || (prevItem && !isSeparator(prevItem) && i !== items.length - 1)) {
Expand All @@ -20,17 +21,36 @@ function withoutUnnecessarySeparators(items: Array<Mithril.Vnode>): Array<Mithri
return newItems;
}

export interface ModdedVnodeAttrs {
itemClassName?: string;
key?: string;
}

export type ModdedVnode<Attrs> = Mithril.Vnode<ModdedVnodeAttrs, Component.default<Attrs> | {}> & {
itemName?: string;
itemClassName?: string;
};

/**
* The `listItems` helper wraps a collection of components in <li> tags,
* The `listItems` helper wraps an array of components in the provided tag,
* stripping out any unnecessary `Separator` components.
*
* By default, this tag is an `<li>` tag, but this is customisable through the
* second function parameter, `customTag`.
*/
export default function listItems(items: Mithril.Vnode | Array<Mithril.Vnode>): Array<Mithril.Vnode> {
export default function listItems<Attrs extends Record<string, unknown>>(
items: ModdedVnode<Attrs> | ModdedVnode<Attrs>[],
customTag: string | Component.default<Attrs> = 'li',
attributes: Attrs = {}
): Mithril.Vnode[] {
if (!(items instanceof Array)) items = [items];

return withoutUnnecessarySeparators(items).map((item: Mithril.Vnode) => {
const isListItem = item.tag && item.tag.isListItem;
const active = item.tag && item.tag.isActive && item.tag.isActive(item.attrs);
const className = (item.attrs && item.attrs.itemClassName) || item.itemClassName;
const Tag = customTag;

return withoutUnnecessarySeparators(items).map((item: ModdedVnode<Attrs>) => {
const isListItem = item.tag?.isListItem;
const active = item.tag?.isActive?.(item.attrs);
const className = item.attrs?.itemClassName || item.itemClassName;

if (isListItem) {
item.attrs = item.attrs || {};
Expand All @@ -41,12 +61,14 @@ export default function listItems(items: Mithril.Vnode | Array<Mithril.Vnode>):
const node: Mithril.Vnode = isListItem ? (
item
) : (
<li
// @ts-expect-error `Component` does not have any construct or call signatures
<Tag
className={classList([className, item.itemName && `item-${item.itemName}`, active && 'active'])}
key={(item.attrs && item.attrs.key) || item.itemName}
key={item?.attrs?.key || item.itemName}
{...attributes}
>
{item}
</li>
</Tag>
);

return node;
Expand Down