forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.tsx
More file actions
161 lines (149 loc) · 4.95 KB
/
Card.tsx
File metadata and controls
161 lines (149 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { createContext, forwardRef } from 'react';
import styles from '@patternfly/react-styles/css/components/Card/card';
import { css } from '@patternfly/react-styles';
import { useOUIAProps, OUIAProps } from '../../helpers';
export interface CardProps extends React.HTMLProps<HTMLElement>, OUIAProps {
/** Content rendered inside the Card */
children?: React.ReactNode;
/** ID of the Card. Also passed back in the CardHeader onExpand callback. */
id?: string;
/** Additional classes added to the Card */
className?: string;
/** Sets the base component to render. defaults to div */
component?: keyof React.JSX.IntrinsicElements;
/** Modifies the card to include compact styling. Should not be used with isLarge. */
isCompact?: boolean;
/** Flag indicating that the card is selectable. */
isSelectable?: boolean;
/** Flag indicating that the card is clickable and contains some action that triggers on click. */
isClickable?: boolean;
/** Flag indicating whether a card that is either selectable only or both clickable and selectable is
* currently selected and has selected styling.
*/
isSelected?: boolean;
/** Flag indicating whether a card that is either only clickable or that is both clickable and selectable
* is currently clicked and has clicked styling.
*/
isClicked?: boolean;
/** Flag indicating that a clickable or selectable card is disabled. */
isDisabled?: boolean;
/** Modifies the card to be large. Should not be used with isCompact. */
isLarge?: boolean;
/** Cause component to consume the available height of its container */
isFullHeight?: boolean;
/** Modifies the card to include plain styling; this removes border and background */
isPlain?: boolean;
/** Modifies the card to include glass styling */
isGlass?: boolean;
/** Flag indicating if a card is expanded. Modifies the card to be expandable. */
isExpanded?: boolean;
/** Card background color variant */
variant?: 'default' | 'secondary';
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
/** @hide Forwarded ref */
innerRef?: React.Ref<any>;
}
interface CardContextProps {
cardId: string;
isExpanded: boolean;
isClickable: boolean;
isSelectable: boolean;
isSelected: boolean;
isClicked: boolean;
isDisabled: boolean;
}
export const CardContext = createContext<Partial<CardContextProps>>({
cardId: '',
isExpanded: false,
isClickable: false,
isSelectable: false,
isSelected: false,
isClicked: false,
isDisabled: false
});
const CardBase: React.FunctionComponent<CardProps> = ({
children,
id = '',
className,
component = 'div',
isCompact = false,
isSelectable = false,
isClickable = false,
isDisabled = false,
isSelected = false,
isClicked = false,
isExpanded = false,
isLarge = false,
isFullHeight = false,
isPlain = false,
isGlass = false,
variant = 'default',
ouiaId,
ouiaSafe = true,
innerRef,
...props
}: CardProps) => {
const Component = component as any;
const ouiaProps = useOUIAProps(Card.displayName, ouiaId, ouiaSafe);
if (isCompact && isLarge) {
// eslint-disable-next-line no-console
console.warn('Card: Cannot use isCompact with isLarge. Defaulting to isCompact');
isLarge = false;
}
const getSelectableModifiers = () => {
if (isSelectable && isClickable) {
return css(
styles.modifiers.selectable,
styles.modifiers.clickable,
(isSelected || isClicked) && styles.modifiers.current
);
}
if (isSelectable) {
return css(styles.modifiers.selectable, isSelected && styles.modifiers.selected);
}
if (isClickable) {
return css(styles.modifiers.clickable, isClicked && styles.modifiers.current);
}
return '';
};
return (
<CardContext.Provider
value={{
cardId: id,
isExpanded,
isClickable,
isSelectable,
isSelected,
isClicked,
isDisabled
}}
>
<Component
ref={innerRef}
id={id}
className={css(
styles.card,
isCompact && styles.modifiers.compact,
isExpanded && styles.modifiers.expanded,
isLarge && styles.modifiers.displayLg,
isFullHeight && styles.modifiers.fullHeight,
isPlain && styles.modifiers.plain,
isGlass && styles.modifiers.glass,
variant === 'secondary' && styles.modifiers.secondary,
getSelectableModifiers(),
isDisabled && styles.modifiers.disabled,
className
)}
{...props}
{...ouiaProps}
>
{children}
</Component>
</CardContext.Provider>
);
};
export const Card = forwardRef((props: CardProps, ref: React.Ref<any>) => <CardBase {...props} innerRef={ref} />);
Card.displayName = 'Card';