Skip to content

Commit

Permalink
Add prefix and custom attributes to Field class, and implement fullsc…
Browse files Browse the repository at this point in the history
…reen option in Group class
  • Loading branch information
tdwesten committed Jan 31, 2024
1 parent 5472151 commit 4084ab9
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 5 deletions.
42 changes: 37 additions & 5 deletions src/FieldTypes/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Field implements Renderable
{
protected $handle;

protected $prefix;

protected $type = 'text';

protected $displayName;
Expand All @@ -35,12 +37,14 @@ class Field implements Renderable

protected $width;

protected $customAttributes;

/**
* @var Collection
*/
protected $validate;

public function __construct($handle, $type = null)
public function __construct($handle)
{
$this->handle = $handle;
$this->type = $this->getType();
Expand All @@ -62,10 +66,12 @@ public function toArray()
{
$fieldDefaults = $this->fieldDefaults();

$field = $fieldDefaults->merge($this->fieldToArray());
$field = $fieldDefaults
->merge($this->fieldToArray())
->merge($this->customAttributes ?? []);

$content = collect([
'handle' => $this->handle,
'handle' => $this->getHandle(),
'field' => $field,
]);

Expand All @@ -79,8 +85,6 @@ public function toArray()
// Sort keys
$content['field'] = collect($content['field'])->sortKeys()->all();

ray($content->toArray())->label('Field '.$this->type);

return $content->toArray();
}

Expand All @@ -91,6 +95,7 @@ public function fieldDefaults(): Collection
'display' => $this->displayName,
'duplicate' => $this->duplicate,
'hide_display' => $this->hideDisplay,
'icon' => $this->icon,
'instructions' => $this->instructions,
'instructions_position' => $this->instructionsPosition,
'listable' => $this->listable,
Expand All @@ -113,11 +118,30 @@ public function validateToArray(): array
return $this->validate->toArray();
}

public function withAttributes(array $attributes)
{
$this->customAttributes = $attributes;

return $this;

}

public function getHandle()
{
if ($this->prefix) {
return $this->prefix.'.'.$this->handle;
}

return $this->handle;
}

public function prefix($prefix)
{
$this->prefix = $prefix;

return $this;
}

public function getType()
{
return $this->type;
Expand Down Expand Up @@ -241,4 +265,12 @@ public function width($width)

return $this;
}

public function type(string $type)
{
$this->type = $type;

return $this;

}
}
128 changes: 128 additions & 0 deletions tests/Unit/FieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

test('Can set a handle prefix', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->displayName('Name')
->prefix('county');

expect($field->toArray()['handle'])->toBe('county.title');
});

test('Can set a display name', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->displayName('Name');

expect($field->toArray()['field']['display'])->toBe('Name');
});

test('Can set instructions', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->instructions('Enter the title');

expect($field->toArray()['field']['instructions'])->toBe('Enter the title');
});

test('Can set visibility', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->visibility('hidden');

expect($field->toArray()['field']['visibility'])->toBe('hidden');
});

test('Can set required', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->required();

expect($field->toArray()['field']['validate'])->toBe(['required']);
});

test('Can set instructions position', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->instructionsPosition('below');

expect($field->toArray()['field']['instructions_position'])->toBe('below');
});

test('Can set listable', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->listable();

expect($field->toArray()['field']['listable'])->toBe(true);
});

test('Can set replicator preview', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->replicatorPreview(true);

expect($field->toArray()['field']['replicator_preview'])->toBe(true);
});

test('Can set width', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->width(50);

expect($field->toArray()['field']['width'])->toBe(50);
});

test('Can set antlers', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->antlers(true);

expect($field->toArray()['field']['antlers'])->toBe(true);
});

test('Can set duplicate', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->duplicate(true);

expect($field->toArray()['field']['duplicate'])->toBe(true);
});

test('Can set hide display', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->hideDisplay(true);

expect($field->toArray()['field']['hide_display'])->toBe(true);
});

test('Can set validation to sometimes', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field
->required()
->sometimes();

expect($field->toArray()['field']['validate'])->toBe(['required', 'sometimes']);
});

test(' can set multiple validation rules', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->validate(['required', 'min:5']);

expect($field->toArray()['field']['validate'])->toBe(['required', 'min:5']);
});

test('Can set a custom icon', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->icon('icon');

expect($field->toArray()['field']['icon'])->toBe('icon');
});

test('Can create a thirth-party field', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field
->type('tailwind-color')
->withAttributes([
'swatches' => [
'red' => 'Red',
'blue' => 'Blue',
],
]);

expect($field->toArray()['field']['type'])->toBe('tailwind-color');

expect($field->toArray()['field']['swatches'])->toBe([
'red' => 'Red',
'blue' => 'Blue',
]);
});
14 changes: 14 additions & 0 deletions tests/Unit/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,17 @@

expect($group->toArray())->toBe($expected);
});

test('Can set to fullscreen', function () {

$group = Group::make('cta', [
Text::make('label')
->displayName('Label')
->instructionsPosition('above')
->visibility('visible')
->listable()
->width(33),
])->displayName('Call to Action')->fullscreen();

expect($group->toArray()['field']['fullscreen'])->toBe(true);
});
5 changes: 5 additions & 0 deletions tests/Unit/TermsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'display' => 'Counties',
'duplicate' => true,
'hide_display' => false,
'icon' => 'taxonomy',
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'hidden',
Expand Down Expand Up @@ -51,6 +52,7 @@
'display' => 'Counties',
'duplicate' => true,
'hide_display' => false,
'icon' => 'taxonomy',
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'hidden',
Expand Down Expand Up @@ -87,6 +89,7 @@
'display' => 'Counties',
'duplicate' => true,
'hide_display' => false,
'icon' => 'taxonomy',
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'hidden',
Expand Down Expand Up @@ -124,6 +127,7 @@
'display' => 'Counties',
'duplicate' => true,
'hide_display' => false,
'icon' => 'taxonomy',
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'hidden',
Expand Down Expand Up @@ -162,6 +166,7 @@
'display' => 'Counties',
'duplicate' => true,
'hide_display' => false,
'icon' => 'taxonomy',
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'hidden',
Expand Down

0 comments on commit 4084ab9

Please sign in to comment.