From 3a7d1126e76bc336f5c2cd5d7306e100155714cf Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 20 May 2022 22:33:14 +0800 Subject: [PATCH] style: add AutoFolder --- .../components/AutoFolder/index.stories.tsx | 35 ++++++++++++ .../design/components/AutoFolder/index.tsx | 57 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 packages/design/components/AutoFolder/index.stories.tsx create mode 100644 packages/design/components/AutoFolder/index.tsx diff --git a/packages/design/components/AutoFolder/index.stories.tsx b/packages/design/components/AutoFolder/index.stories.tsx new file mode 100644 index 00000000..61cbdb04 --- /dev/null +++ b/packages/design/components/AutoFolder/index.stories.tsx @@ -0,0 +1,35 @@ +import React from 'react'; +import { ComponentStory, ComponentMeta } from '@storybook/react'; + +import { AutoFolder } from '.'; + +export default { + title: 'Tailchat/AutoFolder', + component: AutoFolder, + argTypes: {}, +} as ComponentMeta; + +const Template: ComponentStory = (args) => ( + +); + +export const Default = Template.bind({}); +Default.args = { + maxHeight: 100, + children: ( +
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
foooooo
+
+ ), +}; diff --git a/packages/design/components/AutoFolder/index.tsx b/packages/design/components/AutoFolder/index.tsx new file mode 100644 index 00000000..5d0f1c69 --- /dev/null +++ b/packages/design/components/AutoFolder/index.tsx @@ -0,0 +1,57 @@ +import React, { useEffect, useRef, useState } from 'react'; + +interface AutoFolderProps { + maxHeight: number; + showFullText?: string; + backgroundColor?: string; +} +export const AutoFolder: React.FC = React.memo((props) => { + const { + maxHeight, + showFullText = 'More', + backgroundColor = 'rgba(0,0,0,0)', + } = props; + const [isShowFull, setIsShowFull] = useState(false); + const contentRef = useRef(); + + useEffect(() => { + if (contentRef.current) { + if (contentRef.current.scrollHeight > maxHeight) { + setIsShowFull(false); + } else { + setIsShowFull(true); + } + } + }, [props.maxHeight]); + + return ( +
+
{props.children}
+ + {!isShowFull && ( +
setIsShowFull(true)} + > + {showFullText} +
+ )} +
+ ); +}); +AutoFolder.displayName = 'AutoFolder';