Skip to content

Commit

Permalink
Refactor submittable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Dec 17, 2023
1 parent 0e8b107 commit 87d7652
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 59 deletions.
8 changes: 4 additions & 4 deletions resources/dist/js/livewire-forms.js

Large diffs are not rendered by default.

42 changes: 19 additions & 23 deletions resources/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,31 @@ export default () => ({
conditions: new FieldConditions,

processFields(fields) {
this.fields = Object.entries(fields).reduce((field, [key, value]) => {
field[key] = {
value: value.value,
conditions: value.properties.conditions ?? [],
visible: true,
const values = Object.entries(fields).reduce((fields, [key, field]) => {
fields[key] = field.value

return fields
}, {})

this.fields = Object.entries(fields).reduce((fields, [key, field]) => {
const passesConditions = this.conditions.showField(field.properties.conditions, values)

fields[key] = {
visible: passesConditions && !field.properties.hidden,
submittable: field.properties.always_save || passesConditions
}
return field;
}, {});

this.$wire.submittableFields[key] = fields[key].submittable

return fields
}, {})
},

showField(field) {
return this.fields[field].visible = this.conditions.showField(this.fields[field].conditions, this.values())
return this.fields[field].visible
},

showSection(fields) {
return Object.entries(fields).some(([field]) => this.fields[field].visible)
},

values() {
return Object.entries(this.fields).reduce((field, [key, value]) => {
field[key] = value.value
return field;
}, {});
},

submittableFields() {
return Object.entries(this.fields).reduce((field, [key, value]) => {
field[key] = value.visible
return field;
}, {});
},
}
})
2 changes: 1 addition & 1 deletion resources/views/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
x-data="form"
x-effect="processFields($wire.fields)"
x-cloak
wire:submit="submit(submittableFields())"
wire:submit="submit"
>
<div class="grid gap-y-16">
@formView('layouts.sections')
Expand Down
1 change: 0 additions & 1 deletion resources/views/default/layouts/field.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class="space-y-2 col-span-1
{{ $field->width === 66 ? 'md:col-span-8' : '' }}
{{ $field->width === 75 ? 'md:col-span-9' : '' }}
{{ $field->width === 100 ? 'md:col-span-12' : '' }}
{{ $field->hidden ? 'hidden' : '' }}
"
>
@formView($field->view)
Expand Down
13 changes: 0 additions & 13 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ abstract class Field implements Arrayable

protected mixed $value = null;

protected bool $submittable = true;

public function __construct(protected StatamicField $field, protected string $id)
{
//
Expand All @@ -67,10 +65,6 @@ public function validationAttributes(): array

public function process(): mixed
{
if (! $this->submittable()) {
return null;
}

if ($this->cast_booleans && in_array($this->value, ['true', 'false'])) {
return Str::toBool($this->value);
}
Expand All @@ -92,13 +86,6 @@ public function resetValue(): self
return $this;
}

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

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

protected bool $submittable = false;
}
11 changes: 0 additions & 11 deletions src/Livewire/Concerns/HandlesSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,17 @@

trait HandlesSubmission
{
protected array $submittableFields = [];

protected Submission $submission;

protected function handleSubmission(): self
{
return $this
->updateSubmittableFields()
->makeSubmission()
->handleFormEvents()
->saveSubmission()
->sendEmails();
}

// TODO: Remove submittable on the field?
protected function updateSubmittableFields(): self
{
collect($this->submittableFields)->each(fn ($submittable, $field) => $this->fields->get($field)->submittable($submittable));

return $this;
}

protected function makeSubmission(): self
{
$submission = $this->form->makeSubmission();
Expand Down
7 changes: 6 additions & 1 deletion src/Livewire/Concerns/HandlesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ trait HandlesValues
{
use WithFileUploads;

public array $submittableFields = [];

protected function values(): Collection
{
return $this->fields->map(fn ($field) => $field->value());
}

protected function processedValues(): Collection
{
return $this->fields->map(fn ($field) => $field->process());
return $this->fields
->filter(fn ($field) => $this->submittableFields[$field->handle])
->filter(fn ($field) => $field->handle !== $this->honeypot->handle)
->map(fn ($field) => $field->process());
}

protected function resetValues(): Collection
Expand Down
4 changes: 1 addition & 3 deletions src/Livewire/Concerns/SubmitsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ trait SubmitsForm
use HandlesSuccess;
use HandlesValidation;

public function submit(array $submittableFields): void
public function submit(): void
{
$this->validate();

$this->submittableFields = $submittableFields;

try {
$this->handleSpam()->handleSubmission()->handleSuccess();
} catch (SilentFormFailureException) {
Expand Down

0 comments on commit 87d7652

Please sign in to comment.