playbook_ui 15.4.0.pre.rc.0 → 15.4.0.pre.rc.1
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/app/pb_kits/playbook/pb_advanced_table/Hooks/useTableState.ts +15 -0
- data/app/pb_kits/playbook/pb_advanced_table/docs/_advanced_table_pagination_with_props.jsx +4 -1
- data/app/pb_kits/playbook/pb_advanced_table/docs/_advanced_table_pagination_with_props.md +3 -2
- data/app/pb_kits/playbook/pb_date_picker/date_picker.rb +2 -0
- data/app/pb_kits/playbook/pb_popover/_popover.tsx +69 -34
- data/app/pb_kits/playbook/pb_popover/docs/_popover_append_to.jsx +68 -0
- data/app/pb_kits/playbook/pb_popover/docs/_popover_append_to_react.md +1 -0
- data/app/pb_kits/playbook/pb_popover/docs/example.yml +1 -0
- data/app/pb_kits/playbook/pb_popover/docs/index.js +1 -0
- data/app/pb_kits/playbook/pb_popover/popover.test.js +80 -0
- data/app/pb_kits/playbook/pb_text_input/text_input.rb +2 -2
- data/app/pb_kits/playbook/pb_typeahead/_typeahead.test.jsx +41 -0
- data/app/pb_kits/playbook/pb_typeahead/_typeahead.tsx +23 -9
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_custom_options.html.erb +64 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_custom_options.jsx +70 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_custom_options.md +1 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_default_options.html.erb +67 -1
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_default_value.jsx +68 -6
- data/app/pb_kits/playbook/pb_typeahead/docs/example.yml +2 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/index.js +2 -1
- data/dist/chunks/{_line_graph-h5H-imfn.js → _line_graph-CqE0-dq5.js} +1 -1
- data/dist/chunks/_typeahead-3ZAbZUqU.js +6 -0
- data/dist/chunks/{_weekday_stacked-c6_R08J-.js → _weekday_stacked-BFB3mjtE.js} +2 -2
- data/dist/chunks/vendor.js +1 -1
- data/dist/playbook-doc.js +2 -2
- data/dist/playbook-rails-react-bindings.js +1 -1
- data/dist/playbook-rails.js +1 -1
- data/lib/playbook/version.rb +1 -1
- metadata +11 -6
- data/dist/chunks/_typeahead-U8AjZIIW.js +0 -6
- /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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3228eb0eef8bd4c6ea21b1f728bf755c02819ca51a26f88b47c2b18b94a20f7d
|
|
4
|
+
data.tar.gz: 6cbedab0cc40f9a3aaa1bfd67faab03675b5936db81a2afc82c25b184d201b02
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0c6875620df77729995d251dd5cbfc9b2ddc73273aaeb7299644dd723d80133eb8a052a503a521eb5fe1e3f8acc4542a7994ef124d4d27dacaa17bad75bb436b
|
|
7
|
+
data.tar.gz: 35dade6af63d802f74790efe9c45282641f6160d6b7202b08e081bbb75b51bb6426cf185380e48479e2112c0d2c83225221115d3c1e162214fd68539695237a1
|
|
@@ -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;
|
|
@@ -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
|
|
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.
|
|
@@ -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
|
-
|
|
203
|
+
shouldClosePopoverRef.current = shouldClosePopover;
|
|
204
|
+
}, [shouldClosePopover]);
|
|
177
205
|
|
|
206
|
+
useEffect(() => {
|
|
178
207
|
if (!closeOnClick) return;
|
|
179
208
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
216
|
+
const targetIsPopover =
|
|
217
|
+
target.closest("#" + targetId) !== null;
|
|
218
|
+
const targetIsReference =
|
|
219
|
+
target.closest("#reference-" + targetId) !== null;
|
|
189
220
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
221
|
+
const shouldClose = () => {
|
|
222
|
+
setTimeout(() => shouldClosePopoverRef.current(true), 0);
|
|
223
|
+
}
|
|
193
224
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
+
|
|
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
|
+
|
|
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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
By default, the popover tooltip attaches to the `<body>`. To attach it elsewhere, use the `appendTo` prop. Set it to `"parent"` to place the tooltip inside its parent element, or pass any CSS selector (`#id` or `.class`) to specify a custom container.
|
|
@@ -4,3 +4,4 @@ export { default as PopoverClose } from './_popover_close.jsx'
|
|
|
4
4
|
export { default as PopoverZIndex } from './_popover_z_index.jsx'
|
|
5
5
|
export { default as PopoverScrollHeight } from './_popover_scroll_height.jsx'
|
|
6
6
|
export { default as PopoverActionableContent } from './_popover_actionable_content.jsx'
|
|
7
|
+
export { default as PopoverAppendTo } from './_popover_append_to.jsx'
|
|
@@ -159,6 +159,62 @@ const PopoverTestClicktoClose3 = () => {
|
|
|
159
159
|
)
|
|
160
160
|
};
|
|
161
161
|
|
|
162
|
+
//Test Popover with appendTo="parent"
|
|
163
|
+
const PopoverTestAppendToParent = () => {
|
|
164
|
+
const [mockState, setMockState] = React.useState(false)
|
|
165
|
+
const togglePopover = () => {
|
|
166
|
+
setMockState(!mockState)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const popoverReference = (
|
|
170
|
+
<Button onClick={togglePopover}
|
|
171
|
+
text="Click Me"
|
|
172
|
+
/>
|
|
173
|
+
);
|
|
174
|
+
return (
|
|
175
|
+
<div data-testid="parent-container"
|
|
176
|
+
id="parent-container"
|
|
177
|
+
>
|
|
178
|
+
<PbReactPopover
|
|
179
|
+
appendTo="parent"
|
|
180
|
+
offset
|
|
181
|
+
reference={popoverReference}
|
|
182
|
+
show={mockState}
|
|
183
|
+
>
|
|
184
|
+
{"Appended to parent"}
|
|
185
|
+
</PbReactPopover>
|
|
186
|
+
</div>
|
|
187
|
+
)
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
//Test Popover with appendTo CSS selector
|
|
191
|
+
const PopoverTestAppendToSelector = () => {
|
|
192
|
+
const [mockState, setMockState] = React.useState(false)
|
|
193
|
+
const togglePopover = () => {
|
|
194
|
+
setMockState(!mockState)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const popoverReference = (
|
|
198
|
+
<Button onClick={togglePopover}
|
|
199
|
+
text="Click Me"
|
|
200
|
+
/>
|
|
201
|
+
);
|
|
202
|
+
return (
|
|
203
|
+
<div data-testid="custom-container"
|
|
204
|
+
id="custom-container"
|
|
205
|
+
>
|
|
206
|
+
<PbReactPopover
|
|
207
|
+
appendTo="#custom-container"
|
|
208
|
+
offset
|
|
209
|
+
reference={popoverReference}
|
|
210
|
+
show={mockState}
|
|
211
|
+
>
|
|
212
|
+
{"Appended to custom container"}
|
|
213
|
+
</PbReactPopover>
|
|
214
|
+
</div>
|
|
215
|
+
)
|
|
216
|
+
};
|
|
217
|
+
|
|
162
218
|
|
|
163
219
|
test("renders Popover", () => {
|
|
164
220
|
render(<PopoverTest data={{ testid: testId}}/>)
|
|
@@ -217,4 +273,28 @@ const PopoverTestClicktoClose3 = () => {
|
|
|
217
273
|
fireEvent.click(btn);
|
|
218
274
|
|
|
219
275
|
expect(kit).not.toBeInTheDocument;
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test("renders Popover with appendTo parent", () => {
|
|
279
|
+
render(<PopoverTestAppendToParent data={{ testid: testId}}/>)
|
|
280
|
+
const btn = screen.getByText(/click me/i)
|
|
281
|
+
fireEvent.click(btn);
|
|
282
|
+
const kit = screen.getByText("Appended to parent");
|
|
283
|
+
expect(kit).toBeInTheDocument();
|
|
284
|
+
|
|
285
|
+
// Check that the popover is rendered inside the parent container
|
|
286
|
+
const parentContainer = screen.getByTestId("parent-container");
|
|
287
|
+
expect(parentContainer).toContainElement(kit);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test("renders Popover with appendTo CSS selector", () => {
|
|
291
|
+
render(<PopoverTestAppendToSelector data={{ testid: testId}}/>)
|
|
292
|
+
const btn = screen.getByText(/click me/i)
|
|
293
|
+
fireEvent.click(btn);
|
|
294
|
+
const kit = screen.getByText("Appended to custom container");
|
|
295
|
+
expect(kit).toBeInTheDocument();
|
|
296
|
+
|
|
297
|
+
// Check that the popover is rendered inside the custom container
|
|
298
|
+
const customContainer = screen.getByTestId("custom-container");
|
|
299
|
+
expect(customContainer).toContainElement(kit);
|
|
220
300
|
});
|
|
@@ -99,8 +99,8 @@ module Playbook
|
|
|
99
99
|
# Convert camelCase (ex. notAllowed) to kebab-case (ex. not-allowed)
|
|
100
100
|
cursor.to_s.gsub(/([a-z\d])([A-Z])/, '\1-\2').downcase
|
|
101
101
|
else
|
|
102
|
-
# Default to '
|
|
103
|
-
"
|
|
102
|
+
# Default to 'text'
|
|
103
|
+
"text"
|
|
104
104
|
end
|
|
105
105
|
end
|
|
106
106
|
|
|
@@ -171,4 +171,45 @@ test('typeahead with defaultValue with focus behavior', async () => {
|
|
|
171
171
|
expect(focusedOption).toBeInTheDocument()
|
|
172
172
|
expect(focusedOption).toHaveTextContent('Red')
|
|
173
173
|
})
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
test('typeahead with grouped options and defaultValue focus behavior', async () => {
|
|
177
|
+
const groupedOptions = [
|
|
178
|
+
{
|
|
179
|
+
label: "Warm Colors",
|
|
180
|
+
options: [
|
|
181
|
+
{ label: "Red", value: "#FF0000" },
|
|
182
|
+
{ label: "Orange", value: "#FFA500" },
|
|
183
|
+
{ label: "Yellow", value: "#FFFF00" }
|
|
184
|
+
]
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
label: "Cool Colors",
|
|
188
|
+
options: [
|
|
189
|
+
{ label: "Blue", value: "#0000FF" },
|
|
190
|
+
{ label: "Teal", value: "#008080" },
|
|
191
|
+
{ label: "Cyan", value: "#00FFFF" }
|
|
192
|
+
]
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
label: "Fun Shades",
|
|
196
|
+
options: [
|
|
197
|
+
{ label: "Pink", value: "#FFC0CB" },
|
|
198
|
+
{ label: "Magenta", value: "#FF00FF" },
|
|
199
|
+
{ label: "Purple", value: "#800080" }
|
|
200
|
+
]
|
|
201
|
+
}
|
|
202
|
+
]
|
|
203
|
+
|
|
204
|
+
render(
|
|
205
|
+
<Typeahead
|
|
206
|
+
data={{ testid: 'grouped-options-focus-test' }}
|
|
207
|
+
defaultValue={[{ label: "Pink", value: "#FFC0CB" }]}
|
|
208
|
+
options={groupedOptions}
|
|
209
|
+
/>
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
const kit = screen.getByTestId('grouped-options-focus-test')
|
|
213
|
+
const inputDiv = kit.querySelector(".typeahead-kit-select__single-value")
|
|
214
|
+
expect(inputDiv).toHaveTextContent("Pink")
|
|
174
215
|
})
|
|
@@ -144,6 +144,18 @@ const Typeahead = forwardRef<HTMLInputElement, TypeaheadProps>(({
|
|
|
144
144
|
// Create a ref to access React Select instance
|
|
145
145
|
const selectRef = useRef<any>(null)
|
|
146
146
|
|
|
147
|
+
// Helper function to flatten grouped options if custom groups are used
|
|
148
|
+
const flattenOptions = (options: any[]): any[] => {
|
|
149
|
+
if (!options) return []
|
|
150
|
+
|
|
151
|
+
return options.reduce((acc, option) => {
|
|
152
|
+
if (option.options && Array.isArray(option.options)) {
|
|
153
|
+
return [...acc, ...option.options]
|
|
154
|
+
}
|
|
155
|
+
return [...acc, option]
|
|
156
|
+
}, [])
|
|
157
|
+
}
|
|
158
|
+
|
|
147
159
|
// Configure focus on selected option using React Select's API
|
|
148
160
|
const handleMenuOpen = () => {
|
|
149
161
|
setTimeout(() => {
|
|
@@ -159,17 +171,18 @@ const Typeahead = forwardRef<HTMLInputElement, TypeaheadProps>(({
|
|
|
159
171
|
|
|
160
172
|
const options = props.options
|
|
161
173
|
if (options) {
|
|
162
|
-
//
|
|
163
|
-
const
|
|
174
|
+
// Flatten grouped options to find the matching option and find matching option
|
|
175
|
+
const flatOptions = flattenOptions(options)
|
|
176
|
+
|
|
177
|
+
const targetOption = flatOptions.find((option: any) => {
|
|
164
178
|
const optionValue = props.getOptionValue ? props.getOptionValue(option) : option.value
|
|
165
179
|
const currentOptionValue = props.getOptionValue ? props.getOptionValue(currentValue) : currentValue.value
|
|
166
180
|
return optionValue === currentOptionValue
|
|
167
181
|
})
|
|
168
182
|
|
|
169
|
-
if (
|
|
183
|
+
if (targetOption) {
|
|
170
184
|
// Use React Select's internal state to set focused option
|
|
171
185
|
if (selectRef.current && selectRef.current.setState) {
|
|
172
|
-
const targetOption = options[focusedIndex]
|
|
173
186
|
selectRef.current.setState({
|
|
174
187
|
focusedOption: targetOption,
|
|
175
188
|
focusedValue: null
|
|
@@ -179,11 +192,12 @@ const Typeahead = forwardRef<HTMLInputElement, TypeaheadProps>(({
|
|
|
179
192
|
setTimeout(() => {
|
|
180
193
|
if (selectRef.current && selectRef.current.menuListRef) {
|
|
181
194
|
const menuElement = selectRef.current.menuListRef
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const
|
|
195
|
+
// Find the focused option using React Select's class
|
|
196
|
+
const focusedElement = menuElement.querySelector('.typeahead-kit-select__option--is-focused')
|
|
197
|
+
|
|
198
|
+
if (focusedElement) {
|
|
199
|
+
const optionTop = focusedElement.offsetTop
|
|
200
|
+
const optionHeight = focusedElement.offsetHeight
|
|
187
201
|
const menuHeight = menuElement.clientHeight
|
|
188
202
|
|
|
189
203
|
// Set the menu's scrollTop to position the selected option in the middle
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
<%
|
|
2
|
+
grouped_options = [
|
|
3
|
+
{
|
|
4
|
+
label: "Warm Colors",
|
|
5
|
+
options: [
|
|
6
|
+
{ label: "Red", value: "#FF0000" },
|
|
7
|
+
{ label: "Orange", value: "#FFA500" },
|
|
8
|
+
{ label: "Yellow", value: "#FFFF00" },
|
|
9
|
+
{ label: "Coral", value: "#FF7F50" },
|
|
10
|
+
{ label: "Gold", value: "#FFD700" }
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
label: "Cool Colors",
|
|
15
|
+
options: [
|
|
16
|
+
{ label: "Blue", value: "#0000FF" },
|
|
17
|
+
{ label: "Teal", value: "#008080" },
|
|
18
|
+
{ label: "Cyan", value: "#00FFFF" },
|
|
19
|
+
{ label: "Navy", value: "#000080" },
|
|
20
|
+
{ label: "Turquoise", value: "#40E0D0" }
|
|
21
|
+
]
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
label: "Neutrals",
|
|
25
|
+
options: [
|
|
26
|
+
{ label: "White", value: "#FFFFFF" },
|
|
27
|
+
{ label: "Black", value: "#000000" },
|
|
28
|
+
{ label: "Gray", value: "#808080" },
|
|
29
|
+
{ label: "Beige", value: "#F5F5DC" },
|
|
30
|
+
{ label: "Silver", value: "#C0C0C0" }
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
label: "Earth Tones",
|
|
35
|
+
options: [
|
|
36
|
+
{ label: "Brown", value: "#A52A2A" },
|
|
37
|
+
{ label: "Olive", value: "#808000" },
|
|
38
|
+
{ label: "Forest Green", value: "#228B22" },
|
|
39
|
+
{ label: "Tan", value: "#D2B48C" },
|
|
40
|
+
{ label: "Maroon", value: "#800000" }
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
label: "Fun Shades",
|
|
45
|
+
options: [
|
|
46
|
+
{ label: "Pink", value: "#FFC0CB" },
|
|
47
|
+
{ label: "Magenta", value: "#FF00FF" },
|
|
48
|
+
{ label: "Lime", value: "#00FF00" },
|
|
49
|
+
{ label: "Purple", value: "#800080" },
|
|
50
|
+
{ label: "Indigo", value: "#4B0082" }
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
%>
|
|
55
|
+
<br />
|
|
56
|
+
<%= pb_rails("typeahead", props: {
|
|
57
|
+
id: "typeahead-custom-options",
|
|
58
|
+
options: grouped_options,
|
|
59
|
+
label: "Colors",
|
|
60
|
+
name: :foo,
|
|
61
|
+
placeholder: "Select a Color...",
|
|
62
|
+
is_multi: false
|
|
63
|
+
})
|
|
64
|
+
%>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
|
|
3
|
+
import Typeahead from '../_typeahead'
|
|
4
|
+
|
|
5
|
+
const groupedOptions = [
|
|
6
|
+
{
|
|
7
|
+
label: "Warm Colors",
|
|
8
|
+
options: [
|
|
9
|
+
{ label: "Red", value: "#FF0000" },
|
|
10
|
+
{ label: "Orange", value: "#FFA500" },
|
|
11
|
+
{ label: "Yellow", value: "#FFFF00" },
|
|
12
|
+
{ label: "Coral", value: "#FF7F50" },
|
|
13
|
+
{ label: "Gold", value: "#FFD700" }
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
label: "Cool Colors",
|
|
18
|
+
options: [
|
|
19
|
+
{ label: "Blue", value: "#0000FF" },
|
|
20
|
+
{ label: "Teal", value: "#008080" },
|
|
21
|
+
{ label: "Cyan", value: "#00FFFF" },
|
|
22
|
+
{ label: "Navy", value: "#000080" },
|
|
23
|
+
{ label: "Turquoise", value: "#40E0D0" }
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
label: "Neutrals",
|
|
28
|
+
options: [
|
|
29
|
+
{ label: "White", value: "#FFFFFF" },
|
|
30
|
+
{ label: "Black", value: "#000000" },
|
|
31
|
+
{ label: "Gray", value: "#808080" },
|
|
32
|
+
{ label: "Beige", value: "#F5F5DC" },
|
|
33
|
+
{ label: "Silver", value: "#C0C0C0" }
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
label: "Earth Tones",
|
|
38
|
+
options: [
|
|
39
|
+
{ label: "Brown", value: "#A52A2A" },
|
|
40
|
+
{ label: "Olive", value: "#808000" },
|
|
41
|
+
{ label: "Forest Green", value: "#228B22" },
|
|
42
|
+
{ label: "Tan", value: "#D2B48C" },
|
|
43
|
+
{ label: "Maroon", value: "#800000" }
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
label: "Fun Shades",
|
|
48
|
+
options: [
|
|
49
|
+
{ label: "Pink", value: "#FFC0CB" },
|
|
50
|
+
{ label: "Magenta", value: "#FF00FF" },
|
|
51
|
+
{ label: "Lime", value: "#00FF00" },
|
|
52
|
+
{ label: "Purple", value: "#800080" },
|
|
53
|
+
{ label: "Indigo", value: "#4B0082" }
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
const TypeaheadCustomOptions = (props) => {
|
|
59
|
+
return (
|
|
60
|
+
<Typeahead
|
|
61
|
+
label="Colors"
|
|
62
|
+
options={groupedOptions}
|
|
63
|
+
placeholder="Select a Color..."
|
|
64
|
+
{...props}
|
|
65
|
+
/>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export default TypeaheadCustomOptions
|
|
70
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Grouped options can be rendered via structuring the options in the way shown in the code snippet below.
|