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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43e9d7178d6a064d18c4243d1fad3ec15fbb935834eb2d07da8cd89d33c955a1
4
- data.tar.gz: 389f74b51ecd1615a2d3b11c2794e10c57557ce6085595ebe06f3a296b88874a
3
+ metadata.gz: 12aa609bf191df8feb5cdbcbe6c56052cf1e41737bd87d4122b99d65d8788009
4
+ data.tar.gz: d49f89a642d10f28aa141c6ef7728335adbc5168fef7f3ba6aba828f164b2477
5
5
  SHA512:
6
- metadata.gz: faf098b583a4d1ea48aeb788943b3da2400fdca7946f32f02419aef63ca34af371c7ce9cd97368205ff6dcb96e55b5e3044794e5f8f5cf8237faf0e53dd7e62b
7
- data.tar.gz: 5c8fea29044aa4e5a9da1cb804b93f0124e9e302dbc82c0f63d8a216f767e2e13d518c5768704285874596ef6a809750f3033e7ff759176c35a4991f04ee87f0
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.4"
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 &amp; 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
- A custom `tooltip_partial` is rendered once on the server, so it is best for display-oriented content. If your selection changes on the client and the custom tooltip must reflect it live, re-render the field (for example via a Turbo Stream) after the change. The built-in `tooltip: true` list updates client-side automatically.
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 || this.tooltipTarget.hasAttribute("data-advanced-select-tooltip-custom")) {
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
@@ -1,3 +1,3 @@
1
1
  module AdvancedSelect
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
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.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Celik