Skip to content

Commit

Permalink
Use autocomplete by default only for select fields in search form.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Sep 8, 2024
1 parent 4820ff6 commit 21d75e8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
18 changes: 11 additions & 7 deletions src/Listener/ViewSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function afterPaginate(EventInterface $event): void
*/
public function fields(): array
{
$fields = Hash::normalize($this->getConfig('fields'), default: []) ?: [];
$fields = $this->getConfig('fields');
$config = $this->getConfig();

$schema = $this->_model()->getSchema();
Expand Down Expand Up @@ -119,13 +119,14 @@ public function fields(): array

$input['value'] = $request->getQuery($field);

if (empty($input['options']) && $schema->getColumnType($field) === 'boolean') {
$input['options'] = [__d('crud', 'No'), __d('crud', 'Yes')];
if (!isset($input['options']) && $schema->getColumnType($field) === 'boolean') {
$input['options'] = [1 => __d('crud', 'Yes'), 0 => __d('crud', 'No')];
$input['type'] = 'select';
}

if (!empty($input['options'])) {
if (isset($input['options'])) {
$input['empty'] ??= $this->getPlaceholder($field);

if (empty($input['class']) && !$config['select2']) {
$input['class'] = 'no-select2';
}
Expand All @@ -135,7 +136,7 @@ public function fields(): array
continue;
}

if (empty($input['class']) && $config['autocomplete']) {
if ($input['type'] === 'select' && empty($input['class']) && $config['autocomplete']) {
$input['class'] = 'autocomplete';
}

Expand Down Expand Up @@ -167,8 +168,11 @@ public function fields(): array
$input['empty'] ??= $this->getPlaceholder($field);
}

$urlArgs = [];
if (!isset($input['data-url'])) {
if (
!empty($input['class'])
&& strpos($input['class'], 'autocomplete') !== false
&& !isset($input['data-url'])
) {
$urlArgs = [];

$fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field];
Expand Down
52 changes: 43 additions & 9 deletions tests/TestCase/Listener/ViewSearchListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function testFields()
{
$this->listener->setConfig(['fields' => [
'name',
'auto' => ['class' => 'autocomplete'],
'is_active',
'user_id',
'custom_select' => ['empty' => false, 'type' => 'select'],
Expand All @@ -60,41 +61,47 @@ public function testFields()
$expected = [
'name' => [
'required' => false,
'type' => 'select',
'type' => 'text',
'value' => null,
'placeholder' => 'Name',
],
'auto' => [
'class' => 'autocomplete',
'data-url' => '/blogs/lookup.json?id=name&value=name',
'required' => false,
'type' => 'select',
'value' => null,
'data-input-type' => 'text',
'data-tags' => 'true',
'data-allow-clear' => 'true',
'data-placeholder' => '',
'empty' => 'Name',
'empty' => 'Auto',
'data-url' => '/blogs/lookup.json?id=auto&value=auto',
],
'is_active' => [
'required' => false,
'type' => 'select',
'value' => null,
'empty' => true,
'options' => ['No', 'Yes'],
'options' => [1 => 'Yes', 0 => 'No'],
'empty' => 'Is Active',
],
'user_id' => [
'required' => false,
'type' => 'select',
'empty' => true,
'value' => null,
'class' => 'autocomplete',
'empty' => 'User',
'data-url' => '/blogs/lookup.json?id=user_id&value=user_id',
],
'custom_select' => [
'required' => false,
'type' => 'select',
'empty' => false,
'type' => 'select',
'required' => false,
'value' => null,
'class' => 'autocomplete',
'data-url' => '/blogs/lookup.json?id=custom_select&value=custom_select',
],
];
$this->assertEquals($expected, $fields);
$this->assertSame($expected, $fields);

$this->listener->setConfig([
'fields' => ['name' => ['data-url' => '/custom']],
Expand All @@ -103,5 +110,32 @@ public function testFields()
$fields = $this->listener->fields();
$expected['name']['data-url'] = '/custom';
$this->assertEquals($expected, $fields);

$this->listener->setConfig([
'autocomplete' => false,
'fields' => [
'user_id',
'custom_select' => ['empty' => false, 'type' => 'select', 'class' => 'autocomplete'],
],
], merge: false);

$fields = $this->listener->fields();
$expected = [
'user_id' => [
'required' => false,
'type' => 'select',
'value' => null,
'empty' => 'User',
],
'custom_select' => [
'empty' => false,
'type' => 'select',
'class' => 'autocomplete',
'required' => false,
'value' => null,
'data-url' => '/blogs/lookup.json?id=custom_select&value=custom_select',
],
];
$this->assertSame($expected, $fields);
}
}

0 comments on commit 21d75e8

Please sign in to comment.