Skip to content

Commit

Permalink
added AutocompleteControl
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Feb 10, 2019
1 parent 91c6cf7 commit aee4b5e
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
log
temp
24 changes: 24 additions & 0 deletions examples/autocomplete/AutocompletePresenter.latte
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>
61 changes: 61 additions & 0 deletions examples/autocomplete/AutocompletePresenter.php
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'];
}
}
7 changes: 7 additions & 0 deletions examples/autocomplete/config.neon
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')
19 changes: 19 additions & 0 deletions examples/autocomplete/index.php
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();
67 changes: 67 additions & 0 deletions src/Controls/AutocompleteControl.php
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);
}
}

0 comments on commit aee4b5e

Please sign in to comment.