Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Oct 14, 2023
1 parent 9a5cc01 commit 70e3abd
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 111 deletions.
4 changes: 2 additions & 2 deletions resources/views/columns/cells/row-actions.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<td>
<div class="data-table__actions">
@can('update', $model)
<a href="{{ $url }}" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('Edit') }}">
<a href="{{ $value }}" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('Edit') }}">
<x-root::icon name="edit" class="btn__icon" />
</a>
@endcan
@can('delete', $model)
<form action="{{ $url }}" method="POST" onsubmit="return window.confirm('{{ __('Are you sure?') }}');">
<form action="{{ $value }}" method="POST" onsubmit="return window.confirm('{{ __('Are you sure?') }}');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn--delete btn--sm btn--icon" aria-label="{{ __('Delete') }}">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/checkbox-option.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<label class="form-check">
<input {{ $attrs->class(['form-check__control']) }}>
<input {{ $attrs }}>
<span class="form-label form-check__label">{{ $label }}</span>
</label>
2 changes: 1 addition & 1 deletion resources/views/fields/checkbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</label>
<div class="form-group form-group--vertical-check">
@foreach($options as $option)
{!! $option !!}
@include('root::fields.checkbox-option', $option)
@endforeach
</div>
@if($invalid)
Expand Down
1 change: 0 additions & 1 deletion resources/views/fields/option.blade.php

This file was deleted.

3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

// Resource Fields
Route::any('/{resource}/fields/{field}', ResourceFieldController::class)->where('field', '.*');

// Actions
// Action Fields
16 changes: 11 additions & 5 deletions src/Fields/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Cone\Root\Fields;

use Cone\Root\Fields\Options\CheckboxOption;

class Checkbox extends Select
{
/**
Expand All @@ -12,10 +10,18 @@ class Checkbox extends Select
protected string $template = 'root::fields.checkbox';

/**
* Make a new option instance.
* {@inheritdoc}
*/
public function newOption(mixed $value, string $label): CheckboxOption
public function newOption(mixed $value, string $label): Option
{
return CheckboxOption::make($value, $label)->name(sprintf('%s[]', $this->getModelAttribute()));
$option = parent::newOption($value, $label);

$option->setAttributes([
'type' => 'checkbox',
'class' => 'form-check__control',
'name' => sprintf('%s[]', $this->getModelAttribute()),
]);

return $option;
}
}
34 changes: 15 additions & 19 deletions src/Fields/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Cone\Root\Fields;

use Cone\Root\Fields\Options\DropdownOption;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;

class Dropdown extends Select
{
Expand All @@ -22,34 +24,28 @@ public function __construct(string $label, string $modelAttribute = null)
$this->setAttribute('class', 'form-control combobox__control');
}

/**
* Make a new option instance.
*/
public function newOption(mixed $value, string $label): DropdownOption
{
return new DropdownOption($value, $label);
}

/**
* {@inheritdoc}
*/
public function toArray(): array
public function toFormComponent(Request $request, Model $model): array
{
$data = parent::toArray();
$data = parent::toFormComponent($request, $model);

return array_merge($data, [
'options' => array_map(static function (DropdownOption $option): array {
return $option->toRenderedArray();
'options' => array_map(static function (array $option): array {
return array_merge($option, [
'html' => View::make('root::fields.dropdown-option', $option)->render(),
]);
}, $data['options']),
'selection' => Collection::make($data['options'])
->filter(function (DropdownOption $option): bool {
return $option->getAttribute('selected');
->filter(fn (array $option): bool => $option['selected'] ?? false)
->map(static function (array $option): array {
return array_merge($option, [
'html' => View::make('root::fields.dropdown-option', $option)->render(),
]);
})
->values()
->map(function (DropdownOption $option): array {
return $option->toRenderedArray();
})
->toArray(),
->all(),
'config' => [
'multiple' => $this->getAttribute('multiple'),
],
Expand Down
1 change: 0 additions & 1 deletion src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Fields\Options\Option;
use Cone\Root\Jobs\MoveFile;
use Cone\Root\Jobs\PerformConversions;
use Cone\Root\Models\Medium;
Expand Down
13 changes: 11 additions & 2 deletions src/Fields/Options/Option.php → src/Fields/Option.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

namespace Cone\Root\Fields\Options;
namespace Cone\Root\Fields;

use Cone\Root\Traits\HasAttributes;
use Cone\Root\Traits\Makeable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Traits\Conditionable;
use JsonSerializable;

class Option implements Arrayable
class Option implements Arrayable, JsonSerializable
{
use Conditionable;
use HasAttributes;
Expand Down Expand Up @@ -44,6 +45,14 @@ public function selected(bool $value = true): static
return $this->setAttribute('selected', $value);
}

/**
* Convert the element to a JSON serializable format.
*/
public function jsonSerialize(): mixed
{
return $this->toArray();
}

/**
* Get the array representation of the object.
*/
Expand Down
45 changes: 0 additions & 45 deletions src/Fields/Options/CheckboxOption.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Fields/Options/DropdownOption.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Fields/Options/RadioOption.php

This file was deleted.

16 changes: 11 additions & 5 deletions src/Fields/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

namespace Cone\Root\Fields;

use Cone\Root\Fields\Options\RadioOption;

class Radio extends Checkbox
{
/**
* Make a new option instance.
* {@inheritdoc}
*/
public function newOption(mixed $value, string $label): RadioOption
public function newOption(mixed $value, string $label): Option
{
return RadioOption::make($value, $label)->name($this->getModelAttribute());
$option = parent::newOption($value, $label);

$option->setAttributes([
'type' => 'radio',
'class' => 'form-check__control',
'name' => $this->getModelAttribute(),
]);

return $option;
}
}
1 change: 0 additions & 1 deletion src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Fields\Options\Option;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation;
Expand Down
1 change: 0 additions & 1 deletion src/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Fields\Options\Option;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
Expand Down

0 comments on commit 70e3abd

Please sign in to comment.