File size: 4,081 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import * as React from 'react';
import classNames from 'classnames';
import { devUseWarning } from '../_util/warning';
import { useComponentConfig } from '../config-provider/context';
import useSize from '../config-provider/hooks/useSize';
import { SizeType } from '../config-provider/SizeContext';
import useStyle from './style';
export interface DividerProps {
prefixCls?: string;
type?: 'horizontal' | 'vertical';
/**
* @default center
*/
orientation?:
| 'left'
| 'right'
| 'center'
| 'start' // ๐ 5.24.0+
| 'end'; // ๐ 5.24.0+
orientationMargin?: string | number;
className?: string;
rootClassName?: string;
children?: React.ReactNode;
dashed?: boolean;
/**
* @since 5.20.0
* @default solid
*/
variant?: 'dashed' | 'dotted' | 'solid';
style?: React.CSSProperties;
size?: SizeType;
plain?: boolean;
}
const sizeClassNameMap: Record<string, string> = { small: 'sm', middle: 'md' };
const Divider: React.FC<DividerProps> = (props) => {
const {
getPrefixCls,
direction,
className: dividerClassName,
style: dividerStyle,
} = useComponentConfig('divider');
const {
prefixCls: customizePrefixCls,
type = 'horizontal',
orientation = 'center',
orientationMargin,
className,
rootClassName,
children,
dashed,
variant = 'solid',
plain,
style,
size: customSize,
...restProps
} = props;
const prefixCls = getPrefixCls('divider', customizePrefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
const sizeFullName = useSize(customSize);
const sizeCls = sizeClassNameMap[sizeFullName];
const hasChildren = !!children;
const mergedOrientation = React.useMemo<'start' | 'end' | 'center'>(() => {
if (orientation === 'left') {
return direction === 'rtl' ? 'end' : 'start';
}
if (orientation === 'right') {
return direction === 'rtl' ? 'start' : 'end';
}
return orientation;
}, [direction, orientation]);
const hasMarginStart = mergedOrientation === 'start' && orientationMargin != null;
const hasMarginEnd = mergedOrientation === 'end' && orientationMargin != null;
const classString = classNames(
prefixCls,
dividerClassName,
hashId,
cssVarCls,
`${prefixCls}-${type}`,
{
[`${prefixCls}-with-text`]: hasChildren,
[`${prefixCls}-with-text-${mergedOrientation}`]: hasChildren,
[`${prefixCls}-dashed`]: !!dashed,
[`${prefixCls}-${variant}`]: variant !== 'solid',
[`${prefixCls}-plain`]: !!plain,
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-no-default-orientation-margin-start`]: hasMarginStart,
[`${prefixCls}-no-default-orientation-margin-end`]: hasMarginEnd,
[`${prefixCls}-${sizeCls}`]: !!sizeCls,
},
className,
rootClassName,
);
const memoizedOrientationMargin = React.useMemo<string | number>(() => {
if (typeof orientationMargin === 'number') {
return orientationMargin;
}
if (/^\d+$/.test(orientationMargin!)) {
return Number(orientationMargin);
}
return orientationMargin!;
}, [orientationMargin]);
const innerStyle: React.CSSProperties = {
marginInlineStart: hasMarginStart ? memoizedOrientationMargin : undefined,
marginInlineEnd: hasMarginEnd ? memoizedOrientationMargin : undefined,
};
// 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.',
);
}
return wrapCSSVar(
<div
className={classString}
style={{ ...dividerStyle, ...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;
|