playbook_ui 15.3.0.pre.alpha.PLAY2001selectablecarddisabledstyles11833 → 15.3.0.pre.alpha.PLAY2012currencyallownonstring11972

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/app/pb_kits/playbook/pb_advanced_table/Hooks/useTableState.ts +15 -0
  3. data/app/pb_kits/playbook/pb_advanced_table/docs/_advanced_table_pagination_with_props.jsx +4 -1
  4. data/app/pb_kits/playbook/pb_advanced_table/docs/_advanced_table_pagination_with_props.md +3 -2
  5. data/app/pb_kits/playbook/pb_currency/_currency.tsx +20 -7
  6. data/app/pb_kits/playbook/pb_currency/currency.rb +35 -8
  7. data/app/pb_kits/playbook/pb_currency/currency.test.js +47 -0
  8. data/app/pb_kits/playbook/pb_currency/docs/_currency_variants.html.erb +1 -1
  9. data/app/pb_kits/playbook/pb_currency/docs/_currency_variants.jsx +1 -1
  10. data/app/pb_kits/playbook/pb_currency/docs/_currency_variants.md +1 -0
  11. data/app/pb_kits/playbook/pb_date_picker/date_picker.rb +2 -0
  12. data/app/pb_kits/playbook/pb_icon_circle/docs/_icon_circle_color.md +1 -1
  13. data/app/pb_kits/playbook/pb_icon_stat_value/docs/_icon_stat_value_color.html.erb +7 -14
  14. data/app/pb_kits/playbook/pb_icon_stat_value/docs/_icon_stat_value_color.jsx +6 -15
  15. data/app/pb_kits/playbook/pb_popover/_popover.tsx +69 -34
  16. data/app/pb_kits/playbook/pb_popover/docs/_popover_append_to.jsx +68 -0
  17. data/app/pb_kits/playbook/pb_popover/docs/_popover_append_to_react.md +1 -0
  18. data/app/pb_kits/playbook/pb_popover/docs/example.yml +1 -0
  19. data/app/pb_kits/playbook/pb_popover/docs/index.js +1 -0
  20. data/app/pb_kits/playbook/pb_popover/popover.test.js +80 -0
  21. data/app/pb_kits/playbook/pb_table/docs/_table_with_dynamic_collapsible.html.erb +63 -0
  22. data/app/pb_kits/playbook/pb_table/docs/_table_with_dynamic_collapsible.jsx +89 -0
  23. data/app/pb_kits/playbook/pb_table/docs/_table_with_dynamic_collapsible_rails.md +4 -0
  24. data/app/pb_kits/playbook/pb_table/docs/_table_with_dynamic_collapsible_react.md +3 -0
  25. data/app/pb_kits/playbook/pb_table/docs/example.yml +2 -0
  26. data/app/pb_kits/playbook/pb_table/docs/index.js +1 -0
  27. data/app/pb_kits/playbook/pb_text_input/text_input.rb +2 -2
  28. data/app/pb_kits/playbook/pb_typeahead/_typeahead.test.jsx +41 -0
  29. data/app/pb_kits/playbook/pb_typeahead/_typeahead.tsx +23 -9
  30. data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_custom_options.html.erb +64 -0
  31. data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_custom_options.jsx +70 -0
  32. data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_custom_options.md +1 -0
  33. data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_default_options.html.erb +67 -1
  34. data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_default_value.jsx +68 -6
  35. data/app/pb_kits/playbook/pb_typeahead/docs/example.yml +2 -0
  36. data/app/pb_kits/playbook/pb_typeahead/docs/index.js +2 -1
  37. data/dist/chunks/{_line_graph-h5H-imfn.js → _line_graph-CqE0-dq5.js} +1 -1
  38. data/dist/chunks/_typeahead-3ZAbZUqU.js +6 -0
  39. data/dist/chunks/{_weekday_stacked-c6_R08J-.js → _weekday_stacked-DFwAiApZ.js} +2 -2
  40. data/dist/chunks/vendor.js +1 -1
  41. data/dist/playbook-doc.js +2 -2
  42. data/dist/playbook-rails-react-bindings.js +1 -1
  43. data/dist/playbook-rails.js +1 -1
  44. data/lib/playbook/version.rb +1 -1
  45. metadata +16 -6
  46. data/dist/chunks/_typeahead-U8AjZIIW.js +0 -6
  47. /data/app/pb_kits/playbook/pb_popover/docs/{_popover_append_to.md → _popover_append_to_rails.md} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d57319fd1abfd566b44b950dbb1ce2417ee14f3dca4f3f6d8e2d2976172f5e78
