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

Add Entry Total Count Component #36

Merged
merged 5 commits into from
Jan 24, 2025
Merged

Conversation

pixelastronauts
Copy link
Contributor

This PR adds a new entry count component to display the total number of entries matching the current filters in the Statamic Livewire Filters package. This feature improves user experience by providing immediate feedback about how many entries match their filter criteria.

I'd love to hear if this is something you're interested in adding to the package. If the approach and code look good to you, I'm happy to refactor anything needed and add proper documentation.

Overview

The component displays the total number of entries with proper pluralization support through Laravel's translation system. It updates reactively whenever filters are changed or search terms are modified.

Changes

  • Added new Livewire component LfCount for displaying entry counts
  • Created blade view component for rendering the count display
  • Added translation support for proper entry pluralization
  • Implemented event-based updates to maintain count accuracy
  • Added count dispatch in the main collection component

Implementation Details

New Components

  1. LfCount.php Livewire Component:

    • Handles receiving count updates via Livewire events
    • Supports customizable view rendering
    • Uses translation system for proper pluralization
  2. count.blade.php View:

    • Simple span-wrapped count display
    • Uses Laravel's trans_choice for proper pluralization
    • Minimal markup for easy styling customization

Translations

Added new translation key in resources/lang/en/ui.php:

'entries' => '{0} entries|{1} entry|[2,*] entries'

Event System

The count is updated through Livewire's event system:

  • Main collection component dispatches 'total-count-updated' event
  • Count component listens for updates using Livewire's #[On] attribute
  • Updates happen automatically when filters change

Usage

Add the count component to your template:

<livewire:lf-count />

The component will automatically receive updates whenever the entry count changes due to filtering or search operations.

Example Output

Examples of how the component displays different counts:

  • "0 entries"
  • "1 entry"
  • "42 entries"

@pixelastronauts
Copy link
Contributor Author

pixelastronauts commented Jan 4, 2025

Just wanted to add a note about localization: Since the count component relies on the translation string for proper pluralization of "entries", we should probably document how users can publish and customize the language files.

We could add this to the publishes array in the service provider:

$this->publishes([
    __DIR__ . '/../resources/lang' => resource_path('lang/vendor/statamic-livewire-filters'),
], 'statamic-livewire-filters-lang');

And then add a section in the documentation about customizing translations:

Customizing Translations

You can publish the language files to customize the translations:

php artisan vendor:publish --tag="statamic-livewire-filters-lang"

This will publish the language files to resources/lang/vendor/statamic-livewire-filters where you can modify the translations, including the entry count pluralization rules.

What do you think about adding this?

@afonic
Copy link
Contributor

afonic commented Jan 9, 2025

Thank you for this! I've run Laravel Pint in the default settings in order to keep consistent formatting.

I would like to add some tests before merging this and check if there is a more efficient way of doing this without requiring an extra Livewire component. Any specific reason you chose this implementation (eg. templating) instead of just using a public variable in the LivewireCollection component?

@pixelastronauts
Copy link
Contributor Author

I chose this implementation approach because it allows us to display the total count on the submit button within a filter modal. Here you can see an example:

Scherm­afbeelding 2025-01-09 om 11 03 35

Let me know if there is anything I can do in the meantime!

@afonic
Copy link
Contributor

afonic commented Jan 9, 2025

Thank you that makes sense! I'll check over the weekend why the tests are failing and add some more, if you want to try you're welcome. If you do, remember to run Laravel Pint before you commit.

@afonic afonic merged commit afbd4c8 into reachweb:master Jan 24, 2025
3 checks passed
@afonic
Copy link
Contributor

afonic commented Jan 25, 2025

I've made a few changes:

  • the tests where failing because it didn't work when the results where not paginated so that is fixed
  • I moved it at the entries-updated event in order to possibly add more meta data

I've also added the language file as you suggested, will update the docs soon.

Thanks for the help!

@pixelastronauts
Copy link
Contributor Author

Thanks for the update! Great to hear everything is working and that you've addressed the pagination edge case.

The entries-updated event placement makes sense for future metadata extensibility.

Glad I could help with this addition. If I think of any future additions or improvements, I'll submit them as pull requests. I'll be happy to assist with any future updates to the package.

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