import { Children } from 'react';
import ConditionalWrapper from '../../common/ConditionalWrapper';
import HintText from '../../common/HintText';
import ScreenReaderText from '../../common/ScreenReaderText';
import Tag from '../Tag';
import { TaskListGroupProps, TaskListItemProps, TaskListProps } from './types';
import clsx from 'clsx';
const TaskItem = ({
children,
className,
href,
id,
isComplete = false,
linkComponent,
statusText,
tagColour = 'grey',
title,
...props
}: TaskListItemProps) => {
if (isComplete) {
tagColour = 'green';
statusText = statusText || 'Completed'
}
const LINK_CLASS = 'ds_task-list__task-link';
function getLinkElement(children: React.ReactNode) {
let linkElement;
if (linkComponent) {
linkElement = linkComponent({ className: LINK_CLASS, href, children });
} else {
linkElement = {children};
}
return linkElement as React.JSX.Element;
}
return (
getLinkElement(children)}
>
{title}
{statusText && ({statusText})}
{children}
{typeof statusText !== 'undefined' &&
{statusText}
}
);
};
/**
* @param {Object} props
* @param {string} props.intro - Intro text
* @param {string} props.title - The title of the task group
* @returns {JSX.Element} - The element
*/
const TaskGroup = ({
children,
className,
intro,
title,
...props
}: TaskListGroupProps) => {
return (
{title}
{intro && {intro}
}
);
};
const TaskList = ({
children,
className,
headingId = 'task-list',
title,
...props
}: TaskListProps) => {
let taskCount = 0;
const incompleteTaskIds: string[] = [];
let completedTasksCount = 0;
function processChild(item: React.JSX.Element) {
if (item.type.displayName === 'TaskList.Item') {
taskCount = taskCount + 1;
if (item.props.isComplete) {
completedTasksCount = completedTasksCount + 1;
} else {
incompleteTaskIds.push(item.props.id);
}
} else if (item.type.displayName === 'TaskList.Group') {
Children.forEach(item.props.children, child => {
processChild(child);
});
}
}
function firstIncompleteTaskLink() {
if (incompleteTaskIds.length > 0) {
return (
Skip to first incomplete section
)
}
}
Children.forEach(children, child => {
processChild(child as React.JSX.Element);
});
return (
{title}
);
};
TaskList.displayName = 'TaskList';
TaskItem.displayName = 'TaskList.Item';
TaskGroup.displayName = 'TaskList.Group';
TaskList.Item = TaskItem;
TaskList.Group = TaskGroup;
export default TaskList;