Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 30, 2023
1 parent ae6dd20 commit f3452c0
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 21 deletions.
15 changes: 6 additions & 9 deletions src/Fields/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
]);
}
}
28 changes: 23 additions & 5 deletions src/Fields/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -29,23 +32,24 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
{
parent::__construct($label, $modelAttribute);

$this->type('date')->step(1);
$this->type('date');
$this->step(1);
}

/**
* Set the "min" HTML attribute.
*/
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'));
}

/**
* Set the "max" HTML attribute.
*/
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'));
}

/**
Expand All @@ -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;
Expand All @@ -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);
}
}
4 changes: 3 additions & 1 deletion src/Support/ClassList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
10 changes: 5 additions & 5 deletions tests/Fields/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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']);
Expand Down
27 changes: 27 additions & 0 deletions tests/Fields/CheckboxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cone\Root\Tests\Fields;

use Cone\Root\Fields\Checkbox;
use Cone\Root\Tests\TestCase;

class CheckboxTest extends TestCase
{
protected Checkbox $field;

public function setUp(): void
{
parent::setUp();

$this->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()))
);
}
}
23 changes: 23 additions & 0 deletions tests/Fields/ColorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Cone\Root\Tests\Fields;

use Cone\Root\Fields\Color;
use Cone\Root\Tests\TestCase;

class ColorTest extends TestCase
{
protected Color $field;

public function setUp(): void
{
parent::setUp();

$this->field = new Color('Primary');
}

public function test_a_color_field_has_color_type(): void
{
$this->assertSame('color', $this->field->getAttribute('type'));
}
}
54 changes: 54 additions & 0 deletions tests/Fields/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Cone\Root\Tests\Fields;

use Cone\Root\Fields\Date;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;
use Illuminate\Support\Facades\Date as DateFactory;

class DateTest extends TestCase
{
protected Date $field;

public function setUp(): void
{
parent::setUp();

$this->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));
}
}

0 comments on commit f3452c0

Please sign in to comment.