Debian Codegraph @

node/@patternfly/react-core/src/components/DataList/DataList.tsx

raw
1 import { createContext, forwardRef } from 'react';
2 import { css } from '@patternfly/react-styles';
3 import styles from '@patternfly/react-styles/css/components/DataList/data-list';
4
5 const gridBreakpointClasses = {
6 none: styles.modifiers.gridNone,
7 always: 'pf-m-grid', // Placeholder per https://github.com/patternfly/patternfly-react/issues/4965#issuecomment-704984236
8 sm: styles.modifiers.gridSm,
9 md: styles.modifiers.gridMd,
10 lg: styles.modifiers.gridLg,
11 xl: styles.modifiers.gridXl,
12 '2xl': styles.modifiers.grid_2xl
13 };
14
15 export enum DataListWrapModifier {
16 nowrap = 'nowrap',
17 truncate = 'truncate',
18 breakWord = 'breakWord'
19 }
20
21 export interface DataListProps extends React.HTMLProps<HTMLUListElement> {
22 /** Content rendered inside the data list list */
23 children?: React.ReactNode;
24 /** Additional classes added to the data list list */
25 className?: string;
26 /** Adds accessible text to the data list list */
27 'aria-label': string;
28 /** Optional callback to make data list selectable, fired when data list item selected */
29 onSelectDataListItem?: (event: React.MouseEvent | React.KeyboardEvent, id: string) => void;
30 /** Id of data list item currently selected */
31 selectedDataListItemId?: string;
32 /** Flag indicating if data list should have compact styling */
33 isCompact?: boolean;
34 /** @beta Flag indicating if data list should have plain styling with a transparent background */
35 isPlain?: boolean;
36 /** @beta Flag to prevent the data list from automatically applying plain styling when glass theme is enabled. */
37 isNoPlainOnGlass?: boolean;
38 /** Specifies the grid breakpoints */
39 gridBreakpoint?: 'none' | 'always' | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
40 /** Determines which wrapping modifier to apply to the data list */
41 wrapModifier?: DataListWrapModifier | 'nowrap' | 'truncate' | 'breakWord';
42 /** Object that causes the data list to render hidden inputs which improve selectable item a11y */
43 onSelectableRowChange?: (event: React.FormEvent<HTMLInputElement>, id: string) => void;
44 /** @hide custom ref of the data list */
45 innerRef?: React.RefObject<HTMLUListElement | null>;
46 }
47
48 interface DataListContextProps {
49 isSelectable: boolean;
50 selectedDataListItemId: string;
51 updateSelectedDataListItem: (event: React.MouseEvent | React.KeyboardEvent, id: string) => void;
52 onSelectableRowChange?: (event: React.FormEvent<HTMLInputElement>, id: string) => void;
53 }
54
55 export const DataListContext = createContext<Partial<DataListContextProps>>({
56 isSelectable: false
57 });
58
59 export const DataListBase: React.FunctionComponent<DataListProps> = ({
60 children = null,
61 className = '',
62 'aria-label': ariaLabel,
63 onSelectDataListItem,
64 selectedDataListItemId = '',
65 isCompact = false,
66 isPlain = false,
67 isNoPlainOnGlass = false,
68 gridBreakpoint = 'md',
69 wrapModifier = null,
70 onSelectableRowChange,
71 innerRef,
72 ...props
73 }: DataListProps) => {
74 const isSelectable = onSelectDataListItem !== undefined;
75
76 const updateSelectedDataListItem = (event: React.MouseEvent | React.KeyboardEvent, id: string) => {
77 onSelectDataListItem(event, id);
78 };
79
80 return (
81 <DataListContext.Provider
82 value={{
83 isSelectable,
84 selectedDataListItemId,
85 updateSelectedDataListItem,
86 onSelectableRowChange
87 }}
88 >
89 <ul
90 className={css(
91 styles.dataList,
92 isCompact && styles.modifiers.compact,
93 isPlain && styles.modifiers.plain,
94 isNoPlainOnGlass && styles.modifiers.noPlainOnGlass,
95 gridBreakpointClasses[gridBreakpoint],
96 wrapModifier && styles.modifiers[wrapModifier],
97 className
98 )}
99 style={props.style}
100 role="list"
101 aria-label={ariaLabel}
102 ref={innerRef}
103 {...props}
104 >
105 {children}
106 </ul>
107 </DataListContext.Provider>
108 );
109 };
110
111 DataListBase.displayName = 'DataListBase';
112
113 export const DataList = forwardRef((props: DataListProps, ref: React.Ref<HTMLUListElement>) => (
114 <DataListBase innerRef={ref as React.MutableRefObject<any>} {...props} />
115 ));
116
117 DataList.displayName = 'DataList';