Skip to content

Commit

Permalink
Merge pull request #1383 from moonshine-software/update-in-popover-fix
Browse files Browse the repository at this point in the history
Update in popover fix
  • Loading branch information
lee-to authored Dec 18, 2024
2 parents 9faa0f6 + 973c4f6 commit 83ad3a7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
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

0 comments on commit 83ad3a7

Please sign in to comment.