diff --git a/src/Fields/Checkbox.php b/src/Fields/Checkbox.php index 076906e4..924c15ba 100644 --- a/src/Fields/Checkbox.php +++ b/src/Fields/Checkbox.php @@ -14,14 +14,11 @@ class Checkbox extends Select */ public function newOption(mixed $value, string $label): Option { - $option = parent::newOption($value, $label); - - $option->setAttributes([ - 'type' => 'checkbox', - 'class' => 'form-check__control', - 'name' => sprintf('%s[]', $this->getModelAttribute()), - ]); - - return $option; + return parent::newOption($value, $label) + ->class('form-check__control') + ->setAttributes([ + 'type' => 'checkbox', + 'name' => sprintf('%s[]', $this->getModelAttribute()), + ]); } } diff --git a/src/Fields/Date.php b/src/Fields/Date.php index 4a2b778e..46ad98e5 100644 --- a/src/Fields/Date.php +++ b/src/Fields/Date.php @@ -4,6 +4,9 @@ use Closure; use DateTimeInterface; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Date as DateFactory; class Date extends Field { @@ -29,7 +32,8 @@ public function __construct(string $label, Closure|string|null $modelAttribute = { parent::__construct($label, $modelAttribute); - $this->type('date')->step(1); + $this->type('date'); + $this->step(1); } /** @@ -37,7 +41,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute = */ public function min(string|DateTimeInterface $value): static { - return $this->setAttribute('min', (string) $value); + return $this->setAttribute('min', is_string($value) ? $value : $value->format('Y-m-d')); } /** @@ -45,7 +49,7 @@ public function min(string|DateTimeInterface $value): static */ public function max(string|DateTimeInterface $value): static { - return $this->setAttribute('max', (string) $value); + return $this->setAttribute('max', is_string($value) ? $value : $value->format('Y-m-d')); } /** @@ -61,10 +65,10 @@ public function step(int $value): static */ public function withTime(bool $value = true): static { - $this->format = $value ? 'Y-m-d H:i:s' : 'Y-m-d'; - $this->withTime = $value; + $this->format = $value ? 'Y-m-d H:i:s' : 'Y-m-d'; + $this->type($value ? 'datetime-local' : 'date'); return $this; @@ -79,4 +83,18 @@ public function timezone(?string $value = null): static return $this; } + + /** + * {@inheritdoc} + */ + public function resolveFormat(Request $request, Model $model): ?string + { + if (is_null($this->formatResolver)) { + $this->formatResolver = function (Request $request, Model $model, mixed $value): ?string { + return is_null($value) ? $value : DateFactory::parse($value)->tz($this->timezone)->format($this->format); + }; + } + + return parent::resolveFormat($request, $model); + } } diff --git a/src/Support/ClassList.php b/src/Support/ClassList.php index 8cb726b9..d9e3ec3d 100644 --- a/src/Support/ClassList.php +++ b/src/Support/ClassList.php @@ -26,8 +26,10 @@ public function __construct(array $classes = []) */ public function add(string|array $value): static { + $value = is_array($value) ? $value : explode(' ', $value); + $this->classes = array_values(array_unique( - array_merge($this->classes, (array) $value) + array_merge($this->classes, $value) )); return $this; diff --git a/src/Traits/HasAttributes.php b/src/Traits/HasAttributes.php index aee77538..f15961c5 100644 --- a/src/Traits/HasAttributes.php +++ b/src/Traits/HasAttributes.php @@ -89,7 +89,7 @@ public function getAttribute(string $key, mixed $default = null): mixed public function setAttribute(string $key, mixed $value): static { match ($key) { - 'class' => $this->classList()->clear()->add((array) $value), + 'class' => $this->classList()->clear()->add($value), default => $this->attributes[$key] = $value, }; diff --git a/tests/Fields/BooleanTest.php b/tests/Fields/BooleanTest.php index b52e8607..7e6c461b 100644 --- a/tests/Fields/BooleanTest.php +++ b/tests/Fields/BooleanTest.php @@ -18,6 +18,11 @@ public function setUp(): void $this->field = new Boolean('Admin'); } + public function test_a_boolean_field_has_boolean_template(): void + { + $this->assertSame('root::fields.boolean', $this->field->getTemplate()); + } + public function test_a_boolean_field_has_checkbox_type(): void { $this->assertSame('checkbox', $this->field->getAttribute('type')); @@ -34,11 +39,6 @@ public function test_a_boolean_field_has_checked_attribute(): void $this->assertFalse($this->field->getAttribute('checked')); } - public function test_a_boolean_field_has_boolean_template(): void - { - $this->assertSame('root::fields.boolean', $this->field->getTemplate()); - } - public function test_a_boolean_field_gets_value_for_hydrate(): void { $request = Request::createFrom($this->app['request']); diff --git a/tests/Fields/CheckboxTest.php b/tests/Fields/CheckboxTest.php new file mode 100644 index 00000000..cb62c9c7 --- /dev/null +++ b/tests/Fields/CheckboxTest.php @@ -0,0 +1,27 @@ +field = new Checkbox('Permissions'); + } + + public function test_a_checkbox_field_makes_new_option(): void + { + $option = $this->field->newOption('test', 'Test'); + + $this->assertTrue( + empty(array_diff(['value' => 'test'], $option->jsonSerialize())) + ); + } +} diff --git a/tests/Fields/ColorTest.php b/tests/Fields/ColorTest.php new file mode 100644 index 00000000..7a12cfeb --- /dev/null +++ b/tests/Fields/ColorTest.php @@ -0,0 +1,23 @@ +field = new Color('Primary'); + } + + public function test_a_color_field_has_color_type(): void + { + $this->assertSame('color', $this->field->getAttribute('type')); + } +} diff --git a/tests/Fields/DateTest.php b/tests/Fields/DateTest.php new file mode 100644 index 00000000..caeb4051 --- /dev/null +++ b/tests/Fields/DateTest.php @@ -0,0 +1,54 @@ +field = new Date('Created At'); + } + + public function test_a_date_field_has_date_type(): void + { + $this->assertSame('date', $this->field->getAttribute('type')); + $this->assertSame(1, $this->field->getAttribute('step')); + } + + public function test_date_has_min_max_attributes(): void + { + $this->assertNull($this->field->getAttribute('min')); + + $date = DateFactory::now(); + + $this->field->min($date); + $this->assertSame($date->format('Y-m-d'), $this->field->getAttribute('min')); + + $this->field->max($date); + $this->assertSame($date->format('Y-m-d'), $this->field->getAttribute('max')); + } + + public function test_a_date_field_has_time(): void + { + $model = new User(); + + $now = DateFactory::now(); + + $model->setAttribute('created_at', $now); + + $this->assertSame($now->format('Y-m-d'), $this->field->resolveFormat($this->app['request'], $model)); + + $this->field->withTime(); + $this->assertSame('datetime-local', $this->field->getAttribute('type')); + $this->assertSame($now->format('Y-m-d H:i:s'), $this->field->resolveFormat($this->app['request'], $model)); + } +}