refactor: 抽象面板包装器逻辑,使其可以方便复用

pull/13/head
moonrailgun 4 years ago
parent d8b73722d5
commit 8c1cd74355

@ -0,0 +1,63 @@
import React, { useState } from 'react';
import { PanelCommonHeader } from '../common/Header';
import { Icon } from '@iconify/react';
import { Button } from 'antd';
import clsx from 'clsx';
interface RightPanelType {
name: string;
panel: React.ReactNode;
}
/**
*
*/
interface CommonPanelWrapperProps {
header: React.ReactNode;
actions: (
setRightPanel: (info: RightPanelType) => void
) => React.ReactElement[];
}
export const CommonPanelWrapper: React.FC<CommonPanelWrapperProps> = React.memo(
(props) => {
const [rightPanel, setRightPanel] = useState<RightPanelType>();
return (
<div className="w-full h-full flex">
{/* 主面板 */}
<div className="flex flex-col overflow-hidden flex-1">
<PanelCommonHeader actions={props.actions(setRightPanel)}>
{props.header}
</PanelCommonHeader>
<div className="flex-1 overflow-hidden">{props.children}</div>
</div>
{/* 右侧面板 */}
<div
className={clsx(
'transition-all overflow-hidden border-l border-black border-opacity-20',
{
'w-96 mobile:w-full': rightPanel,
'w-0': !rightPanel,
}
)}
>
<PanelCommonHeader
actions={[
// 关闭按钮
<Button
key="close"
icon={<Icon className="anticon text-2xl" icon="mdi:close" />}
onClick={() => setRightPanel(undefined)}
/>,
]}
>
{rightPanel?.name}
</PanelCommonHeader>
{rightPanel?.panel}
</div>
</div>
);
}
);
CommonPanelWrapper.displayName = 'CommonPanelWrapper';

@ -1,11 +1,10 @@
import React, { useState } from 'react'; import React from 'react';
import { t, useGroupPanel } from 'tailchat-shared'; import { t, useGroupPanel } from 'tailchat-shared';
import { PanelCommonHeader } from '../common/Header';
import _isNil from 'lodash/isNil'; import _isNil from 'lodash/isNil';
import { Icon } from '@iconify/react'; import { Icon } from '@iconify/react';
import { Button } from 'antd'; import { Button } from 'antd';
import { MembersPanel } from './MembersPanel'; import { MembersPanel } from './MembersPanel';
import clsx from 'clsx'; import { CommonPanelWrapper } from '../common/Wrapper';
/** /**
* *
@ -17,19 +16,15 @@ interface GroupPanelWrapperProps {
export const GroupPanelWrapper: React.FC<GroupPanelWrapperProps> = React.memo( export const GroupPanelWrapper: React.FC<GroupPanelWrapperProps> = React.memo(
(props) => { (props) => {
const panelInfo = useGroupPanel(props.groupId, props.panelId); const panelInfo = useGroupPanel(props.groupId, props.panelId);
const [rightPanel, setRightPanel] =
useState<{ name: string; panel: React.ReactNode }>();
if (_isNil(panelInfo)) { if (_isNil(panelInfo)) {
return null; return null;
} }
return ( return (
<div className="w-full h-full flex"> <CommonPanelWrapper
{/* 主面板 */} header={panelInfo.name}
<div className="flex flex-col overflow-hidden flex-1"> actions={(setRightPanel) => [
<PanelCommonHeader
actions={[
<Button <Button
key="members" key="members"
icon={ icon={
@ -47,35 +42,8 @@ export const GroupPanelWrapper: React.FC<GroupPanelWrapperProps> = React.memo(
/>, />,
]} ]}
> >
{panelInfo.name} {props.children}
</PanelCommonHeader> </CommonPanelWrapper>
<div className="flex-1 overflow-hidden">{props.children}</div>
</div>
{/* 右侧面板 */}
<div
className={clsx(
'transition-all overflow-hidden border-l border-black border-opacity-20',
{
'w-96 mobile:w-full': rightPanel,
'w-0': !rightPanel,
}
)}
>
<PanelCommonHeader
actions={[
<Button
key="members"
icon={<Icon className="anticon text-2xl" icon="mdi:close" />}
onClick={() => setRightPanel(undefined)}
/>,
]}
>
{rightPanel?.name}
</PanelCommonHeader>
{rightPanel?.panel}
</div>
</div>
); );
} }
); );

@ -7,8 +7,6 @@ import {
useDMConverseName, useDMConverseName,
} from 'tailchat-shared'; } from 'tailchat-shared';
import { PanelCommonHeader } from '../common/Header'; import { PanelCommonHeader } from '../common/Header';
import _isNil from 'lodash/isNil';
import { LoadingSpinner } from '@/components/LoadingSpinner';
const ConversePanelHeader: React.FC<{ converse: ChatConverseState }> = const ConversePanelHeader: React.FC<{ converse: ChatConverseState }> =
React.memo(({ converse }) => { React.memo(({ converse }) => {

Loading…
Cancel
Save