11/**
22 * `<table>` component examples
33 * ```
4- * <table caption="The table description" align="horizon" :columns="3">
4+ * <table ` caption="The table description" align="horizon" :columns="3">
55 * <link>a link could be a part of item</link>
66 * <canvas></canvas> // A canvas also could
77 * Hello world! // A pure-text node also
@@ -28,17 +28,85 @@ const VerticalTableType = type({
2828 rows : 'number' ,
2929} )
3030
31- const TableType = type . merge ( BaseTableType , HorizonTableType , VerticalTableType ) . partial ( )
31+ const TableAlignType = type . or ( HorizonTableType , VerticalTableType )
32+ const TableType = type . and ( BaseTableType , TableAlignType ) . partial ( )
3233
3334export default defineComponent < 'table' , typeof TableType . infer > ( ( attrs , _context ) => {
3435 return {
3536 name : 'table' ,
3637 attrs : TableType ,
37- setup ( _childrenResolver ) {
38+ setup ( children ) {
39+ const container = document . createElement ( 'div' )
40+ container . style . width = '100%'
41+
3842 const table = document . createElement ( 'table' )
39- table . style . inset = size ( toValue ( attrs . inset ) ?? 'md' )
40- // const children = childrenResolver()
41- return table
43+ table . style . width = '100%'
44+ table . style . tableLayout = 'fixed'
45+ table . style . borderCollapse = 'collapse'
46+
47+ // 应用 inset 参数到表格的内边距
48+ const insetValue = toValue ( attrs . inset )
49+ if ( insetValue !== undefined ) {
50+ table . style . padding = size ( insetValue )
51+ }
52+
53+ const caption = toValue ( attrs . caption )
54+ if ( caption ) {
55+ const captionElement = document . createElement ( 'h3' )
56+ captionElement . textContent = caption
57+ captionElement . style . textAlign = 'center'
58+ captionElement . style . margin = '0 0 1em 0' // 添加底部间距
59+ container . append ( captionElement )
60+ }
61+
62+ const align = toValue ( attrs . align )
63+ const childArray = children ( ) . filter ( child => child . nodeType !== 3 )
64+
65+ if ( align === 'horizon' && 'columns' in attrs ) {
66+ const columns = toValue ( attrs . columns ) ?? 1
67+ const rows = Math . ceil ( childArray . length / columns )
68+
69+ const tbody = document . createElement ( 'tbody' )
70+ Array . from ( { length : rows } ) . forEach ( ( _ , rowIndex ) => {
71+ const tr = document . createElement ( 'tr' )
72+ Array . from ( { length : columns } ) . forEach ( ( _ , colIndex ) => {
73+ const td = document . createElement ( 'td' )
74+ td . style . textAlign = 'center'
75+ td . style . padding = size ( 'sm' )
76+ const childIndex = rowIndex * columns + colIndex
77+ if ( childIndex < childArray . length ) {
78+ td . append ( childArray [ childIndex ] )
79+ }
80+ tr . append ( td )
81+ } )
82+ tbody . append ( tr )
83+ } )
84+ table . append ( tbody )
85+ }
86+ else if ( align === 'vertical' && 'rows' in attrs ) {
87+ const rows = toValue ( attrs . rows ) ?? 1
88+ const columns = Math . ceil ( childArray . length / rows )
89+
90+ const tbody = document . createElement ( 'tbody' )
91+ Array . from ( { length : rows } ) . forEach ( ( _ , rowIndex ) => {
92+ const tr = document . createElement ( 'tr' )
93+ Array . from ( { length : columns } ) . forEach ( ( _ , colIndex ) => {
94+ const td = document . createElement ( 'td' )
95+ td . style . textAlign = 'center'
96+ td . style . padding = size ( 'sm' )
97+ const childIndex = rowIndex * columns + colIndex
98+ if ( childIndex < childArray . length ) {
99+ td . append ( childArray [ childIndex ] )
100+ }
101+ tr . append ( td )
102+ } )
103+ tbody . append ( tr )
104+ } )
105+ table . append ( tbody )
106+ }
107+
108+ container . append ( table )
109+ return container
42110 } ,
43111 }
44112} )
0 commit comments