1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui;
6:
7: /**
8: * A Search input field that will reload View
9: * using the view->url with a _q arguments attach to URL.
10: */
11: class JsSearch extends View
12: {
13: public $ui = 'left icon action transparent input';
14: public $defaultTemplate = 'js-search.html';
15:
16: /** @var View The View to reload using this JsSearch. */
17: public $reload;
18:
19: /** @var array */
20: public $args = [];
21:
22: /**
23: * Whether or not JsSearch will query server on each keystroke.
24: * Default is with using Enter key.
25: *
26: * @var bool
27: */
28: public $autoQuery = false;
29:
30: /** @var Form\Control\Line|null The input field. */
31: public $placeHolder = 'Search';
32:
33: /**
34: * The initial value to display in search
35: * input field.
36: * Atn: This is not reloading the view but
37: * rather display in initial input value.
38: * Make sure the model results set match the initial value.
39: * Mostly use when not using ajax reload.
40: *
41: * @var string
42: */
43: public $initValue;
44:
45: /**
46: * Whether or not this search will reload a view
47: * or the entire page.
48: * If search query need to be control via an URL parameter only
49: * set this to false.
50: *
51: * @var bool
52: */
53: public $useAjax = true;
54:
55: /** @var string ui CSS classes */
56: public $button = 'ui mini transparent basic button';
57: /** @var string */
58: public $filterIcon = 'filter';
59: /** @var string */
60: public $buttonSearchIcon = 'search';
61: /** @var string */
62: public $buttonRemoveIcon = 'red remove';
63: /** @var string|null */
64: public $buttonStyle;
65:
66: #[\Override]
67: protected function init(): void
68: {
69: parent::init();
70: }
71:
72: #[\Override]
73: protected function renderView(): void
74: {
75: if ($this->placeHolder) {
76: $this->template->trySet('Placeholder', $this->placeHolder);
77: }
78:
79: if ($this->buttonStyle) {
80: $this->template->trySet('buttonStyle', $this->buttonStyle);
81: }
82:
83: $this->template->set('Button', $this->button);
84: $this->template->set('FilterIcon', $this->filterIcon);
85: $this->template->set('ButtonSearchIcon', $this->buttonSearchIcon);
86: $this->template->set('ButtonRemoveIcon', $this->buttonRemoveIcon);
87:
88: $this->js(true)->atkJsSearch([
89: 'url' => $this->reload->jsUrl(),
90: 'urlOptions' => array_merge(['__atk_reload' => $this->reload->name], $this->args),
91: 'urlQueryKey' => $this->name . '_q',
92: 'autoQuery' => $this->autoQuery,
93: 'q' => $this->initValue,
94: 'useAjax' => $this->useAjax,
95: ]);
96:
97: parent::renderView();
98: }
99: }
100: