2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-09-11 17:28:04 +08:00
|
|
|
import classNames from 'classnames';
|
2017-08-19 12:39:11 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-03-10 13:59:49 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2017-11-21 20:48:22 +08:00
|
|
|
export interface DividerProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
type?: 'horizontal' | 'vertical';
|
2019-06-06 00:39:45 +08:00
|
|
|
orientation?: 'left' | 'right' | 'center';
|
2021-09-19 11:28:45 +08:00
|
|
|
orientationMargin?: string | number;
|
2017-11-21 20:48:22 +08:00
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2017-11-21 20:48:22 +08:00
|
|
|
children?: React.ReactNode;
|
|
|
|
dashed?: boolean;
|
2017-12-07 23:16:25 +08:00
|
|
|
style?: React.CSSProperties;
|
2020-04-21 14:16:33 +08:00
|
|
|
plain?: boolean;
|
2017-11-21 20:48:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const Divider: React.FC<DividerProps> = (props) => {
|
2023-06-19 19:30:16 +08:00
|
|
|
const { getPrefixCls, direction, divider } = React.useContext(ConfigContext);
|
2021-09-19 11:28:45 +08:00
|
|
|
|
2022-03-02 10:10:29 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
type = 'horizontal',
|
|
|
|
orientation = 'center',
|
|
|
|
orientationMargin,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-02 10:10:29 +08:00
|
|
|
children,
|
|
|
|
dashed,
|
|
|
|
plain,
|
2023-06-19 19:30:16 +08:00
|
|
|
style,
|
2022-03-02 10:10:29 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const prefixCls = getPrefixCls('divider', customizePrefixCls);
|
2023-11-15 14:11:12 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-03-10 13:59:49 +08:00
|
|
|
|
2022-03-02 10:10:29 +08:00
|
|
|
const hasChildren = !!children;
|
|
|
|
const hasCustomMarginLeft = orientation === 'left' && orientationMargin != null;
|
|
|
|
const hasCustomMarginRight = orientation === 'right' && orientationMargin != null;
|
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
2023-06-19 19:30:16 +08:00
|
|
|
divider?.className,
|
2022-03-10 13:59:49 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2022-03-02 10:10:29 +08:00
|
|
|
`${prefixCls}-${type}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-with-text`]: hasChildren,
|
2024-06-01 16:37:13 +08:00
|
|
|
[`${prefixCls}-with-text-${orientation}`]: hasChildren,
|
2022-03-02 10:10:29 +08:00
|
|
|
[`${prefixCls}-dashed`]: !!dashed,
|
|
|
|
[`${prefixCls}-plain`]: !!plain,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-no-default-orientation-margin-left`]: hasCustomMarginLeft,
|
|
|
|
[`${prefixCls}-no-default-orientation-margin-right`]: hasCustomMarginRight,
|
|
|
|
},
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-02 10:10:29 +08:00
|
|
|
);
|
2021-09-19 11:28:45 +08:00
|
|
|
|
2023-06-03 12:45:26 +08:00
|
|
|
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 = {
|
2023-06-03 12:45:26 +08:00
|
|
|
...(hasCustomMarginLeft && { marginLeft: memoizedOrientationMargin }),
|
|
|
|
...(hasCustomMarginRight && { marginRight: memoizedOrientationMargin }),
|
2022-03-02 10:10:29 +08:00
|
|
|
};
|
|
|
|
|
2022-09-22 16:15:00 +08:00
|
|
|
// Warning children not work in vertical mode
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Divider');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-09-22 16:15:00 +08:00
|
|
|
warning(
|
|
|
|
!children || type !== 'vertical',
|
2023-09-11 17:28:04 +08:00
|
|
|
'usage',
|
2022-09-22 16:15:00 +08:00
|
|
|
'`children` not working in `vertical` mode.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-15 14:11:12 +08:00
|
|
|
return wrapCSSVar(
|
2023-06-19 19:30:16 +08:00
|
|
|
<div
|
|
|
|
className={classString}
|
|
|
|
style={{ ...divider?.style, ...style }}
|
|
|
|
{...restProps}
|
2024-06-22 21:59:12 +08:00
|
|
|
// biome-ignore lint/a11y/useAriaPropsForRole: divider do not need aria-value
|
2023-06-19 19:30:16 +08:00
|
|
|
role="separator"
|
|
|
|
>
|
2022-09-22 16:15:00 +08:00
|
|
|
{children && type !== 'vertical' && (
|
2022-03-02 10:10:29 +08:00
|
|
|
<span className={`${prefixCls}-inner-text`} style={innerStyle}>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
)}
|
2022-03-10 13:59:49 +08:00
|
|
|
</div>,
|
2022-03-02 10:10:29 +08:00
|
|
|
);
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Divider.displayName = 'Divider';
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
export default Divider;
|