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(BelongsToMany): add ability to set custom color for badge in inLine format #1090

Merged
merged 6 commits into from
Jul 8, 2024

Conversation

carma100
Copy link

@carma100 carma100 commented Jul 3, 2024

Для поля BelongsToMany в сахар ->inline() добавил возможность в качестве badge передавать компонент Badge.

image

Теперь при наличии нескольких полей BelongsToMany на индексной странице есть возможность указать цвет, отличный от primary.

@lee-to lee-to requested a review from DissNik July 4, 2024 13:52
Copy link
Member

@DissNik DissNik left a comment

Choose a reason for hiding this comment

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

Может быть есть смысл сделать цвет через замыкание? Тогда мы сможем изменять цвет каждого badge. Или сам badge через замыкание, чтоб не было много параметров

@lee-to
Copy link
Collaborator

lee-to commented Jul 5, 2024

Еще как вариант передавать компонент Badge (тоже через closure) но не уверен

@carma100
Copy link
Author

carma100 commented Jul 5, 2024

@lee-to, @DissNik спасибо за идеи. Попробую сделать, пр обновлю по возможости.

…pe of the $badge variable and added support for the Badge component
@carma100
Copy link
Author

carma100 commented Jul 6, 2024

@DissNik убрал параметр, добавил замыкание и возможность передачи компонента Badge, сохранил логику работы при badge: true.
В качестве примера использовал:

BelongsToMany::make('Label name', 'relationName', formatted: 'name', resource: new TestResource())
    ->inline(" ", badge: function($model, $value) {
        return Badge::make($value, 'green');
    }),

@@ -70,7 +70,7 @@ class BelongsToMany extends ModelRelationField implements

protected string $inLineSeparator = '';

protected bool $inLineBadge = false;
protected Closure|bool|null $badgeCallback = null;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
protected Closure|bool|null $badgeCallback = null;
protected Closure|bool $inLineBadge = false;

а зачем нужен null? и я бы оставил название

@@ -100,11 +100,11 @@ public function onlyCount(): static
return $this;
}

public function inLine(string $separator = '', bool $badge = false, ?Closure $link = null): static
public function inLine(string $separator = '', Closure|bool $badge = null, ?Closure $link = null): static
Copy link
Member

Choose a reason for hiding this comment

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

вы указали что badge может быть или Closure или bool, но присвоили null

Suggested change
public function inLine(string $separator = '', Closure|bool $badge = null, ?Closure $link = null): static
public function inLine(string $separator = '', Closure|bool $badge = false, ?Closure $link = null): static

{
$this->inLine = true;
$this->inLineSeparator = $separator;
$this->inLineBadge = $badge;
$this->badgeCallback = $badge;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->badgeCallback = $badge;
$this->inLineBadge = $badge;

return Badge::make((string) $value, 'primary')
->customAttributes(['class' => 'm-1'])
->render();
if ($this->badgeCallback) {
Copy link
Member

@DissNik DissNik Jul 6, 2024

Choose a reason for hiding this comment

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

я бы сделал что-то такое, но нужно подумать

if ($this->inLineBadge) {
    $badgeValue = value($this->inLineBadge, $item, $value, $this);

    $badge = $badgeValue instanceof Badge
        ? $badgeValue
        : Badge::make((string) $value, 'primary');

    return $badge->customAttributes(['class' => 'm-1'])->render();
}

@carma100
Copy link
Author

carma100 commented Jul 6, 2024

@DissNik поправил, с учётом ваших комментариев.

@@ -418,9 +418,13 @@ protected function resolvePreview(): View|string
}

if ($this->inLineBadge) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Не очень понятная проверка, лучше сделать value до этого if а потом проверить на false

Copy link
Member

Choose a reason for hiding this comment

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

да, тут value нужно до if, т.к. в замыкании мы можем вернуть false

->render();
$badgeValue = value($this->inLineBadge, $item, (string) $value, $this);

if ($badgeValue) {
Copy link
Member

Choose a reason for hiding this comment

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

если я правильно понял, Данил предложил сделать такую проверку

Suggested change
if ($badgeValue) {
if ($badgeValue !== false) {

return Badge::make((string) $value, 'primary')
->customAttributes(['class' => 'm-1'])
->render();
$badgeValue = value($this->inLineBadge, $item, (string) $value, $this);
Copy link
Collaborator

Choose a reason for hiding this comment

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

зачем здесь value приводить к строке?

@lee-to lee-to force-pushed the 2.x branch 2 times, most recently from a196fee to 8749849 Compare July 8, 2024 13:06
@lee-to lee-to merged commit 086e200 into moonshine-software:2.x Jul 8, 2024
5 checks passed
@DissNik
Copy link
Member

DissNik commented Jul 8, 2024

Пожалуйста, измените описание, чтоб оно соответствовало итоговому решению.

Документацию сможете поправить?

@carma100
Copy link
Author

carma100 commented Jul 8, 2024

Пожалуйста, измените описание, чтоб оно соответствовало итоговому решению.

Документацию сможете поправить?

Описание поменял. В документацию внесу изменения.

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.

3 participants