Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Picker - initial scroll position #1942

Merged
merged 30 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
977ca8a
Find element scroll position (#1935)
bmingles Apr 16, 2024
cea6642
Find scroll position in elements (#1935)
bmingles Apr 17, 2024
e8f1a3b
Cleanup (#1935)
bmingles Apr 17, 2024
73d29b0
Started splitting Picker component (#1935)
bmingles Apr 17, 2024
7664794
WIP (#1935)
bmingles Apr 17, 2024
3fa1fc5
Cleanup (#1935)
bmingles Apr 17, 2024
0a2a23f
Cleanup (#1935)
bmingles Apr 17, 2024
f9e05ce
Cleanup (#1935)
bmingles Apr 17, 2024
eb41359
PickerNormalized (#1935)
bmingles Apr 17, 2024
3a1a9cb
Refactored to use PickerNormalized (#1935)
bmingles Apr 17, 2024
2c0af14
Removed redundant Text wrapper (#1935)
bmingles Apr 18, 2024
7ac58b7
Added keys (#1935)
bmingles Apr 18, 2024
60d1972
Derive key from text values (#1935)
bmingles Apr 18, 2024
0bc0cfe
Empty textValue handling + tests (#1935)
bmingles Apr 18, 2024
bd4f627
Fixed uncontrolled selection detection (#1935)
bmingles Apr 18, 2024
b565cdc
Section heights in scroll position (#1935)
bmingles Apr 18, 2024
72b491a
Diff cleanup (#1935)
bmingles Apr 19, 2024
c880bec
Comment (#1935)
bmingles Apr 19, 2024
3266fec
Added comments (#1935)
bmingles Apr 19, 2024
a6de5c7
useStringifiedSelection tests (#1935)
bmingles Apr 19, 2024
c1f4270
Disable auto scroll for description / section scenarios (#1935)
bmingles Apr 22, 2024
b5a6d76
Tests (#1935)
bmingles Apr 22, 2024
678b74b
usePickerScrollOnOpen tests (#1935)
bmingles Apr 22, 2024
6a87a6f
Comments (#1935)
bmingles Apr 22, 2024
4122ef5
Comment (#1935)
bmingles Apr 22, 2024
3bc0bdd
test coverage (#1935)
bmingles Apr 22, 2024
4a7539a
Styleguide tweak (#1935)
bmingles Apr 22, 2024
fa5e179
Added to index (#1935)
bmingles Apr 22, 2024
7c25f9f
Review comments (#1935)
bmingles Apr 24, 2024
1f37fde
Fixed off by 1 bug (#1935)
bmingles Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 69 additions & 32 deletions packages/code-studio/src/styleguide/Pickers.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,56 @@
import React, { useCallback, useState } from 'react';
import React, { cloneElement, useCallback, useState } from 'react';
import {
Flex,
Item,
Picker,
ItemKey,
Section,
Text,
PickerNormalized,
} from '@deephaven/components';
import { vsPerson } from '@deephaven/icons';
import { Icon } from '@adobe/react-spectrum';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { generateNormalizedItems, sampleSectionIdAndClasses } from './utils';
import { getPositionOfSelectedItem } from '@deephaven/react-hooks';
import { PICKER_ITEM_HEIGHTS, PICKER_TOP_OFFSET } from '@deephaven/utils';
import {
generateItemElements,
generateNormalizedItems,
sampleSectionIdAndClasses,
} from './utils';

// Generate enough items to require scrolling
const items = [...generateNormalizedItems(52)];
const itemElementsA = [...generateItemElements(0, 51)];
const itemElementsB = [...generateItemElements(52, 103)];
const itemElementsC = [...generateItemElements(104, 155)];
const itemElementsD = [...generateItemElements(156, 207)];
const itemElementsE = [...generateItemElements(208, 259)];

const mixedItemsWithIconsNoDescriptions = [
'String 1',
'String 2',
'String 3',
'',
'Some really long text that should get truncated',
444,
999,
true,
false,
...itemElementsA.map((itemEl, i) =>
i % 5 > 0
? itemEl
: cloneElement(itemEl, {
...itemEl.props,
children: [
<PersonIcon key={`icon-${itemEl.props.children}`} />,
<Text key={`label-${itemEl.props.children}`}>
{itemEl.props.children}
</Text>,
],
})
),
];

function PersonIcon(): JSX.Element {
return (
Expand All @@ -26,6 +63,17 @@ function PersonIcon(): JSX.Element {
export function Pickers(): JSX.Element {
const [selectedKey, setSelectedKey] = useState<ItemKey | null>(null);

const getInitialScrollPosition = useCallback(
async () =>
getPositionOfSelectedItem({
keyedItems: items,
itemHeight: PICKER_ITEM_HEIGHTS.medium,
selectedKey,
topOffset: PICKER_TOP_OFFSET,
}),
[selectedKey]
);

const onChange = useCallback((key: ItemKey): void => {
setSelectedKey(key);
}, []);
Expand All @@ -37,69 +85,58 @@ export function Pickers(): JSX.Element {

<Flex gap={14}>
<Picker label="Single Child" tooltip={{ placement: 'bottom-end' }}>
<Item>Aaa</Item>
<Item textValue="Aaa">Aaa</Item>
</Picker>

<Picker label="Mixed Children Types" defaultSelectedKey={999} tooltip>
{/* eslint-disable react/jsx-curly-brace-presence */}
{'String 1'}
{'String 2'}
{'String 3'}
{''}
{'Some really long text that should get truncated'}
{/* eslint-enable react/jsx-curly-brace-presence */}
{444}
{999}
{true}
{false}
<Item>Item Aaa</Item>
<Item>Item Bbb</Item>
<Item textValue="Complex Ccc">
<PersonIcon />
<Text>Complex Ccc with text that should be truncated</Text>
</Item>
<Picker label="Mixed Children Types" defaultSelectedKey="999" tooltip>
{mixedItemsWithIconsNoDescriptions}
</Picker>

<Picker label="Sections" tooltip>
{/* eslint-disable react/jsx-curly-brace-presence */}
{'String 1'}
{'String 2'}
{'String 3'}
<Section title="Section A">
<Item>Item Aaa</Item>
<Item>Item Bbb</Item>
<Section title="Section">
<Item textValue="Item Aaa">Item Aaa</Item>
<Item textValue="Item Bbb">Item Bbb</Item>
<Item textValue="Complex Ccc">
<PersonIcon />
<Text>Complex Ccc</Text>
</Item>
</Section>
<Section key="Key B">
<Item>Item Ddd</Item>
<Item>Item Eee</Item>
<Item textValue="Item Ddd">Item Ddd</Item>
<Item textValue="Item Eee">Item Eee</Item>
<Item textValue="Complex Fff">
<PersonIcon />
<Text>Complex Fff</Text>
</Item>
<Item key="Ggg">
<Item textValue="Ggg">
<PersonIcon />
<Text>Label</Text>
<Text slot="description">Description</Text>
</Item>
<Item key="Hhh">
<Item textValue="Hhh">
<PersonIcon />
<Text>Label that causes overflow</Text>
<Text slot="description">Description that causes overflow</Text>
</Item>
</Section>
<Section title="Section A">{itemElementsA}</Section>
<Section title="Section B">{itemElementsB}</Section>
<Section key="Section C">{itemElementsC}</Section>
<Section key="Section D">{itemElementsD}</Section>
<Section title="Section E">{itemElementsE}</Section>
</Picker>

<Picker
<PickerNormalized
label="Controlled"
getInitialScrollPosition={getInitialScrollPosition}
normalizedItems={items}
selectedKey={selectedKey}
onChange={onChange}
>
{items}
</Picker>
/>
</Flex>
</div>
);
Expand Down
Loading
Loading