Skip to content

Commit 7c7b312

Browse files
authored
fix: table shows wrong width with 3 columns (#29)
1 parent 75c28c7 commit 7c7b312

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

markdown/02_table_example.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@
1717
| A3 | B3 | C3 | D3 | E3 | F3 | G3 | H3 | I3 | J3 |
1818
| A4 | B4 | C4 | D4 | E4 | F4 | G4 | H4 | I4 | J4 |
1919

20-
## Table With Long Text
20+
## Table With Long Text 1
21+
22+
| Task | Priority | Deadline |
23+
|------|----------|----------|
24+
| Complete project documentation | High priority | End of week |
25+
| Review team performance metrics | Medium priority | Next Monday |
26+
| Schedule client meeting | Urgent priority | Tomorrow morning |
27+
| Update software dependencies | Low priority | End of month |
28+
| Prepare quarterly report | High priority | Friday afternoon |
29+
30+
## Table With Long Text 2
2131

2232
| Product Name | Category | Description | Price | Stock | Supplier | Location | Rating | Reviews | Status |
2333
|--------------|----------|-------------|-------|-------|----------|----------|--------|---------|--------|

src/renderers/table.tsx

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,22 @@ const TableContextProvider = ({
5151
columnCount,
5252
children,
5353
}: TableContextProviderProps) => {
54+
const { contentSize } = useMarkdownContext();
5455
const [columnWidths, setColumnWidths] = useState<number[]>(
5556
Array(columnCount).fill(0),
5657
);
5758

5859
const setColumnWidth = useCallback(
5960
(index: number, width: number) => {
61+
console.log("setColumnWidth", index, width);
6062
setColumnWidths((prev) => {
63+
const minWidth = Math.max(contentSize.width / columnCount, 64);
64+
const maxWidth = Math.min(contentSize.width, 180);
6165
const old = prev[index] ?? 0;
62-
const newWidth = Math.min(Math.max(Math.max(old, width), 64), 180);
66+
const newWidth = Math.min(
67+
Math.max(Math.max(old, width), minWidth),
68+
maxWidth,
69+
);
6370
if (newWidth === old) return prev;
6471

6572
const newColumnWidth = [
@@ -70,7 +77,7 @@ const TableContextProvider = ({
7077
return newColumnWidth;
7178
});
7279
},
73-
[setColumnWidths],
80+
[contentSize, columnCount, setColumnWidths],
7481
);
7582

7683
return (
@@ -149,6 +156,11 @@ export const TableCellRenderer = ({
149156
const style = mergeStyles(styles.tableCell, {
150157
fontWeight: rowIndex === 0 ? "bold" : "normal",
151158
});
159+
const measuredStyle = mergeStyles(style, {
160+
width: undefined,
161+
minWidth: undefined,
162+
maxWidth: undefined,
163+
});
152164

153165
const padding = 8;
154166
const onTextLayout = useCallback(
@@ -171,21 +183,31 @@ export const TableCellRenderer = ({
171183
);
172184

173185
return (
174-
<View
175-
style={{
176-
width: width,
177-
minHeight: 32,
178-
padding: padding,
179-
justifyContent: "center",
180-
}}
181-
>
182-
<Text style={style}>{content}</Text>
186+
<>
187+
<View
188+
style={{
189+
width: width,
190+
minHeight: 32,
191+
padding: padding,
192+
justifyContent: "center",
193+
}}
194+
>
195+
<Text style={style}>{content}</Text>
196+
</View>
183197
<Text
184-
style={[style, { position: "absolute", opacity: 0, zIndex: -100 }]}
198+
style={[
199+
measuredStyle,
200+
{
201+
position: "absolute",
202+
opacity: 0,
203+
zIndex: -1000,
204+
},
205+
]}
185206
onLayout={onTextLayout}
207+
numberOfLines={1}
186208
>
187209
{content}
188210
</Text>
189-
</View>
211+
</>
190212
);
191213
};

0 commit comments

Comments
 (0)