Skip to content

Commit

Permalink
Fix search inline form display
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 6, 2023
1 parent 84be527 commit 8e70739
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
34 changes: 28 additions & 6 deletions src/Listener/ViewSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Cake\Event\EventInterface;
use Cake\Routing\Router;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Crud\Listener\BaseListener;
use function Cake\I18n\__d;

/**
* @method \Cake\ORM\Table _model()
Expand Down Expand Up @@ -82,14 +84,14 @@ public function afterPaginate(EventInterface $event): void
*/
public function fields(): array
{
$fields = $this->getConfig('fields') ?: [];
$fields = Hash::normalize($this->getConfig('fields'), default: []) ?: [];
$config = $this->getConfig();

$schema = $this->_model()->getSchema();
$request = $this->_request();

if ($fields) {
$fields = Hash::normalize($fields);
$fields = Hash::normalize($fields, default: []);
} else {
$filters = $this->_model()->searchManager()->getFilters($config['collection']);

Expand All @@ -109,21 +111,21 @@ public function fields(): array
'type' => 'text',
];

if (substr($field, -3) === '_id' && $field !== '_id') {
if (str_ends_with($field, '_id') && $field !== '_id') {
$input['type'] = 'select';
}

$input = (array)$opts + $input;
$input = $opts + $input;

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

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

if (!empty($input['options'])) {
$input['empty'] = true;
$input['empty'] ??= $this->getPlaceholder($field);
if (empty($input['class']) && !$config['select2']) {
$input['class'] = 'no-select2';
}
Expand Down Expand Up @@ -156,6 +158,13 @@ public function fields(): array
];
}

if ($input['type'] === 'text') {
$input['placeholder'] ??= $this->getPlaceholder($field);
}
if ($input['type'] === 'select') {
$input['empty'] ??= $this->getPlaceholder($field);
}

$urlArgs = [];

$fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field];
Expand All @@ -174,4 +183,17 @@ public function fields(): array

return $fields;
}

protected function getPlaceholder(string $field): string

Check failure on line 187 in src/Listener/ViewSearchListener.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Missing doc comment for function getPlaceholder()
{
if (str_contains($field, '.')) {
[, $field] = explode('.', $field);
}

if (str_ends_with($field, '_id') && $field !== '_id') {
$field = substr($field, 0, -3);
}

return Inflector::humanize($field);
}
}
12 changes: 6 additions & 6 deletions templates/element/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
<div class="search-filters">
<?php
$searchOptions = $searchOptions ?? [];
$searchOptions += ['class' => 'form-inline', 'id' => 'searchFilter'];
$searchOptions += ['align' => 'inline', 'id' => 'searchFilter'];

echo $this->Form->create(null, $searchOptions);
echo $this->Form->hidden('_search');
?>

<?= $this->Form->controls($searchInputs, ['fieldset' => false]); ?>
<div class="form-group">
<?= $this->Form->button(__d('crud', 'Filter results'), ['type' => 'submit', 'class' => 'btn btn-primary']); ?>
<?php if ($this->Search->isSearch()) : ?>
<?= $this->Form->button(__d('crud', 'Filter results'), ['type' => 'submit', 'class' => 'btn btn-primary']); ?>
<?php if ($this->Search->isSearch()) : ?>
<div class="col-auto">
<?= $this->Search->resetLink(__d('crud', 'Reset'), ['class' => 'btn btn-primary']) ?>
<?php endif ?>
</div>
</div>
<?php endif ?>

<?= $this->Form->end(); ?>
</div>
22 changes: 13 additions & 9 deletions webroot/css/local.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ h2 .actions {
margin-left: 15px;
}

.search-filters .form-inline .form-group label,
.search-filters .form-inline .btn {
margin-right: .25rem !important;

.search-filters .form-inline .col-auto {
margin-bottom: var(--bs-gutter-y) !important;
}

.search-filters .form-inline .form-group {
margin-right: .5rem !important;
.search-filters .form-inline .form-control input {
min-width: 90px !important;
}

.search-filters .form-inline .form-group {
margin-bottom: 1rem !important;
.search-filters .form-inline option {
color: var(--bs-body-color);
font-style: normal;
}

.search-filters .form-inline .form-control input {
min-width: 90px !important;
.search-filters .form-inline select:invalid,
.search-filters .form-inline select:has(option[value=""]:checked),
.search-filters .form-inline option[value=""] {
font-style: italic;
color: #777;
}

.search-filters .form-inline select.autocomplete {
Expand Down

0 comments on commit 8e70739

Please sign in to comment.