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: implement sorting and searching in Table component #1425

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aarondoet
Copy link
Contributor

@aarondoet aarondoet commented Sep 1, 2024

Replaces #1423

📑 Description

In #1423 I gave it a try to implement sorting within the Table component. I thought that searching would be fairly easy with that setup, when the table already has the raw data itself. So I tried to make a table that can easily be both searched and sorted, this is my current attempt.

Status

  • Not Completed
  • Completed

✅ Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • I have checked the page with https://validator.unl.edu/
  • All the tests have passed
  • My pull request is based on the latest commit (not the npm version).

Additional Information

I would deprecate the TableSearch component with this but don't know if/how that is possible. I don't want to completely delete it yet for backwards compatibility.

Summary by CodeRabbit

  • New Features

    • Introduced dynamic filtering and sorting capabilities in the table component.
    • Added customizable properties for search input and table styling.
    • Enhanced row rendering flexibility with new slots in the table body.
  • Bug Fixes

    • Improved context handling for items and filters in child components.
  • Documentation

    • Updated documentation to reflect new props and functionalities for the table, table head cell, and table body components.

Copy link

vercel bot commented Sep 1, 2024

@aarondoet is attempting to deploy a commit to the Themesberg Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

coderabbitai bot commented Sep 1, 2024

Walkthrough

The changes introduce enhancements to the Table, TableBody, and TableHeadCell components by implementing generics for improved type handling, adding new properties for filtering and sorting functionalities, and streamlining the search and sorting processes. The updates also include modifications to JSON configurations, allowing for greater customization and flexibility in rendering table elements and managing data interactions.

Changes

Files Change Summary
src/lib/table/Table.svelte Added generics, new properties for items, filter, and customization options for search input. Introduced state management for search functionality.
src/lib/table/TableBody.svelte Implemented generics, enhanced context retrieval, and added reactive statements for filtered and sorted items. Updated rendering logic for rows.
src/lib/table/TableHeadCell.svelte Introduced generics, added sorting functionality with new properties for sort direction and default behavior. Enhanced rendering for sorting indicators.
src/routes/component-data/Table.json Expanded slots and props for enhanced functionality, including search and styling options.
src/routes/component-data/TableBody.json Added a new slot for dynamic row rendering.
src/routes/component-data/TableHeadCell.json Expanded props for sorting capabilities, including sort function and direction.
src/routes/docs/components/table.md Updated documentation to reflect changes in filtering and sorting functionality, simplifying the structure and enhancing row customization.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Table
    participant TableBody
    participant TableHeadCell

    User->>Table: Input search term
    Table->>TableBody: Update searchTerm
    TableBody->>TableBody: Filter items based on searchTerm
    TableBody->>TableHeadCell: Request sorting function
    TableHeadCell->>TableBody: Provide sorting logic
    TableBody->>User: Display filtered and sorted items
Loading

🐇 "Oh, what a delight, my table's now bright,
With filters and sorts, it dances in light.
Rows that can change, oh what a fun game,
Customizable magic, it’s never the same!
Hops of joy, I must say,
My table's a treasure, hip-hip-hooray!" 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@aarondoet aarondoet mentioned this pull request Sep 1, 2024
8 tasks
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (1)
src/routes/component-data/Table.json (1)

1-1: LGTM! The new props enhance the component's functionality and customization options.

The expansion of the props array in the Table component's configuration introduces several valuable improvements:

  1. The "items" prop of type T[] enables the component to handle generic data, enhancing its type safety and reusability across different data types.
  2. The "filter" prop allows for custom filtering logic to be passed to the component, providing flexibility in how the data is filtered based on the search term.
  3. The "placeholder" prop enables customization of the search input's placeholder text, improving the user experience.
  4. The various class props offer fine-grained control over the styling of different elements within the component, allowing for better visual customization.

The default values provided for the props are sensible and maintain a consistent pattern.

To further improve the configuration, consider adding JSDoc comments to document the purpose and expected values for each prop. This will enhance the developer experience and make it easier for others to understand and use the component correctly.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5a6a801 and f4cc9a2.

Files selected for processing (7)
  • src/lib/table/Table.svelte (4 hunks)
  • src/lib/table/TableBody.svelte (1 hunks)
  • src/lib/table/TableHeadCell.svelte (1 hunks)
  • src/routes/component-data/Table.json (1 hunks)
  • src/routes/component-data/TableBody.json (1 hunks)
  • src/routes/component-data/TableHeadCell.json (1 hunks)
  • src/routes/docs/components/table.md (2 hunks)
Additional comments not posted (23)
src/routes/component-data/TableBody.json (1)

1-1: LGTM! The addition of the "row" slot enhances the TableBody component.

The code change is approved. The addition of the "row" slot to the TableBody component definition is a valid enhancement that allows for the inclusion of row elements within the component. This change expands the component's capabilities by enabling the rendering of rows dynamically while preserving its existing structure and properties, ensuring backward compatibility.

src/routes/component-data/TableHeadCell.json (1)

1-1: LGTM! But consider adding a type constraint and verify the usage of the sort property.

The new properties sort, defaultDirection, defaultSort, and direction enhance the component's capabilities for handling sorting behavior, making it more versatile in data presentation contexts. The changes are well-structured and provide clear default values.

However, please consider adding a type constraint to the generic type T to ensure it's comparable. This will help catch type errors at compile time.

Apply this diff to add the type constraint:

-["sort","((a: T, b: T) => number) | null = null;",""]
+["sort","((a: T extends Comparable, b: T extends Comparable) => number) | null = null;",""]

