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

Improve Repeater's grid() method to accept closure that can be evaluated later #14553

Closed

Conversation

tjodalv
Copy link
Contributor

@tjodalv tjodalv commented Oct 17, 2024

Description

The grid() method of the Repeater component already accepts arrays, integers, or strings. This PR introduces support for using closures that can be evaluated later. If the value 'auto' is provided to the grid() method, a closure will be registered to automatically return the number of items in the Repeater. Additionally, users can provide their own custom closure to calculate the grid size based on specific logic.

Functional changes

  • Code style has been fixed by running the composer cs command.
  • Changes have been tested to not break existing functionality.
  • Documentation is up-to-date.

@niladam
Copy link

niladam commented Oct 20, 2024

I think this is a great addition.

I was already looking to this, in order to dynamically set the grid based on some other key.

This allows for doing stuff like this:

TextInput::make('products_number')
    ->label(__('Number of products'))
    ->reactive()
    ->numeric(),

Repeater::make('visible_products')
    ->reactive()
    ->schema([
        Select::make('product_id')
            ->label(__('Choose a product'))
            ->searchable()
            ->options(fn() => Product::active()->pluck('title', 'id')),
    ])
    ->usesPosition()
    ->hiddenLabel()
    ->grid(fn(Get $get) => $get('products_number') ?? 4)

Good job @tjodalv and thank you!

@danharrin danharrin added the enhancement New feature or request label Oct 21, 2024
@danharrin danharrin added this to the v3 milestone Oct 21, 2024
@danharrin
Copy link
Member

Hi, apologies but we are no longer accepting new features for v3 as per #13744. Also, please check out our contributing guide which talks about proposing the feature before submitting the PR, to ensure you aren't spending time building something that we won't merge.

I would accept a PR for this feature when v4 is released, if you are still interested, with the following changes:

  • Not sure how I feel about auto at the moment, feels a too bit magic and probably could be accomplished with ->grid(fn (array $state): int => count($state)). Let's not include that
  • The logic will need to be fixed so that when calling ->grid() multiple times, it will merge onto the array instead of replacing the array, as per the original behaviour that you have removed.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants