playbook_ui 14.14.0.pre.alpha.play1755pbcontenttag6464 → 14.14.0.pre.alpha.play1755pbcontenttag6477

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: 4a0dfdbe65861c574c5ae3d71fe1b145da7b3242135041cb8f4dfa204fb8a1d0
4
- data.tar.gz: 66ac870918f75471d9b3320be3c72cdedd1b110bbdcb520c75e2bd3850fa4740
3
+ metadata.gz: 8ef906cdde886008e185c13657433fa51118dcc3bfa87c710f34d9047e2d1b8f
4
+ data.tar.gz: b737b42a1fb810c3c03130f79ca450ef0afcc073f4c8e5118ea2ddd848fb5f8b
5
5
  SHA512:
6
- metadata.gz: 6dcf36d3f70ec676e2f309170afe4e16e3a577e494a7d68c47add383970de0b5ca5ec8f33047973b936341d3bb1d025d68f8ed17984b7aec65f86828cbd0a544
7
- data.tar.gz: f01907111bf029a002058b95af1c5118f539904adcf140b4d3cdebd1d0a97374d785744b9a54b1881df23e1b93333d447502c72e6261ba645af8da8bb13f7e87
6
+ metadata.gz: 2bb8e2859b564ab83a8043ce2cdeb03643a46f2a11de162d4d924d038669abcaee163f76e24f45ac1ecc85a047380248e77c739049f69797d53dd4d4185286c5
7
+ data.tar.gz: 22ffeb839f0d4e54803860e19d092002b6faa2f89710f231e704cc21142b35e70b2fb8b775b7fd0929c7f15eb9a1d116fc1f216c993cb3f39dc71add6ff60b78
@@ -1,8 +1,6 @@
1
1
  <%= pb_content_tag(:div,
2
- aria: object.aria,
3
- data: object.data,
4
- class: object.classnames,
5
- **combined_html_options) do %>
2
+ id: nil,
3
+ class: object.classnames ) do %>
6
4
  <% if object.label %>
7
5
  <label class="pb_select_kit_label" for="<%= object.name %>">
8
6
  <%= pb_rails("caption", props: { text: object.label, dark: object.dark }) %>
@@ -1,4 +1,4 @@
1
- import React from 'react'
1
+ import React, { useState, useEffect} from 'react'
2
2
  import Select from 'react-select'
3
3
  import AsyncSelect from 'react-select/async'
4
4
  import CreateableSelect from 'react-select/creatable'
@@ -45,8 +45,12 @@ type TypeaheadProps = {
45
45
  getOptionLabel?: string | (() => string),
46
46
  getOptionValue?: string | (() => string),
47
47
  name?: string,
48
+ options?: Array<{ label: string; value?: string }>,
48
49
  marginBottom?: "none" | "xxs" | "xs" | "sm" | "md" | "lg" | "xl",
49
50
  pillColor?: "primary" | "neutral" | "success" | "warning" | "error" | "info" | "data_1" | "data_2" | "data_3" | "data_4" | "data_5" | "data_6" | "data_7" | "data_8" | "windows" | "siding" | "roofing" | "doors" | "gutters" | "solar" | "insulation" | "accessories",
51
+ optionsByContext?: Record<string, Array<{ label: string; value?: string }>>
52
+ searchContextSelector?: string,
53
+ clearOnContextChange?: boolean,
50
54
  } & GlobalProps
51
55
 
