import React, { ReactElement } from 'react'; import { SideBarProps } from './interface'; import { cssClasses, strings } from '@douyinfe/semi-foundation/sidebar/constants'; import cls from 'classnames'; import BaseComponent from '../_base/baseComponent'; import PropTypes, { ReactNodeLike } from 'prop-types'; import Container from './container'; import Options from './options'; import { pick } from 'lodash'; import { IconClose, IconCopyStroked } from '@douyinfe/semi-icons'; import Button from '../button'; import { CodeItem } from './widget/code'; import { FileItem } from './widget/file'; import copy from 'copy-text-to-clipboard'; import { ToastFactory } from '../toast'; import FileContent from './widget/file'; import CodeContent from './widget/code'; import LocaleConsumer from '../locale/localeConsumer'; import { Locale } from '../locale/interface'; const prefixCls = cssClasses.SIDEBAR; interface SideBarState {} class Sidebar extends BaseComponent { static propTypes = { ...Container.propTypes, mode: PropTypes.string, activeKey: PropTypes.string, options: PropTypes.array, onActiveOptionChange: PropTypes.func, renderMainContent: PropTypes.func, renderDetailHeader: PropTypes.func, renderDetailContent: PropTypes.func, fileEditable: PropTypes.bool, onFileContentChange: PropTypes.func, onBackWard: PropTypes.func, }; static FileContent = FileContent; static CodeContent = CodeContent; static FileItem = FileItem; static CodeItem = CodeItem; static Container = Container; static defaultProps = { mode: strings.MODE.MAIN, fileEditable: true }; containerRef: React.RefObject; ToastInCustomContainer: any; constructor(props: SideBarProps) { super(props); this.containerRef = React.createRef(); this.ToastInCustomContainer = ToastFactory.create({ getPopupContainer: () => this.containerRef.current, }); } renderOption = () => { const { activeKey, options, onActiveOptionChange, renderOptionItem } = this.props; return ; } renderMain = () => { const activeKey = this.props.activeKey; const { renderMainContent } = this.props; return
{this.renderOption()}
{renderMainContent?.(activeKey)}
; } renderDetail = () => { const { renderDetailContent, detailContent = {}, imgUploadProps, fileEditable, onFileContentChange, mode } = this.props; const result = renderDetailContent?.(mode); if (result) { return result; } if (mode === 'code') { return ; } else if (mode === 'file') { return ; } return null; } renderContent = () => { const { mode } = this.props; return mode === strings.MODE.MAIN ? this.renderMain() : this.renderDetail(); } renderTitle = () => { const { title, mode } = this.props; if (mode === strings.MODE.MAIN) { return title; } return null; } onDetailClose = (e: any) => { const { onBackWard } = this.props; onBackWard?.(e, strings.MODE.MAIN); } handleCopyDetailContent = (e: React.MouseEvent, locale: Locale['Sidebar']) => { const { detailContent, mode, onDetailContentCopy } = this.props; const content = detailContent.content; const res = copy(content); res && this.ToastInCustomContainer.success({ content: locale.copySuccess, }); onDetailContentCopy?.(e, content, res); } renderHeader = () => { const { renderDetailHeader, detailContent, mode } = this.props; const result = renderDetailHeader?.(mode, detailContent); if (result) { return result; } return
; } render() { const { mode } = this.props; const containerProps = pick(this.props, ['title', 'style', 'visible', 'motion', 'minWidth', 'maxWidth', 'onCancel', 'afterVisibleChange', 'resizable', 'defaultSize', 'children', 'className', 'showClose']); return {this.renderContent()} ; } } export default Sidebar;