ant-design/components/layout/Sider.tsx
陈帅 0d1b1c40a6
merge featrue into master (#30636)
* feat: add Table expandable fixed (#29959)

* fix: Use flex gap of space (#30023)

* fix: use flex gap when supported

* test: update snapshot

* refactor: Use single hooks

* feat: Allow breadcrumb component in PageHeader (#30019)

* Allow breadcrumb component in PageHeader

* Allow breadcrumb component in PageHeader

* Allow breadcrumb component in PageHeader

* Rename variable

rename var from _breadcrumbRender to breadcrumbRenderDomFromProps

* feat: add onChange for Statistic.Countdown (#30265)

* feat: add onChange for countdown

* update the demo

* feat(upload): add onDrop (#30319)

* feat(upload): Add onDrop

* Replace "if prop" logic with existential operator

* Remove redundant conditional

* feat(upload): itemRender add actions params (#30236)

* feat(upload): itemRender add actions params

* chore: optimize type definition

* chore: update doc

* chore: rename actions

* chore: trigger ci

* chore: rename method name of actions

* feat: Add missing dutch translations (#30389)

* feat: Add missing dutch translations

* fix: Translate remaining english values

* fix: Update snapshot for ui tests

* test: increase code coverage to 100% (#30415)

* test: fix Space code coverage

* test: should use nl_BE locale for DatePicker

* fix: Switch tabIndex type (#30416)

* feat: updated Romanian internationalization (#30419)

* feat: updated Romanian internationalization

* fixed lint error

* feat: Menu support accessibility & keyboard access (#30382)

* chore: Use focus style

* fix: prefixCls

* fix: prefixCls

* fix: inline tooltip

* fix: inlineCollapse logic

* fix: ts definition

* test: Update snapshot

* test: Update snapshot

* fix: dropdown logic

* test: Update snapshot

* test: Fix some test  case

* bump rc-menu

* test: More test case

* fix test finder

* test: fix test case

* test: Update snapshot

* test: Update snapshot

* chore: Update ssr effect

* test: Update ConfigProvider snapshot

* test: Fix Table Filter test case

* test: Fix table test case

* chore: Update style

* chore: beauti css

* bump rc-menu

* test: update snapshot

* test: update snapshot

* test: Fix menu test

* test: Fix test case

* test: Coverage

* chore: clean up

* bump rc-menu

* ehance accessibility style

* feat(radioGroup): support data-* and aria-* props (#30507)

close #30501

* feat: Typography add italic type (#30458)

* Typography增加斜体字支持

* update snapshot

* 文档添加版本号

Co-authored-by: lidahao <lidahao@sisyphe.com.cn>

* chore: alpha Menu fix merge (#30546)

* chore: Update script

* bump alpha version

* chore: Update script desc

* chore: bump rc-tabs & rc-mentions

* chore: Adjust style

* chore: 4.16.0-alpha.1

* test: Fix mention test case

* fix: sider of layout width style

* chore: bump 4.16.0-alpha.2

* fix: Tabs tabBarGutter should work as expected (#30545)

close #30526

* feat: Table summary support sticky mode (#30631)

* chore: Bump rc-table

* feat: Patch summary fixed color

* fix: style className

* test: Update snapshot

Co-authored-by: xrkffgg <xrkffgg@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: 二货机器人 <smith3816@gmail.com>
Co-authored-by: gepd <guillermoepd@hotmail.com>
Co-authored-by: appleshell <appleshell@outlook.com>
Co-authored-by: Eric Bonow <ebonow@hotmail.com>
Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
Co-authored-by: Kermit <kermitlx@outlook.com>
Co-authored-by: Lewis <lewisfidlers@gmail.com>
Co-authored-by: afc163 <afc163@gmail.com>
Co-authored-by: Ștefan Filip <stefy.filip@gmail.com>
Co-authored-by: vldh <alwaysloseall@sina.com>
Co-authored-by: lidahao <lidahao@sisyphe.com.cn>
2021-05-24 16:24:00 +08:00

232 lines
6.7 KiB
TypeScript

import * as React from 'react';
import { useContext, useRef, useState, useEffect } from 'react';
import classNames from 'classnames';
import omit from 'rc-util/lib/omit';
import BarsOutlined from '@ant-design/icons/BarsOutlined';
import RightOutlined from '@ant-design/icons/RightOutlined';
import LeftOutlined from '@ant-design/icons/LeftOutlined';
import { LayoutContext } from './layout';
import { ConfigContext } from '../config-provider';
import isNumeric from '../_util/isNumeric';
const dimensionMaxMap = {
xs: '479.98px',
sm: '575.98px',
md: '767.98px',
lg: '991.98px',
xl: '1199.98px',
xxl: '1599.98px',
};
export interface SiderContextProps {
siderCollapsed?: boolean;
}
export const SiderContext: React.Context<SiderContextProps> = React.createContext({});
export type CollapseType = 'clickTrigger' | 'responsive';
export type SiderTheme = 'light' | 'dark';
export interface SiderProps extends React.HTMLAttributes<HTMLDivElement> {
prefixCls?: string;
collapsible?: boolean;
collapsed?: boolean;
defaultCollapsed?: boolean;
reverseArrow?: boolean;
onCollapse?: (collapsed: boolean, type: CollapseType) => void;
zeroWidthTriggerStyle?: React.CSSProperties;
trigger?: React.ReactNode;
width?: number | string;
collapsedWidth?: number | string;
breakpoint?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
theme?: SiderTheme;
onBreakpoint?: (broken: boolean) => void;
}
export interface SiderState {
collapsed?: boolean;
below: boolean;
}
const generateId = (() => {
let i = 0;
return (prefix: string = '') => {
i += 1;
return `${prefix}${i}`;
};
})();
const Sider = React.forwardRef<HTMLDivElement, SiderProps>(
(
{
prefixCls: customizePrefixCls,
className,
trigger,
children,
defaultCollapsed = false,
theme = 'dark',
style = {},
collapsible = false,
reverseArrow = false,
width = 200,
collapsedWidth = 80,
zeroWidthTriggerStyle,
breakpoint,
onCollapse,
onBreakpoint,
...props
},
ref,
) => {
const { siderHook } = useContext(LayoutContext);
const [collapsed, setCollapsed] = useState(
'collapsed' in props ? props.collapsed : defaultCollapsed,
);
const [below, setBelow] = useState(false);
useEffect(() => {
if ('collapsed' in props) {
setCollapsed(props.collapsed);
}
}, [props.collapsed]);
const handleSetCollapsed = (value: boolean, type: CollapseType) => {
if (!('collapsed' in props)) {
setCollapsed(value);
}
onCollapse?.(value, type);
};
// ========================= Responsive =========================
const responsiveHandlerRef = useRef<(mql: MediaQueryListEvent | MediaQueryList) => void>();
responsiveHandlerRef.current = (mql: MediaQueryListEvent | MediaQueryList) => {
setBelow(mql.matches);
onBreakpoint?.(mql.matches);
if (collapsed !== mql.matches) {
handleSetCollapsed(mql.matches, 'responsive');
}
};
useEffect(() => {
function responsiveHandler(mql: MediaQueryListEvent | MediaQueryList) {
return responsiveHandlerRef.current!(mql);
}
let mql: MediaQueryList;
if (typeof window !== 'undefined') {
const { matchMedia } = window;
if (matchMedia! && breakpoint && breakpoint in dimensionMaxMap) {
mql = matchMedia(`(max-width: ${dimensionMaxMap[breakpoint]})`);
try {
mql.addEventListener('change', responsiveHandler);
} catch (error) {
mql.addListener(responsiveHandler);
}
responsiveHandler(mql);
}
}
return () => {
try {
mql?.removeEventListener('change', responsiveHandler);
} catch (error) {
mql?.removeListener(responsiveHandler);
}
};
}, []);
useEffect(() => {
const uniqueId = generateId('ant-sider-');
siderHook.addSider(uniqueId);
return () => siderHook.removeSider(uniqueId);
}, []);
const toggle = () => {
handleSetCollapsed(!collapsed, 'clickTrigger');
};
const { getPrefixCls } = useContext(ConfigContext);
const renderSider = () => {
const prefixCls = getPrefixCls('layout-sider', customizePrefixCls);
const divProps = omit(props, ['collapsed']);
const rawWidth = collapsed ? collapsedWidth : width;
// use "px" as fallback unit for width
const siderWidth = isNumeric(rawWidth) ? `${rawWidth}px` : String(rawWidth);
// special trigger when collapsedWidth == 0
const zeroWidthTrigger =
parseFloat(String(collapsedWidth || 0)) === 0 ? (
<span
onClick={toggle}
className={classNames(
`${prefixCls}-zero-width-trigger`,
`${prefixCls}-zero-width-trigger-${reverseArrow ? 'right' : 'left'}`,
)}
style={zeroWidthTriggerStyle}
>
{trigger || <BarsOutlined />}
</span>
) : null;
const iconObj = {
expanded: reverseArrow ? <RightOutlined /> : <LeftOutlined />,
collapsed: reverseArrow ? <LeftOutlined /> : <RightOutlined />,
};
const status = collapsed ? 'collapsed' : 'expanded';
const defaultTrigger = iconObj[status];
const triggerDom =
trigger !== null
? zeroWidthTrigger || (
<div
className={`${prefixCls}-trigger`}
onClick={toggle}
style={{ width: siderWidth }}
>
{trigger || defaultTrigger}
</div>
)
: null;
const divStyle = {
...style,
flex: `0 0 ${siderWidth}`,
maxWidth: siderWidth, // Fix width transition bug in IE11
minWidth: siderWidth, // https://github.com/ant-design/ant-design/issues/6349
width: siderWidth,
};
const siderCls = classNames(
prefixCls,
`${prefixCls}-${theme}`,
{
[`${prefixCls}-collapsed`]: !!collapsed,
[`${prefixCls}-has-trigger`]: collapsible && trigger !== null && !zeroWidthTrigger,
[`${prefixCls}-below`]: !!below,
[`${prefixCls}-zero-width`]: parseFloat(siderWidth) === 0,
},
className,
);
return (
<aside className={siderCls} {...divProps} style={divStyle} ref={ref}>
<div className={`${prefixCls}-children`}>{children}</div>
{collapsible || (below && zeroWidthTrigger) ? triggerDom : null}
</aside>
);
};
return (
<SiderContext.Provider
value={{
siderCollapsed: collapsed,
}}
>
{renderSider()}
</SiderContext.Provider>
);
},
);
Sider.displayName = 'Sider';
export default Sider;