Skip to content

Commit

Permalink
formatting values
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 29, 2023
1 parent 4162e53 commit b76787c
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 13 deletions.
2 changes: 1 addition & 1 deletion resources/views/fields/boolean.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</label>
<div class="form-group">
<label class="form-switch form-switch--sm">
<input {{ $attrs->class(['form-switch__control']) }} value="1">
<input {{ $attrs }} value="1">
</label>
</div>
@if($invalid)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/file.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<span class="required-marker">*</span>
@endif
</label>
<input {{ $attrs->class(['form-file']) }}>
<input {{ $attrs }}>
@if(! empty($options))
<ul class="file-list__items">
@foreach($options as $option)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/range.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class="btn btn--primary btn--sm btn--icon"
>
<x-root::icon name="minus" class="btn__icon" />
</button>
<input {{ $attrs->class(['form-range', 'range-group__control']) }} value="{{ $value }}" x-model="value">
<input {{ $attrs }} value="{{ $value }}" x-model="value">
<button
type="button"
class="btn btn--primary btn--sm btn--icon"
Expand Down
20 changes: 20 additions & 0 deletions resources/views/fields/repeater-table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@if(! empty($values))
<table style="width: 100%;">
<thead>
<tr>
@foreach($values[0] as $field)
<th>{!! $field['label'] !!}</th>
@endforeach
</tr>
</thead>
<thead>
@foreach($values as $value)
<tr>
@foreach($value as $field)
<td>{!! $field['formattedValue'] !!}</td>
@endforeach
</tr>
@endforeach
</thead>
</table>
@endif
2 changes: 1 addition & 1 deletion resources/views/fields/textarea.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="form-group--row">
<label class="form-label" for="{{ $attrs->get('id') }}">{{ $label }}</label>
<textarea {{ $attrs->class(['form-control', 'form-control--invalid' => $invalid]) }}>{{ $value }}</textarea>
<textarea {{ $attrs }}>{{ $value }}</textarea>
@if($invalid)
<span class="field-feedback field-feedback--invalid">{!! $error !!}</span>
@endif
Expand Down
2 changes: 1 addition & 1 deletion resources/views/filters/search.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="search-form">
<input {{ $attrs->class(['form-control search-form__control']) }} value="{{ $value }}">
<input {{ $attrs }} value="{{ $value }}">
<button type="submit" class="search-form__submit">
<span class="sr-only">{{ $label }}</span>
<x-root::icon name="search" class="search-form__icon" />
Expand Down
19 changes: 19 additions & 0 deletions src/Fields/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
parent::__construct($label, $modelAttribute);

$this->type('checkbox');
$this->class(['form-switch__control']);
}

/**
Expand Down Expand Up @@ -52,4 +53,22 @@ public function resolveValue(Request $request, Model $model): mixed

return $value;
}

/**
* {@inheritdoc}
*/
public function resolveFormat(Request $request, Model $model): ?string
{
if (is_null($this->formatResolver)) {
$this->formatResolver = static function (Request $request, Model $model, ?bool $value): string {
return sprintf(
'<span class="status %s">%s</span>',
$value ? 'status--success' : 'status--danger',
$value ? __('Yes') : __('No')
);
};
}

return parent::resolveFormat($request, $model);
}
}
2 changes: 1 addition & 1 deletion src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public function resolveFormat(Request $request, Model $model): ?string
$value = $this->resolveValue($request, $model);

if (is_null($this->formatResolver)) {
return $value;
return is_array($value) ? json_encode($value) : $value;
}

return call_user_func_array($this->formatResolver, [$request, $model, $value]);
Expand Down
5 changes: 3 additions & 2 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
{
parent::__construct($label, $modelAttribute, $relation);

$this->type('file')->multiple(false);

$this->type('file');
$this->multiple(false);
$this->class(['form-file']);
$this->disk(Config::get('root.media.disk', 'public'));
}

Expand Down
6 changes: 5 additions & 1 deletion src/Fields/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
{
parent::__construct($label, $modelAttribute);

$this->type('range')->step(1)->min(0)->max(100);
$this->type('range');
$this->step(1);
$this->min(0);
$this->max(100);
$this->class(['form-range', 'range-group__control']);
}
}
47 changes: 43 additions & 4 deletions src/Fields/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class Repeater extends Field
*/
protected ?int $max = null;

/**
* Create a new repeater field instance.
*/
public function __construct(string $label, Closure|string|null $modelAttribute = null)
{
parent::__construct($label, $modelAttribute);

$this->hiddenOn(['index']);
}

/**
* Get the URI key.
*/
Expand Down Expand Up @@ -127,7 +137,7 @@ public function withFields(Closure $callback): static
$field->setModelAttribute($attribute)
->name($attribute)
->id($attribute)
->value(function () use ($tmpModel, $key): mixed {
->value(static function () use ($tmpModel, $key): mixed {
return $tmpModel->getAttribute($key);
});
});
Expand All @@ -138,6 +148,16 @@ public function withFields(Closure $callback): static
return $this->__withFields($callback);
}

/**
* Resolve the option fields.
*/
public function resolveOptionFields(Request $request, Model $model, Model $tmpModel): Fields
{
return is_null($this->optionFieldsResolver)
? new Fields()
: call_user_func_array($this->optionFieldsResolver, [$request, $model, $tmpModel]);
}

/**
* Make a new temporary model for the option.
*/
Expand Down Expand Up @@ -188,6 +208,27 @@ public function modelUrl(Model $model): string
return str_replace('{resourceModel}', $model->exists ? $model->getKey() : 'create', $this->getUri());
}

/**
* {@inheritdoc}
*/
public function resolveFormat(Request $request, Model $model): ?string
{
if (is_null($this->formatResolver)) {
$this->formatResolver = function (Request $request, Model $model, array $value): string {
$values = array_map(function (array $value) use ($request, $model): array {
return $this->resolveOptionFields($request, $model, $this->newTemporaryModel($value))
->authorized($request, $model)
->visible('show')
->mapToDisplay($request, $model);
}, $value);

return View::make('root::fields.repeater-table', ['values' => $values])->render();
};
}

return parent::resolveFormat($request, $model);
}

/**
* Register the routes using the given router.
*/
Expand Down Expand Up @@ -217,9 +258,7 @@ public function toOption(Request $request, Model $model, Model $tmpModel): array
'open' => true,
'value' => $tmpModel->getAttribute('_key'),
'label' => $this->getOptionName(),
'fields' => is_null($this->optionFieldsResolver)
? new Fields()
: call_user_func_array($this->optionFieldsResolver, [$request, $model, $tmpModel]),
'fields' => $this->resolveOptionFields($request, $model, $tmpModel),
];
}

Expand Down
10 changes: 10 additions & 0 deletions src/Fields/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class Textarea extends Field
*/
protected string $template = 'root::fields.textarea';

/**
* Create a new field instance.
*/
public function __construct(string $label, Closure|string|null $modelAttribute = null)
{
parent::__construct($label, $modelAttribute);

$this->class(['form-control']);
}

/**
* Set the rows attribute.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Filters/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Filters;

use Closure;
use Cone\Root\Fields\Text;

class SearchField extends Text
Expand All @@ -10,4 +11,14 @@ class SearchField extends Text
* The Blade template.
*/
protected string $template = 'root::filters.search';

/**
* Create a new field instance.
*/
public function __construct(string $label, Closure|string|null $modelAttribute = null)
{
parent::__construct($label, $modelAttribute);

$this->class(['search-form__control']);
}
}

0 comments on commit b76787c

Please sign in to comment.