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

Update in popover fix #1383

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,24 @@ public function getField(): ?FieldContract
*/
public function rules(): array
{
$fieldRules = data_get(
$this->getResource()?->getRules(),
request()->getScalar('field'),
);

$valueRules = ['present'];

if (\is_string($fieldRules)) {
$valueRules[] = $valueRules;
}

if (\is_array($fieldRules)) {
$valueRules = array_merge($valueRules, $fieldRules);
}

return [
'field' => ['required'],
'value' => ['present'],
'value' => $valueRules,
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/UI/src/Contracts/HasUpdateOnPreviewContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

interface HasUpdateOnPreviewContract
{
public function disableUpdateOnPreview(): static;

public function isUpdateOnPreview(): bool;

public function setUpdateOnPreviewUrl(Closure $url): static;
Expand Down
21 changes: 15 additions & 6 deletions src/UI/src/Sets/UpdateOnPreviewPopover.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
use MoonShine\UI\Components\Layout\Flex;
use MoonShine\UI\Components\Link;
use MoonShine\UI\Components\Popover;
use MoonShine\UI\Contracts\HasUpdateOnPreviewContract;
use MoonShine\UI\Fields\Hidden;
use MoonShine\UI\Fields\Text;

final readonly class UpdateOnPreviewPopover
{
public function __construct(private FieldContract $field, private string $component, private string $route)
/**
* @param FieldContract&HasUpdateOnPreviewContract $field
* @param string $component
* @param string $route
*/
public function __construct(private HasUpdateOnPreviewContract $field, private string $component, private string $route)
{
}

Expand Down Expand Up @@ -46,13 +51,17 @@ public function __invoke(): Popover
Flex::make([
Hidden::make('_method')->setValue('PUT'),
Hidden::make('field')->setValue($this->field->getColumn()),
Text::make('Title', 'value')
$this->field
->style('margin: 0!important')
->setValue($this->field->toFormattedValue())
->withoutWrapper(),
->setColumn('value')
->customAttributes([
'name' => 'value',
])
->withoutWrapper()
->disableUpdateOnPreview(),
]),
])
->submit('OK', ['class' => 'btn-primary'])
->submit(__('moonshine::ui.save'), ['class' => 'btn-primary'])
);
}
}
12 changes: 12 additions & 0 deletions src/UI/src/Traits/Fields/UpdateOnPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public function readonly(Closure|bool|null $condition = null): static
return parent::readonly($condition);
}

public function disableUpdateOnPreview(): static
{
$this->updateOnPreview = false;
$this->onChangeUrl = null;
$this->updateOnPreviewPopover = false;
$this->updateOnPreviewUrl = null;

$this->removeAttribute('@change');

return $this;
}

public function withUpdateRow(string $component): static
{

Expand Down