wavevision / dependent-selectbox
Dependent selectbox component for Nette forms.
Installs: 2 644
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 1
Open Issues: 18
Requires
- php: >=7.4
- nette/forms: ^3.0
- wavevision/utils: ^2.1
Requires (Dev)
- latte/latte: ^2.5
- nette/application: ^3.0
- nette/bootstrap: ^3.0
- php-coveralls/php-coveralls: ^2.4
- phpstan/extension-installer: ^1.0
- phpstan/phpstan-nette: ^0.12.9
- phpunit/phpunit: ^9.4
- wavevision/coding-standard: ^6.0
- dev-master
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.10
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-dependabot/npm_and_yarn/json5-1.0.2
- dev-dependabot/npm_and_yarn/qs-6.5.3
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/loader-utils-1.4.2
- dev-dependabot/npm_and_yarn/terser-4.8.1
- dev-dependabot/npm_and_yarn/shell-quote-1.7.3
- dev-dependabot/composer/guzzlehttp/guzzle-7.4.5
- dev-dependabot/composer/guzzlehttp/psr7-2.2.1
- dev-dependabot/npm_and_yarn/minimist-1.2.6
- dev-dependabot/npm_and_yarn/nanoid-3.2.0
- dev-dependabot/composer/latte/latte-2.10.8
- dev-dependabot/npm_and_yarn/tmpl-1.0.5
- dev-dependabot/npm_and_yarn/tar-6.1.11
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-dependabot/npm_and_yarn/postcss-7.0.36
- dev-dependabot/npm_and_yarn/trim-newlines-3.0.1
- dev-dependabot/npm_and_yarn/ws-7.4.6
- dev-dependabot/npm_and_yarn/browserslist-4.16.6
- dev-development
This package is auto-updated.
Last update: 2024-08-07 13:46:25 UTC
README
Dependent SelectBox
Dependent select box component for nette/forms with naja extension on client side.
Features:
- strict typed
- works on any form without customizing its renderer (no need for snippets)
- supports form containers (unlimited depth, both for select box itself and its parents)
- TypeScript and Naja powered client side with events emitted so you can attach listeners to them
- conditional parents (make a form control become a select box's parent when some other control has a certain value)
Installation
Use Composer to get the Nette forms component
composer require wavevision/dependent-selectbox
Install the client side via Yarn
yarn add @wavevision/dependent-selectbox
or npm
npm install --save @wavevision/dependent-selectbox
Usage
Server side
Create a form that will be an instance of Wavevision\DependentSelectBox\Form\Form
.
If you don't want to / cannot inherit from our base form, just make sure the form you create uses
Wavevision\DependentSelectBox\Form\DependentForm
trait.
Your form should also use Wavevision\DependentSelectBox\Form\DependentContainer
to have its containers extended with
the dependent select box too, however, if you already have your own implementation of Nette\Forms\Container
,
just make sure the container uses Wavevision\DependentSelectBox\Form\DependentControl
trait.
After this, your form and its containers will be extended with addDependentSelectBox
method. This method accepts following arguments:
The method returns an instance of Wavevision\DependentSelectBox\DependentSelectBox
.
The recommended way of using the form is in a Control
component.
use Nette\Application\UI\Control; use Nette\Application\UI\Presenter; use Wavevision\DependentSelectBox\DependentComponent; use Wavevision\DependentSelectBox\DependentData; use Wavevision\DependentSelectBox\DependentValues; use Wavevision\DependentSelectBox\Form\Form; class FormComponent extends Control { // add 'loadDependenData' signal and a few utilities use DependentComponent; public function __construct() { $this->monitor(Presenter::class, function (): void { // setup form in component - optionally pass form name (default 'form') $this->dependentComponentSetup(); if ($this->hasReceivedDependentSignal()) { // if 'loadDependenData' signal received, do anything extra we need } }); } protected function createComponentForm(): Form { // create your form as you are used to $form->addDependentSelectBox('name', 'Label', $form['someParentControl']) ->setDependentCallback(function (DependentValues $values): DependentData { // get ArrayHash values, if you perfer array, use getRawValues $formattedValues = $values->getValues(); $data = new DependentData(); if ($formattedValues->someParentControl === 'someDependentValue') { $data->setItems(['firstItem' => 'firstValue']); } return $data; }) // make the select box disabled when no values have been loaded ->setDisabledWhenEmpty() // if loaded values contain only one item, select it so the user does not have to ->setAutoSelectSingleValue() // if 'someOtherControl' has 'someValue', treat 'someControl' as parent ->addConditionalParent($form['someControl'], $form['someOtherControl'], 'someValue'); // add form handlers etc. return $form; } }
NOTE: You can use the form in a
Presenter
too, the only limitation is that you can only have one dependent form per page like this. If you wrap your form in a component (each usingDependentComponent
trait), you can then use as many forms on one page as you wish.
Client side
There are a few ways of integrating the client side into your project.
1. Register dependent select box as Naja extension
import naja from 'naja'; import DependentSelectBox from '@wavevision/dependent-selectbox'; naja.registerExtension(new DependentSelectBox()); // add other extensions, initialize etc.
As mentioned in features, the extension emits events you can attach listeners to.
Listeners for both events will receive a CustomEvent
object containing detail
with these properties:
The first event also contains request
property, which is an object containing:
The latter one contains response
object, in which keys are HTML id
attributes of updated select boxes
and values are objects with:
Please, refer to Naja docs to find out more about its extensions.
2. Import bundled version with Naja included
If you don't use Naja elsewhere in your project an you don't want to set it up, this is your way to go.
⚠️ WARNING: This might collide with other Nette AJAX libraries, if used!
import '@wavevision/dependent-selectbox/dist/dependentSelectBox.all';
3. Use it directly in a script
tag
For old-school people only 😎.
<script src="/path/to/assets/dependentSelectBox.all.js"></script>