Also, ensure that the sorting function passed to the sort property is properly typed and handles the generic type T correctly.

Run the following script to verify the usage of the sort property:

src/routes/component-data/Table.json (1)

1-1: LGTM! The new slots enhance the component's extensibility.

The addition of the "search", "svgSearch", and "header" slots to the Table component's configuration is a positive change that improves the component's extensibility and flexibility. The naming of the slots is clear and follows a consistent pattern, making it easy for developers to understand their purpose.

src/lib/table/TableBody.svelte (4)

1-1: LGTM!

The addition of the generics attribute with a type parameter T is a great way to make the component more flexible and reusable by allowing it to work with various data types.


7-11: LGTM!

The enhanced context retrieval and reactive statements for filtering and sorting items are well-implemented and improve the component's capability to manage and display data effectively. The checks for defined filter and sorter functions ensure proper handling of optional functionalities.


17-19: LGTM!

Iterating over the sorted array for rendering table rows is a great way to dynamically generate rows based on the sorted data. This change enhances the component's capability to display data more effectively, taking into account the applied sorting criteria.


Line range hint 23-27: LGTM!

The documentation comment is helpful for understanding the component's usage and available props. It is up-to-date with the current implementation.

src/lib/table/TableHeadCell.svelte (5)

1-3: LGTM!

The code changes are approved. The use of generics and imports are correctly implemented.


7-10: LGTM!

The code changes are approved. The exported properties for sorting functionality are correctly implemented.


12-14: LGTM!

The code changes are approved. The retrieval of the sorter context and the reactive update of the direction are correctly implemented.


16-29: LGTM!

The code changes are approved. The sortItems function and the logic for updating the sorter context are correctly implemented.


32-42: LGTM!

The code changes are approved. The component's rendering logic and the conditional display of the sorting button are correctly implemented.

src/lib/table/Table.svelte (6)

1-1: LGTM!

The introduction of the generic type parameter T is a good practice for making the component more reusable.


14-15: LGTM!

The new properties items and filter are correctly typed using the generic type parameter T. This allows for passing data to the table component and enables dynamic filtering of the items based on user input.


16-21: LGTM!

The new properties for customizing the search input and its associated elements provide flexibility and follow the project's coding style.


23-25: LGTM!

The searchTerm variable is necessary for managing the state of the search input. Using twMerge for merging Tailwind CSS classes is a good practice.


43-50: LGTM!

Sharing items, searchTerm, filter, and sorter using the context API enhances the component's interactivity and responsiveness to user input. Using writable stores for searchTerm, filter, and sorter facilitates reactivity in the component.


54-71: LGTM!

Conditional rendering of the search input and its associated elements using Svelte's {#if} block ensures that the search functionality is only displayed if a filter function is provided. This improves performance by not rendering unnecessary elements.

src/routes/docs/components/table.md (5)

255-264: LGTM!

The code changes are approved.


272-277: LGTM!

The code changes are approved. Using the slot="row" and let:item attributes in TableBodyRow allows for flexible rendering of table rows based on the items data.


297-303: LGTM!

The code changes are approved. The sort prop allows specifying custom sorting logic for each column. The defaultSort and defaultDirection props enable sorting by default on specific columns.


306-314: LGTM!

The code changes are approved. Using the slot="row" and let:item attributes in TableBodyRow allows for flexible rendering of table rows based on the items data. The anchor tag in the last TableBodyCell provides an action to buy the item.


915-915: LGTM!

The code changes are approved. The GitHubCompoLinks component is likely used to display links to the component's GitHub repository.

{"name":"TableHeadCell","slots":[],"events":["on:click","on:focus","on:keydown","on:keypress","on:keyup","on:mouseenter","on:mouseleave","on:mouseover","on:click","on:focus","on:keydown","on:keypress","on:keyup","on:mouseenter","on:mouseleave","on:mouseover"],"props":[["padding","string","'px-6 py-3'"],["sort","((a: T, b: T) => number) | null = null;",""],["defaultDirection","'asc' | 'desc'","'asc'"],["defaultSort","boolean","false"],["direction","'asc' | 'desc' | null","defaultSort ? defaultDirection : null"]]}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the duplicated events in the events array.

The events array contains the same set of events twice:

"events":["on:click","on:focus","on:keydown","on:keypress","on:keyup","on:mouseenter","on:mouseleave","on:mouseover","on:click","on:focus","on:keydown","on:keypress","on:keyup","on:mouseenter","on:mouseleave","on:mouseover"]

This duplication is redundant and can lead to confusion. Please remove one set of the duplicated events.

Apply this diff to remove the duplicated events:

-"events":["on:click","on:focus","on:keydown","on:keypress","on:keyup","on:mouseenter","on:mouseleave","on:mouseover","on:click","on:focus","on:keydown","on:keypress","on:keyup","on:mouseenter","on:mouseleave","on:mouseover"]
+"events":["on:click","on:focus","on:keydown","on:keypress","on:keyup","on:mouseenter","on:mouseleave","on:mouseover"]

Copy link

vercel bot commented Sep 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
flowbite-svelte ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 2, 2024 9:57am

@shinokada
Copy link
Collaborator

As far as I remember, you don't need generics="T" attribute in script tag in Svelte 4. Please remove it.

@aarondoet
Copy link
Contributor Author

As far as I remember, you don't need generics="T" attribute in script tag in Svelte 4. Please remove it.

I do get errors that T is not defined when removing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants