Skip to content

Commit

Permalink
Datepicker: add support for plugins (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
robsontenorio committed Apr 27, 2024
1 parent 12796a7 commit dcc94d8
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/View/Components/DatePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Arr;
use Illuminate\View\Component;

class DatePicker extends Component
{

public string $uuid;

public function __construct(
Expand All @@ -23,8 +23,7 @@ public function __construct(
public ?string $errorClass = 'text-red-500 label-text-alt p-1',
public ?bool $omitError = false,
public ?bool $firstErrorOnly = false,
)
{
) {
$this->uuid = "mary" . md5(serialize($this));
}

Expand All @@ -43,12 +42,23 @@ public function setup(): string
$config = json_encode(array_merge([
'dateFormat' => 'Y-m-d H:i',
'altInput' => true,
'clickOpens' => !$this->attributes->has('readonly') || $this->attributes->get('readonly') == false,
'defaultDate' => 'x',
], $this->config));
'clickOpens' => ! $this->attributes->has('readonly') || $this->attributes->get('readonly') == false,
'defaultDate' => '#model#',
'plugins' => ['#plugins#'],
], Arr::except($this->config, ["plugins"])));

// Plugins
$plugins = null;

foreach (Arr::get($this->config, 'plugins', []) as $plugin) {
$plugins .= "new " . key($plugin) . "( " . json_encode(current($plugin)) . " ),";
}

// Plugins
$config = str_replace('"#plugins#"', $plugins, $config);

// Sets default date as current binded model
$config = str_replace('"x"', '$wire.' . $this->modelName(), $config);
// Sets default date as current bound model
$config = str_replace('"#model#"', '$wire.' . $this->modelName(), $config);

return $config;
}
Expand Down

0 comments on commit dcc94d8

Please sign in to comment.