nepada / autocomplete-input
Autocomplete text input for Nette forms.
Installs: 6 799
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: >=8.1.0 <8.5
- nette/application: ^3.1.4@dev
- nette/forms: ^3.1@dev
- nette/utils: ^3.2@dev || ^4.0@dev
Requires (Dev)
- composer-runtime-api: ^2.0
- composer/semver: 3.4.3
- mockery/mockery: 1.6.12
- nepada/coding-standard: 7.14.0
- nepada/phpstan-nette-tester: 1.2.1
- nette/bootstrap: >=3.1@dev
- nette/component-model: >=3.0.2
- nette/di: ^3.0.6@dev
- nette/http: *@dev
- nette/neon: >=3.3.4@dev
- nette/robot-loader: *@dev
- nette/schema: ^1.0.3@dev
- nette/tester: 2.5.4
- php-parallel-lint/php-parallel-lint: 1.4.0
- phpstan/phpstan: 1.12.5
- phpstan/phpstan-mockery: 1.1.3
- phpstan/phpstan-nette: 1.3.8
- phpstan/phpstan-strict-rules: 1.6.1
- shipmonk/phpstan-rules: 3.2.1
- spaze/phpstan-disallowed-calls: 3.4.0
This package is auto-updated.
Last update: 2024-10-28 14:33:05 UTC
README
Installation
Via Composer:
$ composer require nepada/autocomplete-input
Option A: install form container extension method via DI extension
extensions: - Nepada\Bridges\AutocompleteInputDI\AutocompleteInputExtension
It will register extension method addAutocomplete($name, $label, callable $dataSource): AutocompleteInput
to Nette\Forms\Container
.
Option B: use trait in your base form/container class
You can also use AutocompleteInputMixin
trait in your base form/container class to add method addAutocomplete($name, $label, callable $dataSource): AutocompleteInput
.
Example:
trait FormControls { use Nepada\Bridges\AutocompleteInputForms\AutocompleteInputMixin; public function addContainer($name) { $control = new Container; $control->setCurrentGroup($this->getCurrentGroup()); if ($this->currentGroup !== null) { $this->currentGroup->add($control); } return $this[$name] = $control; } } class Container extends Nette\Forms\Container { use FormControls; } class Form extends Nette\Forms\Form { use FormControls; }
Usage
AutocompleteInput
is a standard text input from Nette enhanced with the autocomplete feature. The input exposes URL to retrieve the entries matching a specified query - you need to pass data source callback when creating the input:
$autocompleteInput = $form->addAutocomplete('foo', 'Foo', function (string $query) { // return entries matching the query }); $autocompleteInput->setAutocompleteMinLength(3); // set minimum input length to trigger autocomplete
Client side
The backend form control is not tightly coupled to any specific client side implementation. The rendered input contains data attributes with all the necessary settings:
<input type="text" name="foo" data-autocomplete-query-placeholder="__QUERY_PLACEHOLDER__" data-autocomplete-url="/some-url/?do=form-foo-autocomplete&form-foo-query=__QUERY_PLACEHOLDER__" data-autocomplete-min-length="3" > <!-- data-autocomplete-min-length is optional -->
If you do not want to roll out your own client side solution, try @nepada/autocomplete-input npm package.