Skip to content

Commit

Permalink
Merge pull request #41 from evelynfredin/fix/add-fields-to-grid
Browse files Browse the repository at this point in the history
Add fields parameter to Grid constructor and initialize fields property
  • Loading branch information
tdwesten authored Jan 10, 2025
2 parents d0b319e + 492ed8d commit 445acb1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/FieldTypes/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ class Grid extends Field

protected $min_rows;

protected $fields;

protected $mode = GridModeOption::Table;

public function __construct(string $handle)
public function __construct(string $handle, array $fields = [])
{
parent::__construct($handle);

$this->fields = collect($fields);
}

public function fieldToArray(): Collection
Expand All @@ -38,6 +42,7 @@ public function fieldToArray(): Collection
'max_rows' => $this->max_rows,
'min_rows' => $this->min_rows,
'mode' => $this->mode->value,
'fields' => $this->fieldsToArray(),
]);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Fields/GridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@

expect($field->toArray()['field']['mode'])->toBe(\Tdwesten\StatamicBuilder\Enums\GridModeOption::Table->value);
});

it('can render to a array with fields', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Grid('grid', [
new \Tdwesten\StatamicBuilder\FieldTypes\Text('title'),
]);

expect($field->toArray()['field']['fields'][0]['field']['type'])->toBe('text');
});

0 comments on commit 445acb1

Please sign in to comment.