52
56
  export type SelectValueType = {
@@ -81,6 +85,9 @@ const Typeahead = ({
81
85
  loadOptions = noop,
82
86
  marginBottom = "sm",
83
87
  pillColor,
88
+ optionsByContext = {},
89
+ searchContextSelector,
90
+ clearOnContextChange = false,
84
91
  ...props
85
92
  }: TypeaheadProps) => {
86
93
  const selectProps = {
@@ -115,6 +122,32 @@ const Typeahead = ({
115
122
  ...props,
116
123
  }
117
124
 
125
+ const [contextValue, setContextValue] = useState("")
126
+
127
+ useEffect(() => {
128
+ if (searchContextSelector) {
129
+ const searchContextElement = document.getElementById(searchContextSelector)
130
+
131
+ setContextValue((searchContextElement as HTMLInputElement)?.value)
132
+ const handleContextChange = (e: Event) => {
133
+ const target = e.target as HTMLInputElement;
134
+ setContextValue(target.value);
135
+ if (clearOnContextChange) document.dispatchEvent(new CustomEvent(`pb-typeahead-kit-${selectProps.id}:clear`))
136
+ }
137
+
138
+ if (searchContextElement) searchContextElement.addEventListener('change', handleContextChange)
139
+
140
+ return () => {
141
+ if (searchContextElement) searchContextElement.removeEventListener('change', handleContextChange)
142
+ }
143
+ }
144
+ }, [searchContextSelector])
145
+
146
+ const contextArray = optionsByContext[contextValue]
147
+ if (Array.isArray(contextArray) && contextArray.length > 0) {
148
+ selectProps.options = contextArray
149
+ }
150
+
118
151
  const Tag = (
119
152
  createable
120
153
  ? (async ? AsyncCreateableSelect : CreateableSelect)
@@ -0,0 +1,45 @@
1
+ <%= pb_rails("select", props: {
2
+ id:"color_context_2",
3
+ label: "Choose a Color",
4
+ name: "color_name",
5
+ options: [
6
+ { value: "red", value_text: "Red" },
7
+ { value: "blue", value_text: "Blue" },
8
+ { value: "green", value_text: "Green" }
9
+ ],
10
+ }) %>
11
+
12
+ <%= pb_rails("typeahead", props: {
13
+ label: "Pick a Shade",
14
+ is_multi: false,
15
+ search_context_selector: "color_context_2",
16
+ options_by_context: {
17
+ "red": [
18
+ { label: "Scarlet", value: "scarlet" },
19
+ { label: "Mahogany", value: "mahogany" },
20
+ { label: "Crimson", value: "crimson" }
21
+ ],
22
+ "blue": [
23
+ { label: "Sky Blue", value: "sky" },
24
+ { label: "Cerulean", value: "cerulean" },
25
+ { label: "Navy", value: "navy" }
26
+ ],
27
+ "green": [
28
+ { label: "Emerald", value: "emerald" },
29
+ { label: "Mint", value: "mint" },
30
+ { label: "Olive", value: "olive" }
31
+ ]
32
+ },
33
+ id: "typeahead-dynamic-options",
34
+ }) %>
35
+
36
+
37
+ <%= javascript_tag defer: "defer" do %>
38
+ document.addEventListener("pb-typeahead-kit-typeahead-dynamic-options-result-option-select", function(event) {
39
+ console.log('Single Option selected')
40
+ console.dir(event.detail)
41
+ })
42
+ document.addEventListener("pb-typeahead-kit-typeahead-dynamic-options-result-clear", function() {
43
+ console.log('All options cleared')
44
+ })
45
+ <% end %>
@@ -0,0 +1,5 @@
1
+ You can also set up a typeahead to render options dynamically based on input from a select. To achieve this:
2
+ - The typeahead must have a unique `id`
3
+ - Use the `search_context_selector` prop on the typeahead. The value here must match the id of the select so the Typeahead knows where to read the current "context" from.
4
+ - Use `options_by_context` to pass in a hash whose keys match the possible values of your “context” select. Each key maps to an array of { label, value } objects. The typeahead automatically displays only the subset of options matching the current context.
5
+ - Additionally, the optional `clear_on_context_change` prop controls whether the typeahead clears or not when a change happens in the linked select. This prop is set to true by default so that whenever a selection is made in the select, the Typeahead automatically clears its current input/selection.
@@ -0,0 +1,33 @@
1
+ <%= pb_rails("select", props: {
2
+ id:"color_context",
3
+ label: "Choose a Color",
4
+ name: "color_name_2",
5
+ options: [
6
+ { value: "red", value_text: "Red" },
7
+ { value: "blue", value_text: "Blue" },
8
+ { value: "green", value_text: "Green" }
9
+ ],
10
+ }) %>
11
+
12
+ <%= pb_rails("typeahead", props: {
13
+ label: "Pick a Shade",
14
+ search_context_selector: "color_context",
15
+ options_by_context: {
16
+ "red": [
17
+ { label: "Scarlet", value: "scarlet" },
18
+ { label: "Mahogany", value: "mahogany" },
19
+ { label: "Crimson", value: "crimson" }
20
+ ],
21
+ "blue": [
22
+ { label: "Sky Blue", value: "sky" },
23
+ { label: "Cerulean", value: "cerulean" },
24
+ { label: "Navy", value: "navy" }
25
+ ],
26
+ "green": [
27
+ { label: "Emerald", value: "emerald" },
28
+ { label: "Mint", value: "mint" },
29
+ { label: "Olive", value: "olive" }
30
+ ]
31
+ },
32
+ search_term_minimum_length: 0,
33
+ }) %>
@@ -0,0 +1,3 @@
1
+ The dynamic rendering of options for the typeahead can also be achieved with a pure Rails implementation (not react rendered). For this implementation, use all the props as above with the following additions:
2
+
3
+ - `search_term_minimum_length`: this sets the minimum input in the typeahead needed to display the dropdown. This is set to 3 by default. Set it to 0 for the dropdown to always display when the typeahead is interacted with.
@@ -12,6 +12,8 @@ examples:
12
12
  - typeahead_margin_bottom: Margin Bottom
13
13
  - typeahead_with_pills_color: With Pills (Custom Color)
14
14
  - typeahead_truncated_text: Truncated Text
15
+ - typeahead_dynamic_options: Dynamic Options
16
+ - typeahead_dynamic_options_pure_rails: Dynamic Options (Pure Rails)
15
17
 
16
18
  react:
17
19
  - typeahead_default: Default
@@ -20,6 +20,34 @@ export default class PbTypeahead extends PbEnhancedElement {
20
20
  this.searchInput.addEventListener('focus', () => this.debouncedSearch())
21
21
  this.searchInput.addEventListener('input', () => this.debouncedSearch())
22
22
  this.resultsElement.addEventListener('click', (event: MouseEvent) => this.optionSelected(event))
23
+
24
+ if (this.clearOnContextChange && this.searchContextElement) {
25
+ this.searchContextElement.addEventListener('change', () => {
26
+ this.searchInputClear()
27
+ this.resultsCacheClear()
28
+ this.clearResults()
29
+ })
30
+ }
31
+ }
32
+
33
+ get optionsByContext() {
34
+ return (this.element as HTMLElement).dataset.pbTypeaheadKitOptionsByContext
35
+ ? JSON.parse((this.element as HTMLElement).dataset.pbTypeaheadKitOptionsByContext)
36
+ : null
37
+ }
38
+
39
+ get searchContextElement() {
40
+ const selector = (this.element as HTMLElement).dataset.pbTypeaheadKitSearchContextSelector
41
+ if (!selector) return null
42
+
43
+ const found = this.element.parentNode?.querySelector(`#${selector}`)
44
+ || this.element.closest(selector)
45
+
46
+ return found || null
47
+ }
48
+
49
+ get clearOnContextChange() {
50
+ return (this.element as HTMLElement).dataset.pbTypeaheadKitClearOnContextChange === 'true'
23
51
  }
24
52
 
25
53
  handleKeydown(event: KeyboardEvent) {
@@ -40,14 +68,32 @@ export default class PbTypeahead extends PbEnhancedElement {
40
68
 
41
69
  const searchTerm = this.searchTerm
42
70
  const searchContext = this.searchContext
43
- const search = {
44
- searchingFor: searchTerm,
45
- searchingContext: searchContext,
46
- setResults: (results: Array<DocumentFragment>) => {
47
- this.resultsCacheUpdate(searchTerm, searchContext, results)
48
- },
71
+
72
+ if (this.optionsByContext && Object.keys(this.optionsByContext).length > 0) {
73
+ const contextArray = this.optionsByContext[searchContext] || []
74
+
75
+ const filteredResults = contextArray.filter((obj: { label: string, value: string }) => {
76
+ return obj.label
77
+ && obj.label.toLowerCase().includes(searchTerm.toLowerCase())
78
+ })
79
+
80
+ const optionFragments = filteredResults.map((obj: { label: string, value: string }) => {
81
+ const singleOption = document.createDocumentFragment()
82
+ singleOption.appendChild(document.createTextNode(obj.label))
83
+ return singleOption
84
+ })
85
+
86
+ this.resultsCacheUpdate(searchTerm, searchContext, optionFragments)
87
+ } else {
88
+ const search = {
89
+ searchingFor: searchTerm,
90
+ searchingContext: searchContext,
91
+ setResults: (results: Array<DocumentFragment>) => {
92
+ this.resultsCacheUpdate(searchTerm, searchContext, results)
93
+ },
94
+ }
95
+ this.element.dispatchEvent(new CustomEvent('pb-typeahead-kit-search', { bubbles: true, detail: search }))
49
96
  }
50
- this.element.dispatchEvent(new CustomEvent('pb-typeahead-kit-search', { bubbles: true, detail: search }))
51
97
  }
52
98
 
53
99
  resultsCacheUpdate(searchTerm: string, searchContext: string, results: Array<DocumentFragment>) {
@@ -87,11 +133,15 @@ export default class PbTypeahead extends PbEnhancedElement {
87
133
  const resultOption = (event.target as Element).closest('[data-result-option-item]')
88
134
  if (!resultOption) return
89
135
 
136
+ const selectedText = resultOption.textContent.trim()
137
+
90
138
  this._validSelection = true
91
139
  this.removeValidationError()
92
140
 
141
+ if (this.searchContextElement) this.searchInput.value = selectedText
142
+
93
143
  this.resultsCacheClear()
94
- this.searchInputClear()
144
+ if (!this.searchContextElement) this.searchInputClear()
95
145
  this.clearResults()
96
146
 
97
147
  this.element.dispatchEvent(new CustomEvent('pb-typeahead-kit-result-option-selected', { bubbles: true, detail: { selected: resultOption, typeahead: this } }))
@@ -173,6 +223,9 @@ export default class PbTypeahead extends PbEnhancedElement {
173
223
  this.element.parentNode.querySelector(selector) ||
174
224
  this.element.closest(selector)
175
225
  ) as HTMLInputElement).value
226
+ else if (this.searchContextElement) {
227
+ return (this.searchContextElement as HTMLInputElement).value
228
+ }
176
229
 
177
230
  return null
178
231
  }
@@ -44,6 +44,12 @@ module Playbook
44
44
  default: false
45
45
  prop :validation, type: Playbook::Props::HashProp,
46
46
  default: {}
47
+ prop :clear_on_context_change, type: Playbook::Props::Boolean,
48
+ default: true
49
+ prop :options_by_context, type: Playbook::Props::HashProp,
50
+ default: {}
51
+ prop :search_context_selector, type: Playbook::Props::String,
52
+ default: nil
47
53
 
48
54
  def classname
49
55
  default_margin_bottom = margin_bottom.present? ? "" : " mb_sm"
@@ -58,7 +64,10 @@ module Playbook
58
64
  Hash(values[:data]).merge(
59
65
  pb_typeahead_kit: true,
60
66
  pb_typeahead_kit_search_term_minimum_length: search_term_minimum_length,
61
- pb_typeahead_kit_search_debounce_timeout: search_debounce_timeout
67
+ pb_typeahead_kit_search_debounce_timeout: search_debounce_timeout,
68
+ pb_typeahead_kit_clear_on_context_change: clear_on_context_change,
69
+ pb_typeahead_kit_search_context_selector: search_context_selector,
70
+ pb_typeahead_kit_options_by_context: options_by_context.to_json
62
71
  )
63
72
  end
64
73
 
@@ -85,6 +94,9 @@ module Playbook
85
94
  placeholder: placeholder,
86
95
  plusIcon: plus_icon,
87
96
  truncate: truncate,
97
+ searchContextSelector: search_context_selector,
98
+ optionsByContext: options_by_context,
99
+ clearOnContextChange: clear_on_context_change,
88
100
  }
89
101
 
90
102
  base_options[:getOptionLabel] = get_option_label if get_option_label.present?