1
- import { inject , defineComponent , HTMLAttributes , CSSProperties } from 'vue' ;
1
+ import { inject , defineComponent , CSSProperties , ExtractPropTypes , computed } from 'vue' ;
2
2
import classNames from '../_util/classNames' ;
3
3
import PropTypes from '../_util/vue-types' ;
4
- import { defaultConfigProvider } from '../config-provider' ;
5
4
import { rowContextState } from './Row' ;
5
+ import useConfigInject from '../_util/hooks/useConfigInject' ;
6
+ import { useInjectRow } from './context' ;
6
7
7
8
type ColSpanType = number | string ;
8
9
@@ -16,22 +17,6 @@ export interface ColSize {
16
17
pull ?: ColSpanType ;
17
18
}
18
19
19
- export interface ColProps extends HTMLAttributes {
20
- span ?: ColSpanType ;
21
- order ?: ColSpanType ;
22
- offset ?: ColSpanType ;
23
- push ?: ColSpanType ;
24
- pull ?: ColSpanType ;
25
- xs ?: ColSpanType | ColSize ;
26
- sm ?: ColSpanType | ColSize ;
27
- md ?: ColSpanType | ColSize ;
28
- lg ?: ColSpanType | ColSize ;
29
- xl ?: ColSpanType | ColSize ;
30
- xxl ?: ColSpanType | ColSize ;
31
- prefixCls ?: string ;
32
- flex ?: FlexType ;
33
- }
34
-
35
20
function parseFlex ( flex : FlexType ) : string {
36
21
if ( typeof flex === 'number' ) {
37
22
return `${ flex } ${ flex } auto` ;
@@ -44,16 +29,43 @@ function parseFlex(flex: FlexType): string {
44
29
return flex ;
45
30
}
46
31
47
- const ACol = defineComponent < ColProps > ( {
32
+ const stringOrNumber = PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ;
33
+ export const colSize = PropTypes . shape ( {
34
+ span : stringOrNumber ,
35
+ order : stringOrNumber ,
36
+ offset : stringOrNumber ,
37
+ push : stringOrNumber ,
38
+ pull : stringOrNumber ,
39
+ } ) . loose ;
40
+ const objectOrNumber = PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number , colSize ] ) ;
41
+
42
+ const colProps = {
43
+ span : stringOrNumber ,
44
+ order : stringOrNumber ,
45
+ offset : stringOrNumber ,
46
+ push : stringOrNumber ,
47
+ pull : stringOrNumber ,
48
+ xs : objectOrNumber ,
49
+ sm : objectOrNumber ,
50
+ md : objectOrNumber ,
51
+ lg : objectOrNumber ,
52
+ xl : objectOrNumber ,
53
+ xxl : objectOrNumber ,
54
+ prefixCls : PropTypes . string ,
55
+ flex : stringOrNumber ,
56
+ } ;
57
+
58
+ export type ColProps = Partial < ExtractPropTypes < typeof colProps > > ;
59
+
60
+ export default defineComponent ( {
48
61
name : 'ACol' ,
62
+ props : colProps ,
49
63
setup ( props , { slots } ) {
50
- const configProvider = inject ( 'configProvider' , defaultConfigProvider ) ;
51
- const rowContext = inject < rowContextState > ( 'rowContext' , { } ) ;
52
-
53
- return ( ) => {
54
- const { gutter } = rowContext ;
55
- const { prefixCls : customizePrefixCls , span, order, offset, push, pull, flex } = props ;
56
- const prefixCls = configProvider . getPrefixCls ( 'col' , customizePrefixCls ) ;
64
+ const { gutter, supportFlexGap, wrap } = useInjectRow ( ) ;
65
+ const { prefixCls, direction } = useConfigInject ( 'col' , props ) ;
66
+ const classes = computed ( ( ) => {
67
+ const { span, order, offset, push, pull } = props ;
68
+ const pre = prefixCls . value ;
57
69
let sizeClassObj = { } ;
58
70
[ 'xs' , 'sm' , 'md' , 'lg' , 'xl' , 'xxl' ] . forEach ( size => {
59
71
let sizeProps : ColSize = { } ;
@@ -66,83 +78,62 @@ const ACol = defineComponent<ColProps>({
66
78
67
79
sizeClassObj = {
68
80
...sizeClassObj ,
69
- [ `${ prefixCls } -${ size } -${ sizeProps . span } ` ] : sizeProps . span !== undefined ,
70
- [ `${ prefixCls } -${ size } -order-${ sizeProps . order } ` ] :
71
- sizeProps . order || sizeProps . order === 0 ,
72
- [ `${ prefixCls } -${ size } -offset-${ sizeProps . offset } ` ] :
73
- sizeProps . offset || sizeProps . offset === 0 ,
74
- [ `${ prefixCls } -${ size } -push-${ sizeProps . push } ` ] : sizeProps . push || sizeProps . push === 0 ,
75
- [ `${ prefixCls } -${ size } -pull-${ sizeProps . pull } ` ] : sizeProps . pull || sizeProps . pull === 0 ,
81
+ [ `${ pre } -${ size } -${ sizeProps . span } ` ] : sizeProps . span !== undefined ,
82
+ [ `${ pre } -${ size } -order-${ sizeProps . order } ` ] : sizeProps . order || sizeProps . order === 0 ,
83
+ [ `${ pre } -${ size } -offset-${ sizeProps . offset } ` ] : sizeProps . offset || sizeProps . offset === 0 ,
84
+ [ `${ pre } -${ size } -push-${ sizeProps . push } ` ] : sizeProps . push || sizeProps . push === 0 ,
85
+ [ `${ pre } -${ size } -pull-${ sizeProps . pull } ` ] : sizeProps . pull || sizeProps . pull === 0 ,
86
+ [ `${ pre } -rtl` ] : direction . value === 'rtl' ,
76
87
} ;
77
88
} ) ;
78
- const classes = classNames (
79
- prefixCls ,
89
+ return classNames (
90
+ pre ,
80
91
{
81
- [ `${ prefixCls } -${ span } ` ] : span !== undefined ,
82
- [ `${ prefixCls } -order-${ order } ` ] : order ,
83
- [ `${ prefixCls } -offset-${ offset } ` ] : offset ,
84
- [ `${ prefixCls } -push-${ push } ` ] : push ,
85
- [ `${ prefixCls } -pull-${ pull } ` ] : pull ,
92
+ [ `${ pre } -${ span } ` ] : span !== undefined ,
93
+ [ `${ pre } -order-${ order } ` ] : order ,
94
+ [ `${ pre } -offset-${ offset } ` ] : offset ,
95
+ [ `${ pre } -push-${ push } ` ] : push ,
96
+ [ `${ pre } -pull-${ pull } ` ] : pull ,
86
97
} ,
87
98
sizeClassObj ,
88
99
) ;
89
- let mergedStyle : CSSProperties = { } ;
90
- if ( gutter ) {
91
- mergedStyle = {
92
- ...( gutter [ 0 ] > 0
93
- ? {
94
- paddingLeft : `${ gutter [ 0 ] / 2 } px` ,
95
- paddingRight : `${ gutter [ 0 ] / 2 } px` ,
96
- }
97
- : { } ) ,
98
- ...( gutter [ 1 ] > 0
99
- ? {
100
- paddingTop : `${ gutter [ 1 ] / 2 } px` ,
101
- paddingBottom : `${ gutter [ 1 ] / 2 } px` ,
102
- }
103
- : { } ) ,
104
- ...mergedStyle ,
105
- } ;
100
+ } ) ;
101
+
102
+ const mergedStyle = computed ( ( ) => {
103
+ const { flex } = props ;
104
+ const gutterVal = gutter . value ;
105
+ let style : CSSProperties = { } ;
106
+ // Horizontal gutter use padding
107
+ if ( gutterVal && gutterVal [ 0 ] > 0 ) {
108
+ const horizontalGutter = `${ gutterVal [ 0 ] / 2 } px` ;
109
+ style . paddingLeft = horizontalGutter ;
110
+ style . paddingRight = horizontalGutter ;
106
111
}
107
- if ( flex ) {
108
- mergedStyle . flex = parseFlex ( flex ) ;
112
+
113
+ // Vertical gutter use padding when gap not support
114
+ if ( gutterVal && gutterVal [ 1 ] > 0 && ! supportFlexGap . value ) {
115
+ const verticalGutter = `${ gutterVal [ 1 ] / 2 } px` ;
116
+ style . paddingTop = verticalGutter ;
117
+ style . paddingBottom = verticalGutter ;
109
118
}
110
119
120
+ if ( flex ) {
121
+ style . flex = parseFlex ( flex ) ;
122
+
123
+ // Hack for Firefox to avoid size issue
124
+ // https://github.com/ant-design/ant-design/pull/20023#issuecomment-564389553
125
+ if ( flex === 'auto' && wrap . value === false && ! style . minWidth ) {
126
+ style . minWidth = 0 ;
127
+ }
128
+ }
129
+ return style ;
130
+ } ) ;
131
+ return ( ) => {
111
132
return (
112
- < div class = { classes } style = { mergedStyle } >
133
+ < div class = { classes . value } style = { mergedStyle . value } >
113
134
{ slots . default ?.( ) }
114
135
</ div >
115
136
) ;
116
137
} ;
117
138
} ,
118
139
} ) ;
119
-
120
- const stringOrNumber = PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ;
121
-
122
- export const ColSize = PropTypes . shape ( {
123
- span : stringOrNumber ,
124
- order : stringOrNumber ,
125
- offset : stringOrNumber ,
126
- push : stringOrNumber ,
127
- pull : stringOrNumber ,
128
- } ) . loose ;
129
-
130
- const objectOrNumber = PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number , ColSize ] ) ;
131
-
132
- ACol . props = {
133
- span : stringOrNumber ,
134
- order : stringOrNumber ,
135
- offset : stringOrNumber ,
136
- push : stringOrNumber ,
137
- pull : stringOrNumber ,
138
- xs : objectOrNumber ,
139
- sm : objectOrNumber ,
140
- md : objectOrNumber ,
141
- lg : objectOrNumber ,
142
- xl : objectOrNumber ,
143
- xxl : objectOrNumber ,
144
- prefixCls : PropTypes . string ,
145
- flex : stringOrNumber ,
146
- } ;
147
-
148
- export default ACol ;
0 commit comments