Debian Codegraph @

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

raw
1 import { Children, Component, createContext } from 'react';
2 import { css } from '@patternfly/react-styles';
3 import styles from '@patternfly/react-styles/css/components/SimpleList/simple-list';
4 import { SimpleListGroup } from './SimpleListGroup';
5 import { SimpleListItemProps } from './SimpleListItem';
6
7 export interface SimpleListProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onSelect'> {
8 /** Content rendered inside the SimpleList */
9 children?: React.ReactNode;
10 /** Additional classes added to the SimpleList container */
11 className?: string;
12 /** Callback when an item is selected */
13 onSelect?: (
14 ref: React.RefObject<HTMLButtonElement | null> | React.RefObject<HTMLAnchorElement | null>,
15 props: SimpleListItemProps
16 ) => void;
17 /** Indicates whether component is controlled by its internal state */
18 isControlled?: boolean;
19 /** aria-label for the <ul> element that wraps the SimpleList items. */
20 'aria-label'?: string;
21 }
22
23 export interface SimpleListState {
24 /** Ref of the current SimpleListItem */
25 currentRef: React.RefObject<HTMLButtonElement | null> | React.RefObject<HTMLAnchorElement | null>;
26 }
27
28 interface SimpleListContextProps {
29 currentRef: React.RefObject<HTMLButtonElement | null> | React.RefObject<HTMLAnchorElement | null>;
30 updateCurrentRef: (
31 id: React.RefObject<HTMLButtonElement | null> | React.RefObject<HTMLAnchorElement | null>,
32 props: SimpleListItemProps
33 ) => void;
34 isControlled: boolean;
35 }
36
37 export const SimpleListContext = createContext<Partial<SimpleListContextProps>>({});
38
39 class SimpleList extends Component<SimpleListProps, SimpleListState> {
40 static displayName = 'SimpleList';
41 state = {
42 currentRef: null as React.RefObject<HTMLButtonElement | null> | React.RefObject<HTMLAnchorElement | null>
43 };
44
45 static defaultProps: SimpleListProps = {
46 children: null as React.ReactNode,
47 className: '',
48 isControlled: true
49 };
50
51 handleCurrentUpdate = (
52 newCurrentRef: React.RefObject<HTMLButtonElement | null> | React.RefObject<HTMLAnchorElement | null>,
53 itemProps: SimpleListItemProps
54 ) => {
55 this.setState({ currentRef: newCurrentRef });
56 const { onSelect } = this.props;
57 onSelect && onSelect(newCurrentRef, itemProps);
58 };
59
60 render() {
61 // eslint-disable-next-line @typescript-eslint/no-unused-vars
62 const { children, className, onSelect, isControlled, 'aria-label': ariaLabel, ...props } = this.props;
63
64 let isGrouped = false;
65 if (children) {
66 isGrouped = (Children.toArray(children)[0] as React.ReactElement<any>).type === SimpleListGroup;
67 }
68
69 return (
70 <SimpleListContext.Provider
71 value={{
72 currentRef: this.state.currentRef,
73 updateCurrentRef: this.handleCurrentUpdate,
74 isControlled
75 }}
76 >
77 <div className={css(styles.simpleList, className)} {...props}>
78 {isGrouped && children}
79 {!isGrouped && (
80 <ul className={css('pf-v6-c-simple-list__list')} role="list" aria-label={ariaLabel}>
81 {children}
82 </ul>
83 )}
84 </div>
85 </SimpleListContext.Provider>
86 );
87 }
88 }
89
90 export { SimpleList };