advanced_select 0.1.5 → 0.1.6
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 +4 -4
- data/README.md +15 -3
- data/app/javascript/advanced_select/advanced_select_controller.js +64 -1
- data/lib/advanced_select/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12aa609bf191df8feb5cdbcbe6c56052cf1e41737bd87d4122b99d65d8788009
|
|
4
|
+
data.tar.gz: d49f89a642d10f28aa141c6ef7728335adbc5168fef7f3ba6aba828f164b2477
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 53132d110065de787848345926574700a2cc2a4b8666d4e0f740c85f4a552950f268378e9aa73d04e1b404612e481f1c364fd3a07cd5a397dfff3c18b6d78122
|
|
7
|
+
data.tar.gz: 3e07f94cffddcdbdeb08576c54903ff5d1a5498db40224f5341a5826c3bacbcad59c9fb5254ccbf36449254702102681c16a6547ff9acc4f0e613bb08c72fe94
|
data/README.md
CHANGED
|
@@ -77,7 +77,7 @@ AdvancedSelect does not provide query objects, model concerns, authorization log
|
|
|
77
77
|
Add the gem to the host Rails app:
|
|
78
78
|
|
|
79
79
|
```ruby
|
|
80
|
-
gem "advanced_select", "~> 0.1.
|
|
80
|
+
gem "advanced_select", "~> 0.1.5"
|
|
81
81
|
```
|
|
82
82
|
|
|
83
83
|
Run the installer:
|
|
@@ -484,13 +484,19 @@ For richer layouts — a table, badges, compatibility columns, etc. — pass `to
|
|
|
484
484
|
) %>
|
|
485
485
|
```
|
|
486
486
|
|
|
487
|
+
The partial is rendered on the server for the initial selection, and the controller keeps it in sync client-side as the selection changes — no Turbo Stream round-trip. To opt in, mark the row container and provide a one-row `<template>` the controller clones per selected option:
|
|
488
|
+
|
|
489
|
+
- `data-advanced-select-tooltip-list` on the element that holds the rows (the same hook the built-in list uses).
|
|
490
|
+
- `<template data-advanced-select-tooltip-template>` containing the markup for a single selected row.
|
|
491
|
+
- `data-advanced-select-tooltip-field="…"` on the elements inside the template that should be filled in. Available fields are `id`, `value`, `label`, and `display_label`; values are written with `textContent` (text only, no markup).
|
|
492
|
+
|
|
487
493
|
```erb
|
|
488
494
|
<%# app/views/alternatives/_tooltip.html.erb %>
|
|
489
495
|
<table>
|
|
490
496
|
<thead>
|
|
491
497
|
<tr><th>Code & Name</th><th>Type</th></tr>
|
|
492
498
|
</thead>
|
|
493
|
-
<tbody>
|
|
499
|
+
<tbody data-advanced-select-tooltip-list>
|
|
494
500
|
<% selected_options.each do |option| %>
|
|
495
501
|
<tr>
|
|
496
502
|
<td><%= option.fetch(:display_label) %></td>
|
|
@@ -498,10 +504,16 @@ For richer layouts — a table, badges, compatibility columns, etc. — pass `to
|
|
|
498
504
|
</tr>
|
|
499
505
|
<% end %>
|
|
500
506
|
</tbody>
|
|
507
|
+
<template data-advanced-select-tooltip-template>
|
|
508
|
+
<tr>
|
|
509
|
+
<td data-advanced-select-tooltip-field="display_label"></td>
|
|
510
|
+
<td data-advanced-select-tooltip-field="value"></td>
|
|
511
|
+
</tr>
|
|
512
|
+
</template>
|
|
501
513
|
</table>
|
|
502
514
|
```
|
|
503
515
|
|
|
504
|
-
|
|
516
|
+
The initial server-rendered rows stay until the first change, then the controller rebuilds them from the template — so just like `tooltip: true`, the custom tooltip updates client-side automatically. A custom partial without a `<template>` still renders once and stays static, which is fine for display-only content.
|
|
505
517
|
|
|
506
518
|
### Add Mode
|
|
507
519
|
|
|
@@ -526,10 +526,19 @@ export default class extends Controller {
|
|
|
526
526
|
}
|
|
527
527
|
|
|
528
528
|
renderTooltip() {
|
|
529
|
-
if (!this.hasTooltipTarget
|
|
529
|
+
if (!this.hasTooltipTarget) {
|
|
530
530
|
return
|
|
531
531
|
}
|
|
532
532
|
|
|
533
|
+
if (this.tooltipTarget.hasAttribute("data-advanced-select-tooltip-custom")) {
|
|
534
|
+
this.renderCustomTooltip()
|
|
535
|
+
return
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
this.renderBuiltInTooltip()
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
renderBuiltInTooltip() {
|
|
533
542
|
const list = this.tooltipTarget.querySelector("[data-advanced-select-tooltip-list]")
|
|
534
543
|
if (!list) {
|
|
535
544
|
return
|
|
@@ -544,6 +553,60 @@ export default class extends Controller {
|
|
|
544
553
|
}
|
|
545
554
|
}
|
|
546
555
|
|
|
556
|
+
renderCustomTooltip() {
|
|
557
|
+
const list = this.tooltipTarget.querySelector("[data-advanced-select-tooltip-list]")
|
|
558
|
+
const template = this.customTooltipTemplate()
|
|
559
|
+
|
|
560
|
+
if (!list || !template) {
|
|
561
|
+
if (this.selectedValue.length === 0) {
|
|
562
|
+
this.hideTooltip(true)
|
|
563
|
+
}
|
|
564
|
+
return
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
list.replaceChildren(
|
|
568
|
+
...this.selectedValue.map((option) => this.customTooltipElement(template, option))
|
|
569
|
+
)
|
|
570
|
+
|
|
571
|
+
if (this.selectedValue.length === 0) {
|
|
572
|
+
this.hideTooltip(true)
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
customTooltipTemplate() {
|
|
577
|
+
if (this.customTooltipTemplateContent) {
|
|
578
|
+
return this.customTooltipTemplateContent
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const template = this.tooltipTarget.querySelector("template[data-advanced-select-tooltip-template]")
|
|
582
|
+
if (!template) {
|
|
583
|
+
return null
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
this.customTooltipTemplateContent = template.content.cloneNode(true)
|
|
587
|
+
template.remove()
|
|
588
|
+
return this.customTooltipTemplateContent
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
customTooltipElement(template, option) {
|
|
592
|
+
const fragment = template.cloneNode(true)
|
|
593
|
+
fragment.querySelectorAll("[data-advanced-select-tooltip-field]").forEach((element) => {
|
|
594
|
+
element.textContent = this.tooltipFieldValue(option, element.dataset.advancedSelectTooltipField)
|
|
595
|
+
})
|
|
596
|
+
|
|
597
|
+
return fragment
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
tooltipFieldValue(option, field) {
|
|
601
|
+
if (field === "display_label" || field === "displayLabel") {
|
|
602
|
+
return this.displayLabel(option)
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const camelizedField = field.replace(/_([a-z])/g, (_match, character) => character.toUpperCase())
|
|
606
|
+
const value = option[field] ?? option[camelizedField]
|
|
607
|
+
return value === undefined || value === null ? "" : value.toString()
|
|
608
|
+
}
|
|
609
|
+
|
|
547
610
|
textElement(tagName, className, text) {
|
|
548
611
|
const element = document.createElement(tagName)
|
|
549
612
|
element.className = className
|