-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
log | ||
temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | ||
<script src="https://unpkg.com/[email protected]/bootstrap3-typeahead.min.js"></script> | ||
<title>Autocomplete demo</title> | ||
</head> | ||
<body> | ||
{control form} | ||
<script> | ||
$('.autocomplete').typeahead({ | ||
source: function (query, resultCb) { | ||
var url = this.$element.data('autocomplete-url').replace('__QUERY_PLACEHOLDER__', query); | ||
$.ajax({ | ||
url: url | ||
}).done(function (response) { | ||
resultCb(response); | ||
}); | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace NextrasDemos\FormsComponents\Autocomplete; | ||
|
||
use Nette\Application\UI\Form; | ||
use Nette\Application\UI\Presenter; | ||
use Nette\Utils\Strings; | ||
use Nextras\FormComponents\Controls\AutocompleteControl; | ||
|
||
|
||
class AutocompletePresenter extends Presenter | ||
{ | ||
private static $names = [ | ||
'Olivia', | ||
'Oliver', | ||
'Amelia', | ||
'Harry', | ||
'Isla', | ||
'Jack', | ||
'Emily', | ||
'George', | ||
'Ava', | ||
'Noah', | ||
'Lily', | ||
'Charlie', | ||
'Mia', | ||
'Jacob', | ||
'Sophia', | ||
'Alfie', | ||
'Isabella', | ||
'Freddie', | ||
'Grace', | ||
'Oscar', | ||
]; | ||
|
||
|
||
public function actionDefault() | ||
{ | ||
} | ||
|
||
|
||
public function createComponentForm() | ||
{ | ||
$form = new Form(); | ||
$form['autocomplete'] = new AutocompleteControl( | ||
'Names', | ||
function (string $q) { | ||
return array_values(array_filter(self::$names, function ($name) use ($q) { | ||
return Strings::startsWith(strtolower($name), strtolower($q)); | ||
})); | ||
} | ||
); | ||
return $form; | ||
} | ||
|
||
|
||
public function formatTemplateFiles(): array | ||
{ | ||
return [__DIR__ . '/AutocompletePresenter.latte']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
application: | ||
scanDirs: false | ||
mapping: | ||
*: NextrasDemos\FormsComponents\Autocomplete\*Module\*Presenter | ||
|
||
services: | ||
routing.router: Nette\Application\Routers\SimpleRouter('Autocomplete:default') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace NextrasDemos\FormsComponents\Autocomplete; | ||
|
||
use Nette\Application\Application; | ||
use Nette\Configurator; | ||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
|
||
$configurator = new Configurator; | ||
$configurator->enableDebugger(__DIR__ . '/log'); | ||
$configurator->setTempDirectory(__DIR__ . '/temp'); | ||
$configurator->createRobotLoader()->addDirectory(__DIR__)->register(); | ||
$configurator->addConfig(__DIR__ . '/config.neon'); | ||
|
||
$container = $configurator->createContainer(); | ||
$app = $container->getByType(Application::class); | ||
$app->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Nextras community extensions of Nette Framework | ||
* | ||
* @license MIT | ||
* @link https://github.com/nextras/form-components | ||
*/ | ||
|
||
namespace Nextras\FormComponents\Controls; | ||
|
||
use Nette\Application\IPresenter; | ||
use Nette\InvalidStateException; | ||
use Nette\Utils\Html; | ||
use Nextras\FormComponents\Fragments\UIControl\TextInput; | ||
|
||
|
||
class AutocompleteControl extends TextInput | ||
{ | ||
/** @var callable|null */ | ||
protected $callback; | ||
|
||
|
||
/** | ||
* @param string|object $caption | ||
*/ | ||
public function __construct($caption = null, ?callable $callback = null) | ||
{ | ||
parent::__construct($caption); | ||
if ($callback) { | ||
$this->setCallback($callback); | ||
} | ||
$this->monitor( | ||
IPresenter::class, | ||
function () { | ||
$this->control->{'data-autocomplete-url'} = $this->link('autocomplete!', ['q' => '__QUERY_PLACEHOLDER__']); | ||
} | ||
); | ||
} | ||
|
||
|
||
public function getControl(): Html | ||
{ | ||
$control = parent::getControl(); | ||
$control->addClass('autocomplete'); | ||
return $control; | ||
} | ||
|
||
|
||
public function setCallback(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
|
||
public function handleAutocomplete(string $q) | ||
{ | ||
if (!$this->callback) { | ||
throw new InvalidStateException('Undefined autocomplete callback.'); | ||
} | ||
|
||
$out = call_user_func($this->callback, $q); | ||
$presenter = $this->getPresenter(); | ||
assert($presenter !== null); | ||
$presenter->sendJson($out); | ||
} | ||
} |