-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathflip.tsx
More file actions
73 lines (67 loc) · 2.61 KB
/
Copy pathflip.tsx
File metadata and controls
73 lines (67 loc) · 2.61 KB
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
import React, { useContext } from 'react';
import classNames from 'classnames';
import { FLIP_ITEM_MARK, hasMarker } from '../_utils/component-markers';
import warning from '../_utils/warning';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { FlipItemProps, FlipProps } from './types';
const Flip = (props: FlipProps): React.ReactElement => {
const {
direction = 'horizontal',
reverse = false,
prefixCls: customisedCls,
width,
height,
className,
children,
style,
...otherProps
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('flip', configContext.prefixCls, customisedCls);
const cls = classNames(prefixCls, className);
const innerCls = classNames(`${prefixCls}__inner`, {
[`${prefixCls}__inner_hor`]: direction === 'horizontal' && !reverse,
[`${prefixCls}__inner_hor_reverse`]: direction === 'horizontal' && reverse,
[`${prefixCls}__inner_ver`]: direction === 'vertical' && !reverse,
[`${prefixCls}__inner_ver_reverse`]: direction === 'vertical' && reverse,
});
warning(
!children || React.Children.count(children) !== 2,
'Children must contain front and back components.',
true
);
return (
<div {...otherProps} className={cls} style={{ width, height, ...style }}>
<div className={innerCls}>
{React.Children.map(children, (child, index: number) => {
const childElement = child as React.FunctionComponentElement<FlipItemProps>;
if (hasMarker(childElement.type, FLIP_ITEM_MARK)) {
const childProps: Partial<FlipItemProps> = {
className: classNames(
{
[`${prefixCls}__item-front`]: index === 0,
[`${prefixCls}__item-back`]: index === 1,
[`${prefixCls}__item-back_hor`]:
index === 1 && direction === 'horizontal' && !reverse,
[`${prefixCls}__item-back_hor_reverse`]:
index === 1 && direction === 'horizontal' && reverse,
[`${prefixCls}__item-back_ver`]:
index === 1 && direction === 'vertical' && !reverse,
[`${prefixCls}__item-back_ver_reverse`]:
index === 1 && direction === 'vertical' && reverse,
},
child.props.className
),
};
return React.cloneElement(childElement, childProps);
} else {
return null;
}
})}
</div>
</div>
);
};
Flip.displayName = 'Flip';
export default Flip;