ant-design/components/divider/index.tsx

117 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
import { devUseWarning } from '../_util/warning';
import { ConfigContext } from '../config-provider';
import useStyle from './style';
2017-11-21 20:48:22 +08:00
export interface DividerProps {
prefixCls?: string;
type?: 'horizontal' | 'vertical';
orientation?: 'left' | 'right' | 'center';
orientationMargin?: string | number;
2017-11-21 20:48:22 +08:00
className?: string;
rootClassName?: string;
2017-11-21 20:48:22 +08:00
children?: React.ReactNode;
dashed?: boolean;
/**
* @since 5.20.0
* @default solid
*/
variant?: 'dashed' | 'dotted' | 'solid';
2017-12-07 23:16:25 +08:00
style?: React.CSSProperties;
plain?: boolean;
2017-11-21 20:48:22 +08:00
}
const Divider: React.FC<DividerProps> = (props) => {
const { getPrefixCls, direction, divider } = React.useContext(ConfigContext);
const {
prefixCls: customizePrefixCls,
type = 'horizontal',
orientation = 'center',
orientationMargin,
className,
rootClassName,
children,
dashed,
variant = 'solid',
plain,
style,
...restProps
} = props;
const prefixCls = getPrefixCls('divider', customizePrefixCls);
feat: Divider support cssVar (#45810) * feat: Divider support cssVar * fix: fix * fix: fix * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * fix: fix * Update components/divider/style/index.ts Signed-off-by: lijianan <574980606@qq.com> * fix: fix --------- Signed-off-by: lijianan <574980606@qq.com> Co-authored-by: MadCcc <madccc@foxmail.com>
2023-11-15 14:11:12 +08:00
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
const hasChildren = !!children;
const hasCustomMarginLeft = orientation === 'left' && orientationMargin != null;
const hasCustomMarginRight = orientation === 'right' && orientationMargin != null;
const classString = classNames(
prefixCls,
divider?.className,
hashId,
cssVarCls,
`${prefixCls}-${type}`,
{
[`${prefixCls}-with-text`]: hasChildren,
[`${prefixCls}-with-text-${orientation}`]: hasChildren,
[`${prefixCls}-dashed`]: !!dashed,
[`${prefixCls}-${variant}`]: variant !== 'solid',
[`${prefixCls}-plain`]: !!plain,
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-no-default-orientation-margin-left`]: hasCustomMarginLeft,
[`${prefixCls}-no-default-orientation-margin-right`]: hasCustomMarginRight,
},
className,
rootClassName,
);
const memoizedOrientationMargin = React.useMemo<string | number>(() => {
if (typeof orientationMargin === 'number') {
return orientationMargin;
}
if (/^\d+$/.test(orientationMargin!)) {
return Number(orientationMargin);
}
return orientationMargin!;
}, [orientationMargin]);
2023-03-18 23:17:41 +08:00
const innerStyle: React.CSSProperties = {
...(hasCustomMarginLeft && { marginLeft: memoizedOrientationMargin }),
...(hasCustomMarginRight && { marginRight: memoizedOrientationMargin }),
};
// Warning children not work in vertical mode
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('Divider');
warning(
!children || type !== 'vertical',
'usage',
'`children` not working in `vertical` mode.',
);
}
feat: Divider support cssVar (#45810) * feat: Divider support cssVar * fix: fix * fix: fix * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/style/index.ts Co-authored-by: MadCcc <madccc@foxmail.com> Signed-off-by: lijianan <574980606@qq.com> * fix: fix * Update components/divider/style/index.ts Signed-off-by: lijianan <574980606@qq.com> * fix: fix --------- Signed-off-by: lijianan <574980606@qq.com> Co-authored-by: MadCcc <madccc@foxmail.com>
2023-11-15 14:11:12 +08:00
return wrapCSSVar(
<div
className={classString}
style={{ ...divider?.style, ...style }}
{...restProps}
role="separator"
>
{children && type !== 'vertical' && (
<span className={`${prefixCls}-inner-text`} style={innerStyle}>
{children}
</span>
)}
</div>,
);
};
if (process.env.NODE_ENV !== 'production') {
Divider.displayName = 'Divider';
}
export default Divider;