Skip to content

Commit 228fcf3

Browse files
committed
Use more extensive strict checking for the available alignment options
1 parent 67ae072 commit 228fcf3

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export async function RecordColumnValue<Tag extends React.ElementType = 'div'>(
115115
return <Tag className={tcls(['w-full', verticalAlignment])}>{''}</Tag>;
116116
}
117117

118-
const alignment = getColumnAlignment(definition);
119118

120119
return (
121120
<Blocks
@@ -130,9 +129,7 @@ export async function RecordColumnValue<Tag extends React.ElementType = 'div'>(
130129
'lg:space-y-3',
131130
'leading-normal',
132131
verticalAlignment,
133-
alignment === 'right' ? '[&_*]:text-right' : null,
134-
alignment === 'center' ? '[&_*]:text-center' : null,
135-
alignment === 'left' ? '[&_*]:text-left' : null,
132+
getColumnAlignment(definition)
136133
]}
137134
context={context}
138135
blockStyle={['w-full', 'max-w-[unset]']}
@@ -326,4 +323,4 @@ export async function RecordColumnValue<Tag extends React.ElementType = 'div'>(
326323
default:
327324
assertNever(definition);
328325
}
329-
}
326+
}

packages/gitbook/src/components/DocumentView/Table/ViewGrid.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ export function ViewGrid(props: TableViewProps<DocumentTableViewGrid>) {
4242
)}
4343
>
4444
<div role="row" className={tcls('flex', 'w-full')}>
45-
{view.columns.map((column) => {
46-
const alignment = getColumnAlignment(block.data.definition[column]);
47-
return (
48-
<div
49-
key={column}
50-
role="columnheader"
51-
className={tcls(
45+
{view.columns.map((column) => (
46+
<div
47+
key={column}
48+
role="columnheader"
49+
className={tcls(
5250
styles.columnHeader,
53-
alignment === 'right' ? 'text-right' : null,
54-
alignment === 'center' ? 'text-center' : null
51+
getColumnAlignment(block.data.definition[column])
5552
)}
5653
style={{
5754
width: getColumnWidth({
@@ -63,11 +60,10 @@ export function ViewGrid(props: TableViewProps<DocumentTableViewGrid>) {
6360
minWidth: columnWidths?.[column] || '100px',
6461
}}
6562
title={block.data.definition[column].title}
66-
>
63+
>
6764
{block.data.definition[column].title}
68-
</div>
69-
);
70-
})}
65+
</div>
66+
))}
7167
</div>
7268
</div>
7369
)}

packages/gitbook/src/components/DocumentView/Table/utils.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ContentRef, DocumentTableDefinition, DocumentTableRecord } from '@gitbook/api';
2+
import assertNever from 'assert-never';
23

34
/**
45
* Get the value for a column in a record.
@@ -14,13 +15,30 @@ export function getRecordValue<T extends number | string | boolean | string[] |
1415
/**
1516
* Get the text alignment for a column.
1617
*/
17-
export function getColumnAlignment(column: DocumentTableDefinition): 'left' | 'right' | 'center' {
18+
export function getColumnAlignment(column: DocumentTableDefinition) {
19+
20+
const defaultAlignment = '[&_*]:text-left';
21+
1822
if (column.type === 'text') {
19-
return column.textAlignment ?? 'left';
23+
switch (column.textAlignment) {
24+
case undefined:
25+
case 'left':
26+
return defaultAlignment;
27+
case 'center':
28+
return '[&_*]:text-center';
29+
case 'right':
30+
return '[&_*]:text-right';
31+
default:
32+
assertNever(column.textAlignment);
33+
}
2034
}
21-
return 'left';
35+
36+
return defaultAlignment;
2237
}
2338

39+
40+
41+
2442
/**
2543
* Get the vertical alignment for a column.
2644
*/

0 commit comments

Comments
 (0)