Skip to content

Commit

Permalink
improve textInput filter widget
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Feb 19, 2024
1 parent cb28302 commit 1aa8e28
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
8 changes: 5 additions & 3 deletions config/widgets-themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Yiisoft\Yii\DataView\Column\ActionColumnRenderer;
use Yiisoft\Yii\DataView\Filter\Widget\DropdownFilter;
use Yiisoft\Yii\DataView\Filter\Widget\TextInputFilter;
use Yiisoft\Yii\DataView\GridView;
use Yiisoft\Yii\DataView\KeysetPagination;
use Yiisoft\Yii\DataView\OffsetPagination;
Expand All @@ -27,9 +28,10 @@
],
],
DropdownFilter::class => [
'attributes()' => [[
'class' => 'form-select',
]],
'attributes()' => [['class' => 'form-select']],
],
TextInputFilter::class => [
'attributes()' => [['class' => 'form-control']],
],
OffsetPagination::class => [
'listTag()' => ['ul'],
Expand Down
41 changes: 40 additions & 1 deletion src/Filter/Widget/TextInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,52 @@
namespace Yiisoft\Yii\DataView\Filter\Widget;

use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Input;

final class TextInputFilter extends FilterWidget
{
private Input $input;

/**
* Add a set of attributes to existing tag attributes.
* Same named attributes are replaced.
*
* @param array $attributes Name-value set of attributes.
*
* @see Input::addAttributes()
*/
final public function addAttributes(array $attributes): static
{
$new = clone $this;
$new->input = $this->getInput()->addAttributes($attributes);
return $new;
}

/**
* Replace attributes with a new set.
*
* @param array $attributes Name-value set of attributes.
*
* @see Input::attributes()
*/
final public function attributes(array $attributes): self
{
$new = clone $this;
$new->input = $this->getInput()->attributes($attributes);
return $new;
}

public function renderFilter(Context $context): string
{
return Html::textInput($context->property, $context->value)
return $this->getInput()
->name($context->property)
->value($context->value)
->form($context->formId)
->render();
}

private function getInput(): Input
{
return $this->input ?? Html::textInput();
}
}

0 comments on commit 1aa8e28

Please sign in to comment.