advanced_select 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0d986f80cf90e0dc2aa254d9eb0a9b8d76b844f205ae8ca86f815c1a5b17bea
4
- data.tar.gz: 72722ff9f42bbed846afe1ec04e46362e9ec259d96c7bacb171207dd8ccbdc79
3
+ metadata.gz: 9f77cb8280bf5c4483c3d38f73628f8e17f7492f73684d4ced437f903198189b
4
+ data.tar.gz: 1baff77c13609cfc3c98770dbc7a8a6a2d67a2166ff04beca10e64aa7405e68c
5
5
  SHA512:
6
- metadata.gz: d287e5b91c20d5dd629d55f802f1095fd2f016e3f4bdccff8f011fabf0e115914d56563ed00f5219a7a65452d1df4b143a4f2b3e5bf68b8ebb93898c83a7910d
7
- data.tar.gz: 1b9d260a2ace0a2ec7d132581da98e35a09fc26265420655afa40ff290454b716584bb1cf5062a5ce2fa1a432cae22c1da75e411546b3673e88e468b4295910e
6
+ metadata.gz: f184de3a7114166e60dd758b6f489191d9e9b469998c2deacb3446ad79992ddbc661f92dd492366acf718e77072b386b7da39c9d11022fac75b31ebfb9257f1e
7
+ data.tar.gz: c82e8749636458398cf8d9204cca3986bbff17785504833e18b28d87367209b1e67e99e98d2da07471a07429a8648d0237359d002dc480809190b945f6ea0a01
data/README.md CHANGED
@@ -22,6 +22,7 @@ AdvancedSelect is a small Rails engine for rendering an advanced select input wi
22
22
  - [Dependent Fields](#dependent-fields)
23
23
  - [Custom Option Content](#custom-option-content)
24
24
  - [Option Contract](#option-contract)
25
+ - [Events](#events)
25
26
  - [API Reference](#api-reference)
26
27
  - [Local Development](#local-development)
27
28
  - [i18n](#i18n)
@@ -745,6 +746,51 @@ Grouped options use this shape:
745
746
  ]
746
747
  ```
747
748
 
749
+ ### Events
750
+
751
+ Every value change dispatches two events:
752
+
753
+ | Event | Target | Purpose |
754
+ | --- | --- | --- |
755
+ | `change` | the first hidden input | native form behaviour and [dependent fields](#dependent-fields) |
756
+ | `advanced-select:change` | the root element | application listeners that need the whole selection |
757
+
758
+ Both bubble. `advanced-select:change` carries a `detail` payload:
759
+
760
+ ```js
761
+ {
762
+ name: "record[item_ids][]",
763
+ value: ["3", "7"],
764
+ options: [
765
+ { id: "3", value: "3", label: "Item A", display_label: "Item A" },
766
+ { id: "7", value: "7", label: "Item B", display_label: "Item B" }
767
+ ]
768
+ }
769
+ ```
770
+
771
+ `value` is the submit value — the option's `value` when it defines one, otherwise its `id`. It is an
772
+ array for multiple selects, a string for single selects, and empty when nothing is selected. A
773
+ multiple select lists the most recently chosen option first, matching the order of its hidden
774
+ inputs.
775
+
776
+ Bind it like any other event:
777
+
778
+ ```erb
779
+ data-action="advanced-select:change->my-controller#refresh"
780
+ ```
781
+
782
+ Prefer it over `change` on multiple selects. `change` is dispatched from the first hidden input, so
783
+ it does not fire at all once the last value is cleared from a field rendered with
784
+ `include_hidden: false` — at that point the field has no inputs left.
785
+
786
+ The current value is also readable from the controller:
787
+
788
+ ```js
789
+ const select = application.getControllerForElementAndIdentifier(element, "advanced-select")
790
+
791
+ select.currentValue // => ["3", "7"]
792
+ ```
793
+
748
794
  ### API Reference
749
795
 
750
796
  `advanced_select_tag`:
@@ -371,6 +371,21 @@ export default class extends Controller {
371
371
  if (input) {
372
372
  input.dispatchEvent(new Event("change", { bubbles: true }))
373
373
  }
374
+
375
+ this.element.dispatchEvent(new CustomEvent("advanced-select:change", {
376
+ bubbles: true,
377
+ detail: {
378
+ name: this.nameValue,
379
+ value: this.currentValue,
380
+ options: [...this.selectedValue]
381
+ }
382
+ }))
383
+ }
384
+
385
+ get currentValue() {
386
+ const values = this.selectedValue.map((option) => option.value || option.id)
387
+
388
+ return this.multipleValue ? values : (values[0] || "")
374
389
  }
375
390
 
376
391
  renderOptionsState() {
@@ -1,3 +1,3 @@
1
1
  module AdvancedSelect
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
@@ -371,6 +371,21 @@ export default class extends Controller {
371
371
  if (input) {
372
372
  input.dispatchEvent(new Event("change", { bubbles: true }))
373
373
  }
374
+
375
+ this.element.dispatchEvent(new CustomEvent("advanced-select:change", {
376
+ bubbles: true,
377
+ detail: {
378
+ name: this.nameValue,
379
+ value: this.currentValue,
380
+ options: [...this.selectedValue]
381
+ }
382
+ }))
383
+ }
384
+
385
+ get currentValue() {
386
+ const values = this.selectedValue.map((option) => option.value || option.id)
387
+
388
+ return this.multipleValue ? values : (values[0] || "")
374
389
  }
375
390
 
376
391
  renderOptionsState() {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: advanced_select
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Celik