Skip to content

Commit

Permalink
Added select method to tab field (#147)
Browse files Browse the repository at this point in the history
* Added selected method to tab field

* Test selected method

* Fix syntax issues

* Fix syntax issues

* Delete .php-cs-fixer.cache

* Update gutenberg-block.php
  • Loading branch information
vinkla authored May 2, 2024
1 parent 45484a4 commit 04a6bf3
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ Tab::make('Tab 1'),
Tab::make('Tab 2'),
Tab::make('Tab 3')
->placement('top') // top, left
->selected() // specify which tab should be selected by default
->endpoint(), // This will make a break in the tabs and create a new group of tabs
```

Expand Down
4 changes: 2 additions & 2 deletions examples/gutenberg-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
'fields' => [
PostObject::make('Employee')
->postTypes(['employee'])
->format('object')
->format('object'),
],
'location' => [
Location::where('block', 'acf/employee')
Location::where('block', 'acf/employee'),
],
]);
8 changes: 4 additions & 4 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function key(string $key): static
{
if (!str_starts_with($key, $this->keyPrefix . '_')) {
throw new InvalidArgumentException(
sprintf('The key should have the prefix [%s_].', $this->keyPrefix)
sprintf('The key should have the prefix [%s_].', $this->keyPrefix),
);
}

Expand Down Expand Up @@ -111,21 +111,21 @@ public function get(string|null $parentKey = null): array
if (isset($this->settings['conditional_logic'])) {
$this->settings['conditional_logic'] = array_map(
fn($rules) => $rules->get($parentKey),
$this->settings['conditional_logic']
$this->settings['conditional_logic'],
);
}

if (isset($this->settings['layouts'])) {
$this->settings['layouts'] = array_map(
fn($layout) => $layout->get($key),
$this->settings['layouts']
$this->settings['layouts'],
);
}

if (isset($this->settings['sub_fields'])) {
$this->settings['sub_fields'] = array_map(
fn($field) => $field->get($key),
$this->settings['sub_fields']
$this->settings['sub_fields'],
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Settings/ConditionalLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function conditionalLogic(array $rules): static
{
$this->settings['conditional_logic'] = array_merge(
$this->settings['conditional_logic'] ?? [],
$rules
$rules,
);

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Settings/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function wrapper(array $wrapper): static
{
$this->settings['wrapper'] = array_merge(
$this->settings['wrapper'] ?? [],
$wrapper
$wrapper,
);

return $this;
Expand Down
7 changes: 7 additions & 0 deletions src/Fields/Tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ public function placement(string $placement): static

return $this;
}

public function selected(): static
{
$this->settings['selected'] = true;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Fields/WYSIWYGEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function toolbar(string|array $toolbar): static
$this->settings['toolbar'] = implode('_', $toolbar);

add_filter('acf/fields/wysiwyg/toolbars', function (
array $toolbars
array $toolbars,
) use ($toolbar) {
$toolbars[$this->settings['toolbar']] = [
1 => $toolbar,
Expand Down
14 changes: 7 additions & 7 deletions tests/ConditionalLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testConditionalLogic()
'field' => 'field_f5456193',
'operator' => '==',
'value' => 10,
]
],
];

$this->assertSame($conditionalLogic, ConditionalLogic::where('age', '==', 10)->get('field'));
Expand Down Expand Up @@ -59,14 +59,14 @@ public function testResolvedParentKey()
->conditionalLogic([
ConditionalLogic::where('select', '==', 'red'),
]),
])
]),
],
'location' => []
'location' => [],
]);

$this->assertSame(
$settings['fields'][0]['key'],
$settings['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field']
$settings['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field'],
);
}

Expand All @@ -84,16 +84,16 @@ public function testFieldKey()
name: 'select',
operator: '==',
value: 'red',
key: 'field_123abc'
key: 'field_123abc',
),
]),
],
'location' => []
'location' => [],
]);

$this->assertSame(
'field_123abc',
$settings['fields'][1]['conditional_logic'][0][0]['field']
$settings['fields'][1]['conditional_logic'][0][0]['field'],
);
}
}
4 changes: 2 additions & 2 deletions tests/Fields/Settings/ConditionalLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testConditionalLogicExternalGroup()
group: 'external',
name: 'type',
operator: '==empty',
)
),
])->get('group');
$this->assertSame('field_21649737', $field['conditional_logic'][0][0]['field']);
}
Expand All @@ -32,7 +32,7 @@ public function testConditionalLogicAnd()
{
$field = $this->make('Conditional Logic And')
->conditionalLogic([
\Extended\ACF\ConditionalLogic::where('type', '==', 'video')->and('highlight', '!=', 'true')
\Extended\ACF\ConditionalLogic::where('type', '==', 'video')->and('highlight', '!=', 'true'),
])->get('group');

$this->assertSame('==', $field['conditional_logic'][0][0]['operator']);
Expand Down
6 changes: 6 additions & 0 deletions tests/Fields/TabTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public function testPlacement()

Tab::make('Invalid Placement')->placement('test')->get();
}

public function testSelected()
{
$field = Tab::make('Selected')->selected()->get();
$this->assertTrue($field['selected']);
}
}

0 comments on commit 04a6bf3

Please sign in to comment.