Skip to content

Commit

Permalink
Moved / renamed Spectrum utils (deephaven#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 1, 2024
1 parent c032c9b commit de9cad0
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 26 deletions.
3 changes: 2 additions & 1 deletion packages/code-studio/src/styleguide/Pickers.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useCallback, useState } from 'react';
import {
Flex,
Item,
Picker,
PickerItemKey,
Section,
Text,
} from '@deephaven/components';
import { vsPerson } from '@deephaven/icons';
import { Icon, Item } from '@adobe/react-spectrum';
import { Icon } from '@adobe/react-spectrum';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { sampleSectionIdAndClasses } from './utils';

Expand Down
1 change: 0 additions & 1 deletion packages/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export * from './SelectValueList';
export * from './shortcuts';
export { default as SocketedButton } from './SocketedButton';
export * from './spectrum';
export * from './spectrum/utils';
export * from './TableViewEmptyState';
export * from './TextWithTooltip';
export * from './theme';
Expand Down
5 changes: 3 additions & 2 deletions packages/components/src/spectrum/picker/Picker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Key, ReactNode, useCallback, useMemo } from 'react';
import { DOMRef } from '@react-types/shared';
import { Flex, Picker as SpectrumPicker, Text } from '@adobe/react-spectrum';
import { Flex, Picker as SpectrumPicker } from '@adobe/react-spectrum';
import {
getPositionOfSelectedItem,
findSpectrumPickerScrollArea,
Expand All @@ -24,9 +24,10 @@ import {
TooltipOptions,
PickerItemKey,
getPickerItemKey,
} from './PickerUtils';
} from '../utils/itemUtils';
import { PickerItemContent } from './PickerItemContent';
import { Item, Section } from '../shared';
import { Text } from '../Text';

export type PickerProps = {
children:
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/spectrum/picker/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './Picker';
export * from './PickerUtils';
export * from './PickerItemContent';
2 changes: 2 additions & 0 deletions packages/components/src/spectrum/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './itemUtils';
export * from './themeUtils';
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
PickerItem,
PickerItemOrSection,
PickerSection,
} from './PickerUtils';
import type { PickerProps } from './Picker';
} from './itemUtils';
import type { PickerProps } from '../picker/Picker';
import { Item, Section } from '../shared';
import { Text } from '../Text';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ export function isItemElement<T>(
* @param node The node to check
* @returns True if the node is a normalized item with keys array
*/
export function isNormalizedItemsWithKeysList(
export function isNormalizedItemsWithKeysList<
TItemOrSection extends PickerItemOrSection,
>(
node:
| PickerItemOrSection
| PickerItemOrSection[]
| TItemOrSection
| TItemOrSection[]
| (NormalizedPickerItem | NormalizedPickerSection)[]
): node is (NormalizedPickerItem | NormalizedPickerSection)[] {
): node is NormalizedItemOrSection<TItemOrSection>[] {
if (!Array.isArray(node)) {
return false;
}
Expand Down Expand Up @@ -234,9 +236,9 @@ function normalizeTextValue(item: PickerItem): string | undefined {
* @param itemOrSection item to normalize
* @returns NormalizedPickerItem object
*/
function normalizePickerItem(
itemOrSection: PickerItemOrSection
): NormalizedPickerItem | NormalizedPickerSection {
function normalizePickerItem<TItemOrSection extends PickerItemOrSection>(
itemOrSection: TItemOrSection
): NormalizedItemOrSection<TItemOrSection> {
if (!isPickerItemOrSection(itemOrSection)) {
log.debug(INVALID_PICKER_ITEM_ERROR_MESSAGE, itemOrSection);
throw new Error(INVALID_PICKER_ITEM_ERROR_MESSAGE);
Expand All @@ -253,7 +255,7 @@ function normalizePickerItem(

return {
item: { key, title, items },
};
} as NormalizedItemOrSection<TItemOrSection>;
}

const key = normalizeItemKey(itemOrSection);
Expand All @@ -264,26 +266,30 @@ function normalizePickerItem(

return {
item: { key, content, textValue },
};
} as NormalizedItemOrSection<TItemOrSection>;
}

type NormalizedItemOrSection<TItemOrSection extends PickerItemOrSection> =
TItemOrSection extends PickerSection
? NormalizedPickerSection
: NormalizedPickerItem;

/**
* Get normalized picker items from a picker item or array of picker items.
* @param itemsOrSections A picker item or array of picker items
* @returns An array of normalized picker items
*/
export function normalizePickerItemList(
itemsOrSections:
| PickerItemOrSection
| PickerItemOrSection[]
| NormalizedPickerItem[]
): (NormalizedPickerItem | NormalizedPickerSection)[] {
export function normalizePickerItemList<
TItemOrSection extends PickerItemOrSection,
>(
itemsOrSections: TItemOrSection | TItemOrSection[] | NormalizedPickerItem[]
): NormalizedItemOrSection<TItemOrSection>[] {
// If already normalized, just return as-is
if (isNormalizedItemsWithKeysList(itemsOrSections)) {
return itemsOrSections;
return itemsOrSections as NormalizedItemOrSection<TItemOrSection>[];
}

const itemsArray = Array.isArray(itemsOrSections)
const itemsArray: TItemOrSection[] = Array.isArray(itemsOrSections)
? itemsOrSections
: [itemsOrSections];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { themeDHDefault } from './utils';
import { themeDHDefault } from './themeUtils';

describe('themeDHDefault', () => {
it('should merge Spectrum default with DH custom styles', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { theme } from '@react-spectrum/theme-default';
import { themeSpectrumClassesCommon } from '../theme/theme-spectrum';
import { themeSpectrumClassesCommon } from '../../theme/theme-spectrum';

const { global, light, dark, medium, large } = theme;

Expand Down

0 comments on commit de9cad0

Please sign in to comment.