4
- data.tar.gz: 459cbc5ae9b3a9b00e443ffcf761f166b1d1fe153c3e89d0374c7397e7af323e
3
+ metadata.gz: 42d45c331ca506d6fccb7e7314717f32162b05f1a731f2116b967a9ec778d033
4
+ data.tar.gz: 26dd29ee31ee473718cdce8a5650650b548567f9f032743094e101021540b49c
5
5
  SHA512:
6
- metadata.gz: 146f79a6bca12c2806e2acf410dc602b99b5856db20b5dfb544088542ecac8f785ed0060a0f4b81595b386bee7eec341671a81e5928f2d073c1776a1f0cb4ce9
7
- data.tar.gz: 732b20d194e5f62ee7ab1fd14b6185e74c6197d5c918c1d03d881fc0b0b26f16c2ca3c10b68a14125a78c5aa6a887883015bf0aa5538c8ecd4cc28775b79739f
6
+ metadata.gz: b29d46b6fad0bee27efc45f4a24af0d4e3771e92b162c96ad88c46dabf5802bfe4998d7d062128746a802b596d5fae29ec00b80434e33c49673477acd7c5aa54
7
+ data.tar.gz: 39734ec58552ba6b7ffc907ee0cb9ddb2158dbca3183cc35218d377e4d22f268d5c7cf4191b2f676837e54a893cb09301a99f58ea4efae4aa9aac97da2db0d19
@@ -250,6 +250,21 @@ export function useTableState({
250
250
  }
251
251
  }, [pagination, inlineRowLoading, paginationProps?.pageIndex, paginationProps?.pageSize]);
252
252
 
253
+ // Call the callback with the new page index when localPagination.pageIndex changes (with inlineRowLoading)
254
+ useEffect(() => {
255
+ if (pagination && inlineRowLoading && paginationProps?.onPageChange) {
256
+ paginationProps.onPageChange(localPagination.pageIndex);
257
+ }
258
+ }, [localPagination.pageIndex, pagination, inlineRowLoading, paginationProps]);
259
+
260
+ // Call the callback with the new page index when localPagination.pageIndex changes (without inlineRowLoading)
261
+ useEffect(() => {
262
+ if (pagination && !inlineRowLoading && paginationProps?.onPageChange) {
263
+ const currentPageIndex = table.getState().pagination.pageIndex;
264
+ paginationProps.onPageChange(currentPageIndex);
265
+ }
266
+ }, [table.getState().pagination.pageIndex, pagination, inlineRowLoading, paginationProps]);
267
+
253
268
  // Check if table has any sub-rows
254
269
  const hasAnySubRows = table.getRowModel().rows.some(row => row.subRows && row.subRows.length > 0);
255
270
  const selectedRowsLength = Object.keys(table.getState().rowSelection).length;
