|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { TestableComponent } from "../../components/interfaces"; |
| 4 | +import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 5 | + |
| 6 | +import { FlexibleLayoutContainer, FlexibleLayoutContainerProps, FlexibleLayoutItem } from "./../FlexibleLayout"; |
| 7 | +import { Spacing, SpacingProps } from "./../Separation/Spacing"; |
| 8 | +import { ChatFieldProps } from "./ChatField"; |
| 9 | + |
| 10 | +export interface ChatAreaProps |
| 11 | + extends Omit<FlexibleLayoutContainerProps, "vertical" | "noEqualItemSpace">, |
| 12 | + TestableComponent { |
| 13 | + /** |
| 14 | + * The inut field for the chat. |
| 15 | + */ |
| 16 | + chatField?: React.ReactElement<ChatFieldProps>; |
| 17 | + /** |
| 18 | + * Set the position of the chat field. |
| 19 | + */ |
| 20 | + chatFieldPosition?: "top" | "bottom"; |
| 21 | + /** |
| 22 | + * Sets the maximum width for chat contents and input. |
| 23 | + */ |
| 24 | + contentWidth?: "small" | "medium" | "large" | "full"; |
| 25 | + /** |
| 26 | + * Put in chat content in a list and add spacings automatically. |
| 27 | + * Works best if each child represents one chat content item. |
| 28 | + */ |
| 29 | + autoSpacingSize?: SpacingProps["size"]; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Component to display a full chat, containing chat content bubbles and text input. |
| 34 | + */ |
| 35 | +export const ChatArea = ({ |
| 36 | + children, |
| 37 | + className, |
| 38 | + chatField, |
| 39 | + chatFieldPosition = "bottom", |
| 40 | + contentWidth = "medium", |
| 41 | + autoSpacingSize, |
| 42 | + gapSize = "medium", |
| 43 | + ...otherFlexibleLayoutContainerProps |
| 44 | +}: ChatAreaProps) => { |
| 45 | + return ( |
| 46 | + <FlexibleLayoutContainer |
| 47 | + className={ |
| 48 | + `${eccgui}-chat__area` + ` ${eccgui}-chat__area--${contentWidth}` + (className ? ` ${className}` : "") |
| 49 | + } |
| 50 | + vertical |
| 51 | + noEqualItemSpace |
| 52 | + gapSize={gapSize} |
| 53 | + {...otherFlexibleLayoutContainerProps} |
| 54 | + > |
| 55 | + {chatField && ( |
| 56 | + <FlexibleLayoutItem |
| 57 | + className={`${eccgui}-chat__area-contents`} |
| 58 | + growFactor={0} |
| 59 | + shrinkFactor={0} |
| 60 | + style={chatFieldPosition === "bottom" ? { order: 1 } : undefined} |
| 61 | + > |
| 62 | + {chatField} |
| 63 | + </FlexibleLayoutItem> |
| 64 | + )} |
| 65 | + <FlexibleLayoutItem |
| 66 | + className={`${eccgui}-chat__area-contents`} |
| 67 | + style={ |
| 68 | + otherFlexibleLayoutContainerProps.useAbsoluteSpace |
| 69 | + ? { |
| 70 | + overflow: "auto", |
| 71 | + minHeight: 0, |
| 72 | + paddingRight: "0.25rem", |
| 73 | + } |
| 74 | + : undefined |
| 75 | + } |
| 76 | + > |
| 77 | + {autoSpacingSize && children ? ( |
| 78 | + <ul> |
| 79 | + {React.Children.toArray(children).map((child) => ( |
| 80 | + <li> |
| 81 | + {child} |
| 82 | + <Spacing size={autoSpacingSize} /> |
| 83 | + </li> |
| 84 | + ))} |
| 85 | + </ul> |
| 86 | + ) : ( |
| 87 | + children |
| 88 | + )} |
| 89 | + </FlexibleLayoutItem> |
| 90 | + </FlexibleLayoutContainer> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +export default ChatArea; |
0 commit comments