forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Item icon and description rendering (deephaven#1890)
- Loading branch information
Showing
14 changed files
with
261 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './itemUtils'; | ||
export * from './itemWrapperUtils'; | ||
export * from './themeUtils'; | ||
export * from './useRenderItemFlags'; | ||
export * from './useRenderNormalizedItem'; | ||
export * from './useStringifiedMultiSelection'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/components/src/spectrum/utils/itemWrapperUtils.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { ReactNode } from 'react'; | ||
import { Icon } from '@adobe/react-spectrum'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { dh as dhIcons } from '@deephaven/icons'; | ||
import { NON_BREAKING_SPACE } from '@deephaven/utils'; | ||
import { Text } from '../Text'; | ||
import { ItemIconSlot } from './itemUtils'; | ||
|
||
/** | ||
* If the given content is a primitive type, wrap it in a Text component. | ||
* @param content The content to wrap | ||
* @param slot The slot to use for the Text component | ||
* @returns The wrapped content or original content if not a primitive type | ||
*/ | ||
export function wrapPrimitiveWithText( | ||
content?: ReactNode, | ||
slot?: string | ||
): ReactNode { | ||
// eslint-disable-next-line no-param-reassign | ||
content = content ?? ''; | ||
|
||
if (['string', 'boolean', 'number'].includes(typeof content)) { | ||
return ( | ||
<Text slot={slot}> | ||
{content === '' ? NON_BREAKING_SPACE : String(content)} | ||
</Text> | ||
); | ||
} | ||
|
||
return content; | ||
} | ||
|
||
/** | ||
* If the given content is a string, wrap it in an Icon component. Otherwise, | ||
* return the original content. If the key is not found in the dhIcons object, | ||
* the vsBlank icon will be used. | ||
* @param maybeIconKey The content to wrap | ||
* @param slot The slot to use for the Icon component | ||
* @returns The wrapped content or original content if not a string | ||
*/ | ||
export function wrapIcon( | ||
maybeIconKey: ReactNode, | ||
slot: ItemIconSlot | ||
): ReactNode { | ||
// eslint-disable-next-line no-param-reassign | ||
maybeIconKey = maybeIconKey ?? ''; | ||
|
||
if (typeof maybeIconKey !== 'string') { | ||
return maybeIconKey; | ||
} | ||
|
||
return ( | ||
<Icon slot={slot}> | ||
<FontAwesomeIcon icon={dhIcons[maybeIconKey] ?? dhIcons.vsBlank} /> | ||
</Icon> | ||
); | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/components/src/spectrum/utils/useRenderItemFlags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useMemo } from 'react'; | ||
import { | ||
doesAnyItemHaveProp, | ||
NormalizedItem, | ||
NormalizedSection, | ||
} from './itemUtils'; | ||
|
||
export interface UseRenderItemFlagsOptions { | ||
normalizedItems: (NormalizedSection | NormalizedItem)[]; | ||
showItemIcons?: boolean; | ||
showItemDescriptions?: boolean; | ||
} | ||
|
||
export interface RenderItemFlags { | ||
showItemIcons: boolean; | ||
showItemDescriptions: boolean; | ||
} | ||
|
||
/** | ||
* Get flags for rendering items. Icons and descriptions need to be hidden or | ||
* shown for all items in a list. If `showItemIcons` or `showItemDescriptions` | ||
* are explicitly provided, use those values. Otherwise, if any item is found | ||
* with an icon or description, set the respective flag to true. | ||
* @param normalizedItems The normalized items to check for icons and descriptions | ||
* @param showItemIcons Whether to show item icons by default | ||
* @param showItemDescriptions Whether to show item descriptions by default | ||
* @returns Flags for rendering items | ||
*/ | ||
export function useRenderItemFlags({ | ||
normalizedItems, | ||
showItemIcons: showItemIconsDefault, | ||
showItemDescriptions: showItemDescriptionsDefault, | ||
}: UseRenderItemFlagsOptions): RenderItemFlags { | ||
const showItemIcons = useMemo( | ||
() => showItemIconsDefault ?? doesAnyItemHaveProp(normalizedItems, 'icon'), | ||
[normalizedItems, showItemIconsDefault] | ||
); | ||
|
||
const showItemDescriptions = useMemo( | ||
() => | ||
showItemDescriptionsDefault ?? | ||
doesAnyItemHaveProp(normalizedItems, 'description'), | ||
[normalizedItems, showItemDescriptionsDefault] | ||
); | ||
|
||
return { showItemIcons, showItemDescriptions }; | ||
} | ||
|
||
export default useRenderItemFlags; |
Oops, something went wrong.