-
Notifications
You must be signed in to change notification settings - Fork 329
/
useStyle.ts
70 lines (63 loc) · 1.9 KB
/
useStyle.ts
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
import { useMemo } from 'react';
import { BaseTableProps, PrimaryTableProps } from '../interface';
import { ClassName, Styles } from '../../common';
import useClassName from './useClassName';
import useCommonClassName from '../../hooks/useCommonClassName';
export function formatCSSUnit(unit: string | number) {
if (!unit) return unit;
return isNaN(Number(unit)) ? unit : `${unit}px`;
}
export default function useStyle(props: BaseTableProps | PrimaryTableProps) {
const { className, size, bordered, stripe, hover, verticalAlign, height, maxHeight, tableContentWidth } = props;
const { tableBaseClass, tableAlignClasses } = useClassName();
const { sizeClassNames } = useCommonClassName();
const tableClasses = useMemo<ClassName>(
() => [
tableBaseClass.table,
size !== 'medium' && sizeClassNames[size],
verticalAlign !== 'middle' && tableAlignClasses[verticalAlign],
{
[tableBaseClass.bordered]: bordered,
[tableBaseClass.striped]: stripe,
[tableBaseClass.hover]: hover,
[tableBaseClass.loading]: props.loading,
[tableBaseClass.affixedHeader]: props.headerAffixedTop,
[tableBaseClass.rowspanAndColspan]: props.rowspanAndColspan,
},
className,
],
[
className,
bordered,
hover,
props.loading,
props.headerAffixedTop,
props.rowspanAndColspan,
size,
sizeClassNames,
stripe,
tableAlignClasses,
tableBaseClass,
verticalAlign,
],
);
const tableContentStyles = useMemo<Styles>(
() => ({
height: formatCSSUnit(height),
maxHeight: formatCSSUnit(maxHeight),
}),
[height, maxHeight],
);
const tableElementStyles = useMemo<Styles>(
() => ({
width: formatCSSUnit(tableContentWidth),
}),
[tableContentWidth],
);
return {
tableClasses,
sizeClassNames,
tableElementStyles,
tableContentStyles,
};
}