@@ -38,7 +38,10 @@ const AdvancedTablePaginationWithProps = (props) => {
38
38
  const paginationProps = {
39
39
  pageIndex: 1,
40
40
  pageSize: 10,
41
- range: 2
41
+ range: 2,
42
+ onPageChange: (pageIndex) => {
43
+ console.log('Current page index:', pageIndex);
44
+ }
42
45
  }
43
46
 
44
47
  return (
@@ -1,5 +1,6 @@
1
- `paginationProps` is an optional prop that can be used to further customize pagination for the AdvancedTable. This prop is an object with 2 optional items:
1
+ `paginationProps` is an optional prop that can be used to further customize pagination for the AdvancedTable. This prop is an object with the following optional items:
2
2
 
3
3
  - `pageIndex`: An optional prop to set which page is set to open on initial load. Default is '0'
4
4
  - `pageSize`: An optional prop to set total number of rows for each page before the Table paginates. Default is '20'
5
- - `range`: The range prop determines how many pages to display in the Pagination component. Regardless of this value, the first two and last two pages are always visible to facilitate navigation to the beginning and end of the pagination. If these always-visible pages fall within the specified range, they are included in the display. If they fall outside the range, the pagination will show additional pages up to the number defined by the range prop. See [here for more details](https://playbook.powerapp.cloud/kits/pagination/react#default). Default is set to '5'
5
+ - `range`: The range prop determines how many pages to display in the Pagination component. Regardless of this value, the first two and last two pages are always visible to facilitate navigation to the beginning and end of the pagination. If these always-visible pages fall within the specified range, they are included in the display. If they fall outside the range, the pagination will show additional pages up to the number defined by the range prop. See [here for more details](https://playbook.powerapp.cloud/kits/pagination/react#default). Default is set to '5'
6
+ - `onPageChange`: A callback function that gives to access to the current page index.
@@ -11,7 +11,7 @@ import Title from '../pb_title/_title'
11
11
  type CurrencyProps = {
12
12
  abbreviate?: boolean,
13
13
  align?: 'center' | 'left' | 'right',
14
- amount: string,
14
+ amount: string | number,
15
15
  aria?: {[key:string]:string},
16
16
  className?: string,
17
17
  dark?: boolean,
@@ -59,6 +59,19 @@ const Currency = (props: CurrencyProps): React.ReactElement => {
59
59
  commaSeparator = false,
60
60
  } = props
61
61
 
62
+ // Convert numeric input to string format
63
+ const convertAmount = (input: string | number): string => {
64
+ if (typeof input === 'number') {
65
+ if (input === 0 && !nullDisplay) {
66
+ return ""
67
+ }
68
+ return input.toFixed(2)
69
+ }
70
+ return input
71
+ }
72
+
73
+ const currencyAmount = convertAmount(amount)
74
+
62
75
  const emphasizedClass = emphasized ? '' : '_deemphasized'
63
76
 
64
77
  let variantClass
@@ -68,7 +81,7 @@ const Currency = (props: CurrencyProps): React.ReactElement => {
68
81
  variantClass = '_bold'
69
82
  }
70
83
 
71
- const [whole, decimal = '00'] = amount.split('.')
84
+ const [whole, decimal = '00'] = currencyAmount.split('.')
72
85
  const ariaProps = buildAriaProps(aria)
73
86
  const dataProps = buildDataProps(data)
74
87
  const htmlProps = buildHtmlProps(htmlOptions)
@@ -92,19 +105,19 @@ const Currency = (props: CurrencyProps): React.ReactElement => {
92
105
  return isAmount ? num.slice(0, -1) : isUnit ? num.slice(-1) : ''
93
106
  }
94
107
 
95
- const getMatchingDecimalAmount = decimals === "matching" ? amount : whole
108
+ const getMatchingDecimalAmount = decimals === "matching" ? currencyAmount : whole
96
109
  const getMatchingDecimalValue = decimals === "matching" ? '' : `.${decimal}`
97
110
 
98
111
  const formatAmount = (amount: string) => {
99
112
  if (!commaSeparator) return amount;
100
-
113
+
101
114
  const [wholePart, decimalPart] = amount.split('.');
102
115
  const formattedWhole = new Intl.NumberFormat('en-US').format(parseInt(wholePart));
103
116
  return decimalPart ? `${formattedWhole}.${decimalPart}` : formattedWhole;
104
117
  }
105
118
 
106
119
  const swapNegative = size === "sm" && symbol !== ""
107
- const handleNegative = amount.startsWith("-") && swapNegative ? "-" : ""
120
+ const handleNegative = currencyAmount.startsWith("-") && swapNegative ? "-" : ""
108
121
  const getAbsoluteAmount = (amountString: string) => amountString.replace(/^-/,'')
109
122
  const getAbbrOrFormatAmount = abbreviate ? getAbbreviatedValue('amount') : formatAmount(getMatchingDecimalAmount)
110
123
  const getAmount = swapNegative ? getAbsoluteAmount(getAbbrOrFormatAmount) : getAbbrOrFormatAmount
@@ -152,7 +165,7 @@ const Currency = (props: CurrencyProps): React.ReactElement => {
152
165
  >
153
166
  {handleNegative}{symbol}
154
167
  </Body>
155
-
168
+
156
169
  <Title
157
170
  className="pb_currency_value"
158
171
  dark={dark}
@@ -160,7 +173,7 @@ const Currency = (props: CurrencyProps): React.ReactElement => {
160
173
  >
161
174
  {getAmount}
162
175
  </Title>
163
-
176
+
164
177
  <Body
165
178
  className="unit"
166
179
  color="light"
@@ -17,8 +17,7 @@ module Playbook
17
17
  prop :symbol, type: Playbook::Props::String,
18
18
  default: "$"
19
19
 
20
- prop :amount, type: Playbook::Props::String,
21
- required: true
20
+ prop :amount, required: true
22
21
 
23
22
  prop :unit, type: Playbook::Props::String,
24
23
  required: false
@@ -92,7 +91,7 @@ module Playbook
92
91
  end
93
92
 
94
93
  def negative_sign
95
- amount.starts_with?("-") && swap_negative ? "-" : ""
94
+ currency_amount.starts_with?("-") && swap_negative ? "-" : ""
96
95
  end
97
96
 
98
97
  def body_props
@@ -117,10 +116,32 @@ module Playbook
117
116
  end
118
117
  end
119
118
 
119
+ def currency_amount
120
+ @currency_amount ||= convert_amount(amount)
121
+ end
122
+
120
123
  private
121
124
 
125
+ # Convert numeric input to string format
126
+ def convert_amount(input)
127
+ if input.is_a?(Numeric)
128
+ if input.zero? && null_display.nil?
129
+ ""
130
+ else
131
+ format("%.2f", input)
132
+ end
133
+ # Handle string representations of zero
134
+ elsif input.to_s.strip.match?(/^-?0+(\.0+)?$/) && null_display.nil?
135
+ ""
136
+ else
137
+ input.to_s
138
+ end
139
+ end
140
+
122
141
  def whole_value
123
- value = amount.split(".").first
142
+ return "" if currency_amount.blank?
143
+
144
+ value = currency_amount.split(".").first
124
145
  if comma_separator
125
146
  number_with_delimiter(value.gsub(",", ""))
126
147
  else
@@ -129,7 +150,9 @@ module Playbook
129
150
  end
130
151
 
131
152
  def decimal_value
132
- amount.split(".")[1] || "00"
153
+ return "00" if currency_amount.blank?
154
+
155
+ currency_amount.split(".")[1] || "00"
133
156
  end
134
157
 
135
158
  def units_element
@@ -147,7 +170,9 @@ module Playbook
147
170
  end
148
171
 
149
172
  def abbreviated_value(index = 0..-2)
150
- value = amount.split(".").first.gsub(",", "").to_i
173
+ return "" if currency_amount.blank?
174
+
175
+ value = currency_amount.split(".").first.gsub(",", "").to_i
151
176
  abbreviated_num = number_to_human(value, units: { thousand: "K", million: "M", billion: "B", trillion: "T" }).gsub(/\s+/, "")
152
177
  abbreviated_num[index]
153
178
  end
@@ -174,9 +199,11 @@ module Playbook
174
199
 
175
200
  if decimals == "matching"
176
201
  if comma_separator
177
- number_with_delimiter(amount.gsub(",", ""))
202
+ return "" if currency_amount.blank?
203
+
204
+ number_with_delimiter(currency_amount.gsub(",", ""))
178
205
  else
179
- amount
206
+ currency_amount
180
207
  end
181
208
  else
182
209
  whole_value
@@ -133,3 +133,50 @@ test('handles negative amounts correctly', () => {
133
133
  expect(screen.getByTestId('test-negative-no-symbol')).toHaveTextContent('-400.50')
134
134
  expect(screen.getByTestId('test-negative-medium-size')).toHaveTextContent('$-500.55')
135
135
  })
136
+
137
+ test('handles numeric amounts correctly', () => {
138
+ render(
139
+ <>
140
+ <Currency
141
+ amount={320}
142
+ data={{ testid: 'test-numeric-default' }}
143
+ />
144
+ <Currency
145
+ abbreviate
146
+ amount={3200000}
147
+ data={{ testid: 'test-numeric-millions' }}
148
+ />
149
+ <Currency
150
+ amount={123456.78}
151
+ commaSeparator
152
+ data={{ testid: 'test-numeric-comma-decimals' }}
153
+ />
154
+ <Currency
155
+ amount={400.50}
156
+ data={{ testid: 'test-numeric-no-symbol' }}
157
+ symbol=""
158
+ />
159
+ <Currency
160
+ amount={500.55}
161
+ data={{ testid: 'test-numeric-medium-size' }}
162
+ size="md"
163
+ />
164
+ <Currency
165
+ amount={-600.70}
166
+ data={{ testid: 'test-numeric-negative' }}
167
+ />
168
+ <Currency
169
+ amount={0.00}
170
+ data={{ testid: 'test-numeric-null' }}
171
+ />
172
+ </>
173
+ )
174
+
175
+ expect(screen.getByTestId('test-numeric-default')).toHaveTextContent('$320')
176
+ expect(screen.getByTestId('test-numeric-millions')).toHaveTextContent('$3.2M')
177
+ expect(screen.getByTestId('test-numeric-comma-decimals')).toHaveTextContent('$123,456.78')
178
+ expect(screen.getByTestId('test-numeric-no-symbol')).toHaveTextContent('400.50')
179
+ expect(screen.getByTestId('test-numeric-medium-size')).toHaveTextContent('$500.55')
180
+ expect(screen.getByTestId('test-numeric-negative')).toHaveTextContent('-$600.70')
181
+ expect(screen.getByTestId('test-numeric-null')).toHaveTextContent('$.00')
182
+ })
@@ -7,7 +7,7 @@
7
7
  }) %>
8
8
 
9
9
  <%= pb_rails("currency", props: {
10
- amount: "342",
10
+ amount: 342,
11
11
  label: "Light",
12
12
  margin_bottom: "md",
13
13
  size: "sm",
@@ -14,7 +14,7 @@ const CurrencyVariants = (props) => {
14
14
  {...props}
15
15
  />
16
16
  <Currency
17
- amount="342"
17
+ amount={342}
18
18
  label="Light"
19
19
  marginBottom="md"
20
20
  size="sm"
@@ -0,0 +1 @@
1
+ **NOTE:** The value passed into the `amount` prop can be either a string or numeric value.
@@ -85,6 +85,8 @@ module Playbook
85
85
  default: ""
86
86
  prop :sync_end_with, type: Playbook::Props::String,
87
87
  default: ""
88
+ prop :cursor, type: Playbook::Props::String,
89
+ default: "pointer"
88
90
 
89
91
  def classname
90
92
  default_margin_bottom = margin_bottom.present? ? "" : " mb_sm"
@@ -1 +1 @@
1
- Customize your icon circle by passing one of our seven base colors to the `variant` prop: `royal` `blue` `orange` `purple` `teal` `red` `yellow` `green`
1
+ Customize your icon circle by passing one of our seven base colors to the `variant` prop: `royal` `orange` `purple` `teal` `red` `yellow` `green`
@@ -3,48 +3,41 @@
3
3
  text: "Mercury",
4
4
  unit: "AU",
5
5
  value: 0.39,
6
- variant:"blue"
6
+ variant:"royal"
7
7
  }) %>
8
8
  <br>
9
9
  <%= pb_rails("icon_stat_value", props: { icon: "planet-ringed",
10
10
  text: "Venus",
11
11
  unit: "AU",
12
12
  value: 0.723,
13
- variant:"royal"
13
+ variant:"purple"
14
14
  }) %>
15
15
  <br>
16
16
  <%= pb_rails("icon_stat_value", props: { icon: "planet-moon",
17
17
  text: "Earth",
18
18
  unit: "AU",
19
19
  value: 1.0,
20
- variant:"purple"
20
+ variant:"teal"
21
21
  }) %>
22
22
  <br>
23
23
  <%= pb_rails("icon_stat_value", props: { icon: "solar-system",
24
24
  text: "Mars",
25
25
  unit: "AU",
26
26
  value: 1.524,
27
- variant:"teal"
27
+ variant:"red"
28
28
  }) %>
29
29
  <br>
30
30
  <%= pb_rails("icon_stat_value", props: { icon: "globe-americas",
31
- text: "Jupitar",
31
+ text: "Jupiter",
32
32
  unit: "AU",
33
33
  value: 5.203,
34
- variant:"red"
34
+ variant:"yellow"
35
35
  }) %>
36
36
  <br>
37
37
  <%= pb_rails("icon_stat_value", props: { icon: "globe-africa",
38
38
  text: "Saturn",
39
39
  unit: "AU",
40
40
  value: 9.539,
41
- variant:"yellow"
42
- }) %>
43
- <br>
44
- <%= pb_rails("icon_stat_value", props: { icon: "globe",
45
- text: "Uranus",
46
- unit: "AU",
47
- value: 19.18,
48
41
  variant:"green"
49
42
  }) %>
50
43
  <br>
@@ -53,4 +46,4 @@
53
46
  unit: "AU",
54
47
  value: 19.18,
55
48
  variant:"orange"
56
- }) %>
49
+ }) %>
@@ -9,7 +9,7 @@ const IconStatValueColor = (props) => {
9
9
  text="Mercury"
10
10
  unit="AU"
11
11
  value={0.39}
12
- variant="blue"
12
+ variant="royal"
13
13
  {...props}
14
14
  />
15
15
  <br />
@@ -18,7 +18,7 @@ const IconStatValueColor = (props) => {
18
18
  text="Venus"
19
19
  unit="AU"
20
20
  value={0.723}
21
- variant="royal"
21
+ variant="purple"
22
22
  {...props}
23
23
  />
24
24
  <br />
@@ -27,7 +27,7 @@ const IconStatValueColor = (props) => {
27
27
  text="Earth"
28
28
  unit="AU"
29
29
  value={1.0}
30
- variant="purple"
30
+ variant="teal"
31
31
  {...props}
32
32
  />
33
33
  <br />
@@ -36,16 +36,16 @@ const IconStatValueColor = (props) => {
36
36
  text="Mars"
37
37
  unit="AU"
38
38
  value={1.524}
39
- variant="teal"
39
+ variant="red"
40
40
  {...props}
41
41
  />
42
42
  <br />
43
43
  <IconStatValue
44
44
  icon="globe-americas"
45
- text="Jupitar"
45
+ text="Jupiter"
46
46
  unit="AU"
47
47
  value={5.203}
48
- variant="red"
48
+ variant="yellow"
49
49
  {...props}
50
50
  />
51
51
  <br />
@@ -54,15 +54,6 @@ const IconStatValueColor = (props) => {
54
54
  text="Saturn"
55
55
  unit="AU"
56
56
  value={9.539}
57
- variant="yellow"
58
- {...props}
59
- />
60
- <br />
61
- <IconStatValue
62
- icon="globe"
63
- text="Uranus"
64
- unit="AU"
65
- value={19.18}
66
57
  variant="green"
67
58
  {...props}
68
59
  />
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable react/no-multi-comp */
2
- import React, { useEffect, useState } from "react";
2
+ import React, { useEffect, useRef, useState } from "react";
3
3
  import ReactDOM from "react-dom";
4
4
  import {
5
5
  Popper,
@@ -24,6 +24,7 @@ import { uniqueId } from '../utilities/object';
24
24
  type ModifiedGlobalProps = Omit<GlobalProps, 'minWidth' | 'maxHeight' | 'minHeight'>
25
25
 
26
26
  type PbPopoverProps = {
27
+ appendTo?: string;
27
28
  aria?: { [key: string]: string };
28
29
  className?: string;
29
30
  closeOnClick?: "outside" | "inside" | "any";
@@ -62,6 +63,25 @@ const popoverModifiers = ({
62
63
  return offset ? modifiers.concat([POPOVER_MODIFIERS.offset]) : modifiers;
63
64
  };
64
65
 
66
+ const getAppendTarget = (
67
+ appendTo: string | undefined,
68
+ targetId: string
69
+ ): HTMLElement => {
70
+ if (!appendTo || appendTo === "body") return document.body;
71
+
72
+ if (appendTo === "parent") {
73
+ const referenceWrapper = document.querySelector(`#reference-${targetId}`);
74
+ if (referenceWrapper?.parentElement) {
75
+ return referenceWrapper.parentElement;
76
+ }
77
+ }
78
+
79
+ const selectorMatch = document.querySelector(appendTo);
80
+ if (selectorMatch instanceof HTMLElement) return selectorMatch;
81
+
82
+ return document.body;
83
+ };
84
+
65
85
  const Popover = (props: PbPopoverProps) => {
66
86
  const {
67
87
  aria = {},
@@ -89,7 +109,7 @@ const Popover = (props: PbPopoverProps) => {
89
109
  const popoverSpacing =
90
110
  filteredGlobalProps.includes("dark") || !filteredGlobalProps
91
111
  ? "p_sm"
92
- : filteredGlobalProps
112
+ : filteredGlobalProps
93
113
  const overflowHandling = maxHeight || maxWidth ? "overflow_handling" : "";
94
114
  const zIndexStyle = zIndex ? { zIndex: zIndex } : {};
95
115
  const widthHeightStyles = () => {
@@ -113,7 +133,7 @@ const Popover = (props: PbPopoverProps) => {
113
133
 
114
134
  return (
115
135
  <Popper
116
- modifiers={popoverModifiers({ modifiers, offset })}
136
+ modifiers={popoverModifiers({ modifiers, offset: offset || false })}
117
137
  placement={placement}
118
138
  referenceElement={referenceElement}
119
139
  >
@@ -154,6 +174,7 @@ const Popover = (props: PbPopoverProps) => {
154
174
  const PbReactPopover = (props: PbPopoverProps): React.ReactElement => {
155
175
  const [targetId] = useState(uniqueId('id-'))
156
176
  const {
177
+ appendTo,
157
178
  className,
158
179
  children,
159
180
  modifiers = [],
@@ -170,42 +191,56 @@ const PbReactPopover = (props: PbPopoverProps): React.ReactElement => {
170
191
  minHeight,
171
192
  minWidth,
172
193
  width,
194
+ closeOnClick,
195
+ shouldClosePopover = noop,
173
196
  } = props;
174
197
 
198
+ // Store latest callback in a ref to avoid re-runs
199
+ const shouldClosePopoverRef = useRef(shouldClosePopover);
200
+
201
+ // Update ref on change
175
202
  useEffect(() => {
176
- const { closeOnClick, shouldClosePopover = noop } = props;
203
+ shouldClosePopoverRef.current = shouldClosePopover;
204
+ }, [shouldClosePopover]);
177
205
 
206
+ useEffect(() => {
178
207
  if (!closeOnClick) return;
179
208
 
180
- document.body.addEventListener(
181
- "click",
182
- (e: MouseEvent) => {
183
- const target = e.target as HTMLElement
209
+ // Function to handle popover event listener and targetId.
210
+ // Ensure that whenever the component is conditionally rendered
211
+ // that the old listener is removed and the new listener is
212
+ // updated with the targetId.
213
+ const handleClick = (e: MouseEvent) => {
214
+ const target = e.target as HTMLElement
184
215
 
185
- const targetIsPopover =
186
- target.closest("#" + targetId) !== null;
187
- const targetIsReference =
188
- target.closest("#reference-" + targetId) !== null;
216
+ const targetIsPopover =
217
+ target.closest("#" + targetId) !== null;
218
+ const targetIsReference =
219
+ target.closest("#reference-" + targetId) !== null;
189
220
 
190
- const shouldClose = () => {
191
- setTimeout(() => shouldClosePopover(true), 0);
192
- }
221
+ const shouldClose = () => {
222
+ setTimeout(() => shouldClosePopoverRef.current(true), 0);
223
+ }
193
224
 
194
- switch (closeOnClick) {
195
- case "outside":
196
- if (!targetIsPopover && !targetIsReference) shouldClose();
197
- break;
198
- case "inside":
199
- if (targetIsPopover) shouldClose();
200
- break;
201
- case "any":
202
- if (targetIsPopover || !targetIsPopover && !targetIsReference) shouldClose();
203
- break;
204
- }
205
- },
206
- { capture: true }
207
- );
208
- }, []);
225
+ switch (closeOnClick) {
226
+ case "outside":
227
+ if (!targetIsPopover && !targetIsReference) shouldClose();
228
+ break;
229
+ case "inside":
230
+ if (targetIsPopover) shouldClose();
231
+ break;
232
+ case "any":
233
+ if (targetIsPopover || !targetIsPopover && !targetIsReference) shouldClose();
234
+ break;
235
+ }
236
+ };
237
+
238
+ document.body.addEventListener("click", handleClick, { capture: true });
239
+
240
+ return () => {
241
+ document.body.removeEventListener("click", handleClick, { capture: true });
242
+ };
243
+ }, [targetId, closeOnClick]);
209
244
 
210
245
  const popoverComponent = (
211
246
  <Popover
@@ -246,10 +281,10 @@ const PbReactPopover = (props: PbPopoverProps): React.ReactElement => {
246
281
  {show &&
247
282
  (usePortal ? (
248
283
  <>
249
- {ReactDOM.createPortal(
250
- popoverComponent,
251
- document.querySelector(portal)
252
- )}
284
+ {ReactDOM.createPortal(
285
+ popoverComponent,
286
+ getAppendTarget(appendTo, targetId)
287
+ )}
253
288
  </>
254
289
  ) : (
255
290
  { popoverComponent }
@@ -0,0 +1,68 @@
1
+ import React, { useState } from "react";
2
+ import { PbReactPopover, CircleIconButton, Body, Flex } from "playbook-ui";
3
+
4
+ const PopoverAppendTo = (props) => {
5
+ const [showParentPopover, setShowParentPopover] = useState(false);
6
+ const [showSelectorPopover, setShowSelectorPopover] = useState(false);
7
+
8
+ const parentPopoverReference = (
9
+ <CircleIconButton
10
+ icon="info"
11
+ onClick={() => setShowParentPopover(!showParentPopover)}
12
+ variant="secondary"
13
+ />
14
+ );
15
+
16
+ const selectorPopoverReference = (
17
+ <CircleIconButton
18
+ icon="info"
19
+ onClick={() => setShowSelectorPopover(!showSelectorPopover)}
20
+ variant="secondary"
21
+ />
22
+ );
23
+
24
+ return (
25
+ <>
26
+ <Flex
27
+ marginBottom="md"
28
+ orientation="row"
29
+ vertical="center"
30
+ {...props}
31
+ >
32
+ <Body text="Click info for more details" />
33
+ &nbsp;
34
+ <PbReactPopover
35
+ appendTo="parent"
36
+ offset
37
+ placement="top"
38
+ reference={parentPopoverReference}
39
+ show={showParentPopover}
40
+ {...props}
41
+ >
42
+ {'I\'m a popover. I have been appended to my parent element.'}
43
+ </PbReactPopover>
44
+ </Flex>
45
+
46
+ <Flex
47
+ orientation="row"
48
+ vertical="center"
49
+ {...props}
50
+ >
51
+ <Body text="Click info for more details" />
52
+ &nbsp;
53
+ <PbReactPopover
54
+ appendTo=".kit-show-wrapper"
55
+ offset
56
+ placement="top"
57
+ reference={selectorPopoverReference}
58
+ show={showSelectorPopover}
59
+ {...props}
60
+ >
61
+ {'I\'m a popover. I have been appended to the .kit-show-wrapper.'}
62
+ </PbReactPopover>
63
+ </Flex>
64
+ </>
65
+ );
66
+ };
67
+
68
+ export default PopoverAppendTo;