Skip to content

Commit 5f0274b

Browse files
authored
Merge pull request #3 from confuseder/main
feat: add `<table>`
2 parents 3546147 + d935914 commit 5f0274b

File tree

6 files changed

+106
-12
lines changed

6 files changed

+106
-12
lines changed

packages/test/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
{
22
"name": "@sciux/test",
3-
"private": true,
43
"type": "module",
4+
"private": true,
55
"scripts": {
66
"dev": "vite",
77
"build": "tsc && vite build",
88
"preview": "vite preview"
99
},
10-
"devDependencies": {
11-
"typescript": "~5.8.3",
12-
"vite": "^6.3.5"
13-
},
1410
"dependencies": {
1511
"@sciux/layout": "workspace:^",
1612
"@sciux/model": "workspace:^",
13+
"@sciux/widget": "workspace:^",
1714
"sciux-laplace": "catalog:"
15+
},
16+
"devDependencies": {
17+
"typescript": "~5.8.3",
18+
"vite": "^6.3.5"
1819
}
1920
}

packages/test/src/example.sciux

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,23 @@
1313
</flexbox>
1414
</rows>
1515
</columns>
16+
17+
<!-- 测试水平布局表格 -->
18+
<table caption="水平布局测试" align="horizon" columns="3">
19+
<block>1</block>
20+
<block>2</block>
21+
<block>3</block>
22+
<block>4</block>
23+
<block>5</block>
24+
<block>6</block>
25+
</table>
26+
27+
<!-- 测试垂直布局表格 -->
28+
<table caption="垂直布局测试" align="vertical" rows="3">
29+
<block>A1</block>
30+
<block>A2</block>
31+
<block>A3</block>
32+
<block>B1</block>
33+
<block>B2</block>
34+
<block>B3</block>
35+
</table>

packages/test/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { align, block, columns, flexbox, grid, rows } from '@sciux/layout'
2-
// import { model } from "@sciux/model";
2+
import { table } from '@sciux/widget'
33
import { button, checkbox, input, slider } from '@sciux/model'
44
import { components, render } from 'sciux-laplace'
55
import source from './example.sciux?raw'
@@ -12,6 +12,7 @@ components.set('grid', grid)
1212
components.set('align', align)
1313
components.set('button', button)
1414
components.set('input', input)
15+
components.set('table', table)
1516
components.set('checkbox', checkbox)
1617
components.set('slider', slider)
1718

packages/widget/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as table } from './table'

packages/widget/src/table.ts

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

3334
export 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
})

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)