-
Notifications
You must be signed in to change notification settings - Fork 126
/
Group.tsx
103 lines (84 loc) · 2.71 KB
/
Group.tsx
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
import classNames from 'classnames';
import * as React from 'react';
import * as PropTypes from 'prop-types';
import Corners from '../Button/Corners';
import '../ensureOldIEClassName';
import styles from './Group.less';
import { Nullable } from '../../typings/utility-types';
import { isButton } from '../Button';
export interface GroupProps {
width?: React.CSSProperties['width'];
}
export interface GroupChildProps {
/** @deprecated */
mainInGroup?: boolean;
width?: React.CSSProperties['width'];
corners?: number;
}
/**
* Главному *Input*, который должен занимать всю доступную ширину, нужно
* передать свойство `mainInGroup`;
*/
class Group extends React.Component<GroupProps> {
public static propTypes = {
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
};
public render() {
const style: React.CSSProperties = {
width: this.props.width,
};
let first: Nullable<React.ReactElement<any>> = null;
let last: Nullable<React.ReactElement<any>> = null;
React.Children.forEach(this.props.children, child => {
if (child && React.isValidElement(child)) {
first = first || child;
last = child;
}
});
const rootCss = classNames(styles.root, { [styles.hasWidth]: this.props.width !== undefined });
return (
<span className={rootCss} style={style}>
{React.Children.map(this.props.children, child => {
if (!child || !React.isValidElement<GroupChildProps>(child)) {
return null;
}
const childProps = child.props;
const wrapCss = classNames({
[styles.wrap]: true,
[styles.fixed]: !childProps.mainInGroup,
[styles.stretch]: childProps.mainInGroup,
});
const itemCss = classNames({
[styles.item]: true,
[styles.itemFirst]: child === first,
});
let corners = 0;
if (child !== first) {
corners |= Corners.TOP_LEFT | Corners.BOTTOM_LEFT;
}
if (child !== last) {
corners |= Corners.TOP_RIGHT | Corners.BOTTOM_RIGHT;
}
let width = childProps.width;
if (childProps.mainInGroup) {
width = '100%';
}
const cloneProps = {
width,
corners,
};
if (!isButton(child)) {
delete cloneProps.corners;
}
child = React.cloneElement(child, cloneProps);
return (
<div className={wrapCss}>
<div className={itemCss}>{child}</div>
</div>
);
})}
</span>
);
}
}
export default Group;