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: New buttons and activeActions approach #1121

Merged
merged 1 commit into from
Jul 25, 2024

Conversation

lee-to
Copy link
Collaborator

@lee-to lee-to commented Jul 25, 2024

ListOf

New approach to adding and changing buttons and active actions

Motivation

Let's say if we need to change the set of active actions, then we override the method and at the same time list all the values, we need to copy them from the parent method and in the future also constantly think about what they are called

public function getActiveActions(): array
{
    return ['view', 'delete', 'massDelete', 'update', 'create'];
}

There is a better example when we need to change the set of index buttons, simply remove one of the buttons, and we will need to display all the others

public function getIndexButtons(): array
{
    return [EditButton::make(), DetailButton::make(), ...];
}

Now

Now we can work with the ListOf object!

Exclude elements, add them to the beginning or end and not think about what set we have there now

Examples

public function indexButtons(): ListOf
{
    return parent::indexButtons()->prepend(
        ActionButton::make('#', '/endpoint?ids[]=1')->bulk(),

        ActionButton::make('Restore')
            ->method('restore', events: [$this->getListEventName()])
            ->canSee(fn(Article $model) => $model->trashed()),

        ActionButton::make('Force delete')
            ->method('forceDelete', events: [$this->getListEventName()])
            ->canSee(fn(Article $model) => $model->trashed()),
    );
}
public function activeActions(): ListOf
{
    return parent::activeActions()->except(Action::VIEW);
}
public function indexButtons(): ListOf
{
    return parent::indexButtons()->add(
        ActionButton::make('#', '/'),
    );
}
public function indexButtons(): ListOf
{
    return parent::indexButtons()->except(fn(ActionButton $btn) => $btn->getName() === 'detail-button');
}

@lee-to lee-to merged commit cf45006 into moonshine-software:3.x Jul 25, 2024
2 checks passed
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.

1 participant