Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Dec 11, 2023
1 parent 7d3bcd1 commit 3c30114
Show file tree
Hide file tree
Showing 24 changed files with 401 additions and 314 deletions.
3 changes: 0 additions & 3 deletions resources/views/default.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<form wire:submit="submit" x-cloak>
{{-- <input type="text" wire:model.change="address.street"> --}}
<input type="text" wire:model.change="synthFields.values.first_name">
<input type="text" wire:model.change="synthFields.values.last_name">
<div class="grid gap-y-16">
@formView('layouts.sections')
<div class="grid gap-y-4">
Expand Down
9 changes: 8 additions & 1 deletion resources/views/default/layouts/field.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<div
x-data="{
fields: $wire.$entangle('fields'),
show() {
return @json($field->hidden) ? false : this.passesConditions()
},
passesConditions() {
return Statamic.$conditions.showField({{ $field->conditions }}, $wire.data)
return Statamic.$conditions.showField({{ $field->conditions }}, this.values())
},
values() {
return Object.entries(this.fields).reduce((values, [key, field]) => {
values[key] = field.value
return values
}, {});
},
}"
x-show="show"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/default/layouts/sections.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="grid gap-y-16">
@foreach($this->fields->sections() as $section)
@foreach($this->sections() as $section)
@formView('layouts.section')
@endforeach
</div>
11 changes: 11 additions & 0 deletions src/Fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Aerni\LivewireForms\Fields;

use Illuminate\Support\Arr;
use Statamic\Forms\Uploaders\AssetsUploader;
use Aerni\LivewireForms\Fields\Properties\WithMultiple;

class Assets extends Field
Expand All @@ -10,6 +12,15 @@ class Assets extends Field

protected string $view = 'assets';

public function process(): mixed
{
$value = parent::process();

return collect(Arr::wrap($value))
->flatMap(fn ($file) => AssetsUploader::field($this->handle)->upload($file))
->all();
}

protected function multipleProperty(): bool
{
return $this->field->get('max_files') !== 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Captcha extends Field
{
protected string $view = 'captcha';

public function process(mixed $value): mixed
public function process(): mixed
{
return null;
}
Expand Down
46 changes: 34 additions & 12 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use Aerni\LivewireForms\Fields\Properties\WithView;
use Aerni\LivewireForms\Fields\Properties\WithWidth;
use Aerni\LivewireForms\Fields\Properties\WithWireModel;
use Illuminate\Contracts\Support\Arrayable;
use Statamic\Fields\Field as StatamicField;
use Statamic\Support\Str;
use Statamic\Support\Traits\FluentlyGetsAndSets;

abstract class Field
abstract class Field implements Arrayable
{
use WithAlwaysSave;
use FluentlyGetsAndSets;
use WithConditions;
use WithDefault;
use WithHandle;
Expand All @@ -38,11 +40,12 @@ abstract class Field
use WithWidth;
use WithWireModel;

protected bool $submittable = true;

protected mixed $value;

public function __construct(protected StatamicField $field, protected string $id)
{
// TODO: Can we move this initialization somewhere else?
$this->value = $this->default;
}

Expand All @@ -66,24 +69,43 @@ public function validationAttributes(): array
return [$this->key => $this->label];
}

public function process(mixed $value): mixed
public function process(): mixed
{
if ($this->cast_booleans && in_array($value, ['true', 'false'])) {
return Str::toBool($value);
if (! $this->submittable()) {
return null;
}

if ($this->cast_booleans && in_array($this->value, ['true', 'false'])) {
return Str::toBool($this->value);
}

return $value;
return $this->value;
}

public function value(mixed $value = null): mixed
{
if (is_null($value)) {
return $this->value;
}
return $this->fluentlyGetOrSet('value')->args(func_get_args());
}

$this->value = $value;
public function submittable(bool $submittable = null): bool|self
{
return $this->fluentlyGetOrSet('submittable')
->getter(function ($submittable) {
return $this->field->get('always_save')
? true : $submittable;
})
->args(func_get_args());
}

return $this;
public function toArray(): array
{
return [
'id' => $this->id,
'handle' => $this->field->handle(),
'config' => $this->field->config(),
'value' => $this->value(),
'submittable' => $this->submittable(),
];
}

protected function get(string $key): mixed
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
class Honeypot extends Field
{
protected string $view = 'honeypot';

protected bool $submittable = false;
}
6 changes: 4 additions & 2 deletions src/Fields/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ protected function inputTypeProperty(): string
return 'number';
}

public function process(mixed $value): mixed
public function process(): mixed
{
return is_null($value) ? null : (int) $value;
$value = parent::process();

return is_null($$value) ? null : (int) $$value;
}
}
11 changes: 0 additions & 11 deletions src/Fields/Properties/WithAlwaysSave.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Fields/Properties/WithKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ trait WithKey
{
protected function keyProperty(): string
{
return "data.{$this->handle()}";
return "fields.{$this->handle()}.value";
}
}
10 changes: 6 additions & 4 deletions src/Fields/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ protected function hiddenProperty(): bool
: parent::hiddenProperty();
}

public function process(mixed $value): mixed
public function process(): mixed
{
if ($this->input_type !== 'number') {
return $value;
$value = parent::process();

if ($this->input_type === 'number') {
return is_null($value) ? null : (int) $value;
}

return is_null($value) ? null : (int) $value;
return $value;
}
}
Loading

0 comments on commit 3c30114

Please sign in to comment.