This repository was archived by the owner on Sep 18, 2025. It is now read-only.
forked from nhsuk/nhsuk-frontend
-
Notifications
You must be signed in to change notification settings - Fork 2
Create TaskList View, ViewComponent and Model #41
Merged
akdalin-hee
merged 5 commits into
main
from
feature/TD-5215-Create-tasklist-view-component
May 1, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37308fb
Create TaskList View, ViewComponent and Model
akdalin-hee ffcdf6d
Parametrise URL
akdalin-hee b2a1c14
Correct typo
akdalin-hee 9245612
Simplify template structure
akdalin-hee 15db0b8
Replace URL with asp dynamically generated link path . Document code
akdalin-hee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
DotnetViewComponents/ViewComponents/TaskListViewComponent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace DotnetViewComponents.ViewComponents | ||
| { | ||
| using System.Collections.Generic; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using DotnetViewComponents.ViewModels; | ||
|
|
||
| public class TaskListViewComponent : ViewComponent | ||
| { | ||
| public IViewComponentResult Invoke( | ||
| IEnumerable<TaskListItemViewModel> listItems) | ||
| { | ||
| var model = new TaskListViewModel(listItems); | ||
|
|
||
| return View(model); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| namespace DotnetViewComponents.ViewModels | ||
| { | ||
| public class TaskListItemViewModel | ||
| { | ||
| /// <summary> | ||
| /// Creates a new instance of the <see cref="TaskListItemViewModel"/> class. | ||
| /// STATE: Choose between "Completed", "Incomplete" and "Cannot start yet". | ||
| /// STATESTYLE: The style should match the state. (e.g. "TaskStateStyle.INCOMPLETE" for "Incomplete", "TaskStateStyle.COMPLETED" for "Completed", "TaskStateStyle.UNSTARTED" for "Cannot start yet") | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier for the task.</param> | ||
| /// <param name="name">The name of the task.</param> | ||
| /// <param name="aspController">The asp-controller associated with the task's action.</param> | ||
| /// <param name="aspAction">The asp-action action associated with the task's action.</param> | ||
| /// <param name="aspRouteData">An asp-all-data-route dictionary containing route data for the task's action.</param> | ||
| /// <param name="state">Choose between "Completed", "Incomplete" and "Cannot start yet".</param> | ||
| /// <param name="stateStyle">The style should match the state. (e.g. "TaskStateStyle.INCOMPLETE" for "Incomplete", "TaskStateStyle.COMPLETED" for "Completed", "TaskStateStyle.UNSTARTED" for "Cannot start yet")</param> | ||
| /// <param name="hintText">Additional text providing hints about the task.</param> | ||
| public TaskListItemViewModel( | ||
| string id, | ||
| string name, | ||
| string aspController, | ||
| string aspAction, | ||
| Dictionary<string, string> aspRouteData, | ||
| string state, | ||
| TaskStateStyle stateStyle, | ||
| string hintText) | ||
| { | ||
| Id = id; | ||
| Name = name; | ||
| AspController = aspController; | ||
| AspAction = aspAction; | ||
| AspRouteData = aspRouteData; | ||
| State = state; | ||
| StateStyle = stateStyle; | ||
| HintText = hintText; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the unique identifier for the task. | ||
| /// </summary> | ||
| public string Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the name of the task. | ||
| /// </summary> | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the asp-controller of the link. | ||
| /// </summary> | ||
| public string AspController { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the as-action of the link. | ||
| /// </summary> | ||
| public string AspAction { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the asp-all-route-data of the link. | ||
| /// </summary> | ||
| public Dictionary<string, string> AspRouteData { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the current state of the task. | ||
| /// </summary> | ||
| public string State { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the style that matches the state of the task. | ||
| /// </summary> | ||
| public TaskStateStyle StateStyle { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets additional text providing hints about the task. | ||
| /// </summary> | ||
| public string HintText { get; set; } | ||
| } | ||
|
|
||
|
|
||
| public enum TaskStateStyle | ||
| { | ||
| COMPLETED, | ||
| INCOMPLETE, | ||
| UNSTARTED | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| namespace DotnetViewComponents.ViewModels | ||
| { | ||
| public class TaskListViewModel | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TaskListViewModel"/> class. | ||
| /// </summary> | ||
| /// <param name="listItems">A collection of task list items.</param> | ||
| public TaskListViewModel( | ||
| IEnumerable<TaskListItemViewModel> listItems) | ||
| { | ||
| ListItems = listItems; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the collection of task list items. | ||
| /// </summary> | ||
| public IEnumerable<TaskListItemViewModel> ListItems { get; set; } | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| @using DotnetViewComponents.ViewModels | ||
| @model TaskListViewModel | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest simplifying the Razor markup by setting up some variables: |
||
| <ul class="nhsuk-task-list"> | ||
| @foreach (var task in Model.ListItems) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we parameterise some of the properties we can simplify this (to avoid repetition) markup to something like: |
||
| { | ||
| var hasLink = task.StateStyle != TaskStateStyle.UNSTARTED; | ||
| var statusClass = task.StateStyle switch | ||
| { | ||
| TaskStateStyle.COMPLETED => "nhsuk-task-list__status nhsuk-task-list__status--completed", | ||
| TaskStateStyle.INCOMPLETE => "nhsuk-task-list__status", | ||
| TaskStateStyle.UNSTARTED => "nhsuk-task-list__status nhsuk-task-list__status--cannot-start-yet", | ||
| _ => "nhsuk-task-list__status" | ||
| }; | ||
| var statusId = task.StateStyle == TaskStateStyle.UNSTARTED | ||
| ? $"task-list-{task.Id}-status" | ||
| : task.Id; | ||
|
|
||
|
|
||
| <li class="nhsuk-task-list__item @(hasLink ? "nhsuk-task-list__item--with-link" : "")"> | ||
| <div class="nhsuk-task-list__name-and-hint"> | ||
| @if (hasLink) | ||
| { | ||
| <a class="nhsuk-link nhsuk-task-list__link" asp-controller="@task.AspController" asp-action="@task.AspAction" asp-all-route-data="@task.AspRouteData" aria-describedby="@statusId"> | ||
| @task.Name | ||
| </a> | ||
| } | ||
| else | ||
| { | ||
| <div>@task.Name</div> | ||
| } | ||
| @if (!String.IsNullOrWhiteSpace(task.HintText)) | ||
| { | ||
| <div id="@statusId" class="nhsuk-task-list__hint">@task.HintText</div> | ||
| } | ||
| </div> | ||
| <div class="@statusClass" id="@statusId"> | ||
| @if (task.StateStyle == TaskStateStyle.INCOMPLETE) | ||
| { | ||
| <strong class="nhsuk-tag nhsuk-tag--blue">@task.State</strong> | ||
| } | ||
| else | ||
| { | ||
| @task.State | ||
| } | ||
| </div> | ||
| </li> | ||
| } | ||
| </ul> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a cannot start yet state too:

This is rendered in a div of
class="nhsuk-task-list__status nhsuk-task-list__status--cannot-start-yet"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see in the Razor partial that this is covered by "UNSTARTED". It still might be worth checking with Allan that we don't need more flexibility in terms of the styling of tags inside the div.