-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChatContentCollapsed.tsx
More file actions
64 lines (57 loc) · 1.95 KB
/
Copy pathChatContentCollapsed.tsx
File metadata and controls
64 lines (57 loc) · 1.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
import React from "react";
import { Markdown } from "../../cmem/markdown/Markdown";
import { IconButton } from "../Icon/IconButton";
import { TextReducer, TextReducerProps } from "../TextReducer/TextReducer";
import { ChatContentProps } from "./ChatContent";
export interface ChatContentCollapsedProps {
children: React.ReactElement<ChatContentProps>;
/**
* Set this to `false` if the compoment should initally start in an expanded state.
*/
collapsed?: boolean;
/**
* Use this to set extra `TextReducer` properties.
* This is used to create the collapsed variant of the given content.
*/
textReducerProps?: Omit<TextReducerProps, "children">;
/**
* Text for collapse button.
*/
textCollapse?: string;
/**
* Text for expand button.
*/
textExpand?: string;
}
/**
* Adds an auto collapsing feature for convenience to `ChatContent`.
*/
export const ChatContentCollapsed = ({
children,
collapsed = true,
textReducerProps = {},
textCollapse = "Collapse",
textExpand = "Expand",
}: ChatContentCollapsedProps) => {
const [displayCollapsed, setDispayCollapsed] = React.useState<boolean>(collapsed);
const childrenAsTextline = (
<TextReducer useOverflowTextWrapper {...textReducerProps}>
{typeof children.props.children === "string" ? (
<Markdown>{children.props.children}</Markdown>
) : (
children.props.children
)}
</TextReducer>
);
return React.cloneElement(children, {
children: displayCollapsed ? childrenAsTextline : children.props.children,
actionButton: (
<IconButton
text={displayCollapsed ? textExpand : textCollapse}
name={displayCollapsed ? "toggler-showmore" : "toggler-showless"}
onClick={() => setDispayCollapsed(!displayCollapsed)}
/>
),
});
};
export default ChatContentCollapsed;