Repository navigation
Add native support for HTML tables in responses #94926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
88087f6
Add native support for HTML tables in responses
dukenv0307 e4a639d
Merge branch 'main' into fix/94612
dukenv0307 d4c9070
fix spell check
dukenv0307 c38f84d
update table styles
dukenv0307 9d7c10f
remove border bottom of the last table item
dukenv0307 41c8167
merge main
dukenv0307 880f34f
resolve bot comments
dukenv0307 556b67d
use nodeiNdex
dukenv0307 b9cfee5
Merge branch 'main' into fix/94612
dukenv0307 0f944fd
update table style
dukenv0307 ecbceff
Merge branch 'main' into fix/94612
dukenv0307 4e18876
update header styles
dukenv0307 21f04a5
Merge branch 'main' into fix/94612
dukenv0307 b1740eb
make the last column right
dukenv0307 48f2354
Merge branch 'main' into fix/94612
dukenv0307 4c07ed3
truncate long table cell text with ellipsis at 192px max width
dukenv0307 fa0224e
update amount alignment
dukenv0307 4a714f8
Merge branch 'main' into fix/94612
dukenv0307 a07e5d4
update max width
dukenv0307 552b0f3
Merge branch 'main' into fix/94612
dukenv0307 3d6ff7c
fix native scroll
dukenv0307 57ce9e6
Merge branch 'main' into fix/94612
dukenv0307 e2e4ebe
fix scroll issue on native
dukenv0307 e74eb28
Merge branch 'main' into fix/94612
dukenv0307 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/components/HTMLEngineProvider/HTMLRenderers/HTMLTableScroll/index.native.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
|
||
| import {useMemo} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import {Gesture, GestureDetector} from 'react-native-gesture-handler'; | ||
| import Animated, {cancelAnimation, useAnimatedStyle, useSharedValue, withDecay} from 'react-native-reanimated'; | ||
|
|
||
| import type HTMLTableScrollProps from './types'; | ||
|
|
||
| /** | ||
| * Native horizontal scroller for HTML tables. A plain ScrollView rendered inside react-native-render-html never wins the | ||
| * pan on native, and a gesture-handler ScrollView can only ever claim one axis (blocking the chat's vertical scroll). | ||
| * This custom pan scrolls the table horizontally while activeOffsetX/failOffsetY let vertical drags fall through to the | ||
| * chat's vertical scroll. | ||
| */ | ||
| function HTMLTableScroll({viewportWidth, contentWidth, children}: HTMLTableScrollProps) { | ||
| const styles = useThemeStyles(); | ||
| const maxScroll = Math.max(0, contentWidth - viewportWidth); | ||
|
|
||
| const translateX = useSharedValue(0); | ||
| const panGesture = useMemo( | ||
| () => | ||
| Gesture.Pan() | ||
| .activeOffsetX([-10, 10]) | ||
| .failOffsetY([-10, 10]) | ||
| .onBegin(() => { | ||
| cancelAnimation(translateX); | ||
| }) | ||
| .onChange((event) => { | ||
| translateX.set(Math.min(0, Math.max(-maxScroll, translateX.get() + event.changeX))); | ||
| }) | ||
| .onEnd((event) => { | ||
| translateX.set(withDecay({velocity: event.velocityX, clamp: [-maxScroll, 0]})); | ||
| }), | ||
| [maxScroll, translateX], | ||
| ); | ||
|
|
||
| const animatedStyle = useAnimatedStyle(() => ({transform: [{translateX: translateX.get()}]})); | ||
|
|
||
| return ( | ||
| <View style={[styles.htmlTable, {width: viewportWidth}]}> | ||
| <GestureDetector gesture={panGesture}> | ||
| <Animated.View style={[{width: contentWidth}, animatedStyle]}>{children}</Animated.View> | ||
| </GestureDetector> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| export default HTMLTableScroll; |
28 changes: 28 additions & 0 deletions
28
src/components/HTMLEngineProvider/HTMLRenderers/HTMLTableScroll/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import ScrollView from '@components/ScrollView'; | ||
|
|
||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
|
||
| import {View} from 'react-native'; | ||
|
|
||
| import type HTMLTableScrollProps from './types'; | ||
|
|
||
| /** | ||
| * Web/default horizontal scroller for HTML tables. A plain ScrollView scrolls fine here because web is not affected by | ||
| * the native responder issue that requires the custom pan gesture in the native variant. The rows are wrapped in a | ||
| * column View because a horizontal ScrollView's content container lays its children out in a row by default. | ||
| */ | ||
| function HTMLTableScroll({viewportWidth, contentWidth, children}: HTMLTableScrollProps) { | ||
| const styles = useThemeStyles(); | ||
|
|
||
| return ( | ||
| <ScrollView | ||
| horizontal | ||
| showsHorizontalScrollIndicator={false} | ||
| style={{width: viewportWidth}} | ||
| > | ||
| <View style={[styles.htmlTable, {width: contentWidth}]}>{children}</View> | ||
| </ScrollView> | ||
| ); | ||
| } | ||
|
|
||
| export default HTMLTableScroll; |
14 changes: 14 additions & 0 deletions
14
src/components/HTMLEngineProvider/HTMLRenderers/HTMLTableScroll/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type {ReactNode} from 'react'; | ||
|
|
||
| type HTMLTableScrollProps = { | ||
| /** Width of the visible scroll viewport, i.e. the available message width. */ | ||
| viewportWidth: number; | ||
|
|
||
| /** Full width of the table content; at least the viewport width. */ | ||
| contentWidth: number; | ||
|
|
||
| /** The rendered table rows. */ | ||
| children: ReactNode; | ||
| }; | ||
|
|
||
| export default HTMLTableScrollProps; |
66 changes: 66 additions & 0 deletions
66
src/components/HTMLEngineProvider/HTMLRenderers/TableCellRenderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import Text from '@components/Text'; | ||
|
|
||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
|
||
| import type {CustomRendererProps, TBlock} from 'react-native-render-html'; | ||
|
|
||
| import {useContext} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import {TNodeChildrenRenderer} from 'react-native-render-html'; | ||
|
|
||
| import type {CellHorizontalAlignment} from './TableColumnAlignmentContext'; | ||
|
|
||
| import {getElementChildren} from './TableChildrenRenderer'; | ||
| import TableColumnAlignmentContext from './TableColumnAlignmentContext'; | ||
|
|
||
| /** Reads an explicit `text-align` from the cell's inline style, if the HTML happens to carry one. */ | ||
| function getExplicitCellAlignment(styleAttribute: string | undefined): CellHorizontalAlignment | undefined { | ||
| if (styleAttribute?.includes('text-align: right')) { | ||
| return 'right'; | ||
| } | ||
| if (styleAttribute?.includes('text-align: center')) { | ||
| return 'center'; | ||
| } | ||
| if (styleAttribute?.includes('text-align: left')) { | ||
| return 'left'; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function TableCellRenderer({tnode}: CustomRendererProps<TBlock>) { | ||
| const styles = useThemeStyles(); | ||
| const columnAlignments = useContext(TableColumnAlignmentContext); | ||
| const isHeaderCell = tnode.tagName === 'th'; | ||
|
|
||
| // An explicit style wins; otherwise fall back to the column alignment derived by the table renderer. | ||
| // Guard the index so an unexpected -1 (cell without a parent row) does not wrap around to the last column. | ||
| const columnIndex = getElementChildren(tnode.parent).indexOf(tnode); | ||
| const columnAlignment = columnIndex >= 0 ? columnAlignments.at(columnIndex) : undefined; | ||
| const alignment = getExplicitCellAlignment(tnode.attributes.style) ?? columnAlignment ?? 'left'; | ||
| const isRightAligned = alignment === 'right'; | ||
|
|
||
| // Map the cell alignment to the shared named styles instead of building inline style objects. | ||
| const cellAlignmentStyle = { | ||
| left: styles.alignItemsStart, | ||
| center: styles.alignItemsCenter, | ||
| right: styles.alignItemsEnd, | ||
| }[alignment]; | ||
| const textAlignmentStyle = { | ||
| left: styles.textAlignLeft, | ||
| center: styles.textAlignCenter, | ||
| right: styles.textAlignRight, | ||
| }[alignment]; | ||
|
|
||
| return ( | ||
| <View style={[styles.htmlTableCell, !isRightAligned && styles.htmlTableCellMaxWidth, cellAlignmentStyle]}> | ||
| <Text | ||
| numberOfLines={1} | ||
| style={[isHeaderCell ? styles.htmlTableHeaderCellText : styles.htmlTableCellText, styles.w100, textAlignmentStyle]} | ||
| > | ||
| <TNodeChildrenRenderer tnode={tnode} /> | ||
| </Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| export default TableCellRenderer; | ||
31 changes: 31 additions & 0 deletions
31
src/components/HTMLEngineProvider/HTMLRenderers/TableChildrenRenderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type {TNode} from 'react-native-render-html'; | ||
|
|
||
| import {TNodeRenderer} from 'react-native-render-html'; | ||
|
|
||
| /** Element (non-whitespace) children of a node, skipping whitespace-only text nodes that sit between block elements in the source HTML. */ | ||
| function getElementChildren(node: TNode | null | undefined): TNode[] { | ||
| return node?.children?.filter((child) => !!child.tagName) ?? []; | ||
| } | ||
|
|
||
| /** | ||
| * Renders the element children of a table-related node through `TNodeRenderer`, skipping whitespace-only text nodes. | ||
| * Shared by the table, section and row renderers so the filter-and-render logic lives in a single place. | ||
| */ | ||
| function TableChildrenRenderer({tnode}: {tnode: TNode}) { | ||
| const children = getElementChildren(tnode); | ||
| return ( | ||
| <> | ||
| {children.map((child, index) => ( | ||
| <TNodeRenderer | ||
| key={`${child.tagName ?? 'node'}-${child.nodeIndex}`} | ||
| tnode={child} | ||
| renderIndex={index} | ||
| renderLength={children.length} | ||
| /> | ||
| ))} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default TableChildrenRenderer; | ||
| export {getElementChildren}; |
13 changes: 13 additions & 0 deletions
13
src/components/HTMLEngineProvider/HTMLRenderers/TableColumnAlignmentContext.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import {createContext} from 'react'; | ||
|
|
||
| type CellHorizontalAlignment = 'left' | 'center' | 'right'; | ||
|
|
||
| /** | ||
| * Per-column horizontal alignment for the table currently being rendered, indexed by column position. | ||
| * The Concierge HTML never carries alignment attributes, so `TableRenderer` derives it from the cell | ||
| * contents and shares it here so both the header cell and the body cells of a column stay in sync. | ||
| */ | ||
| const TableColumnAlignmentContext = createContext<CellHorizontalAlignment[]>([]); | ||
|
|
||
| export default TableColumnAlignmentContext; | ||
| export type {CellHorizontalAlignment}; |
10 changes: 10 additions & 0 deletions
10
src/components/HTMLEngineProvider/HTMLRenderers/TableContentWidthContext.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import {createContext} from 'react'; | ||
|
|
||
| /** | ||
| * The width available to the HTML content, measured at the comment level (above react-native-render-html's per-block | ||
| * wrappers). `TableRenderer` uses it to fill the message width, because the block wrappers shrink-wrap to their content | ||
| * and a width measured inside them collapses to the table's own content width instead of the available space. | ||
| */ | ||
| const TableContentWidthContext = createContext<number>(0); | ||
|
|
||
| export default TableContentWidthContext; |
55 changes: 55 additions & 0 deletions
55
src/components/HTMLEngineProvider/HTMLRenderers/TableRenderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import variables from '@styles/variables'; | ||
|
|
||
| import type {CustomRendererProps, TBlock, TNode} from 'react-native-render-html'; | ||
|
|
||
| import {useContext, useMemo} from 'react'; | ||
| import {useContentWidth} from 'react-native-render-html'; | ||
|
|
||
| import type {CellHorizontalAlignment} from './TableColumnAlignmentContext'; | ||
|
|
||
| import HTMLTableScroll from './HTMLTableScroll'; | ||
| import TableChildrenRenderer, {getElementChildren} from './TableChildrenRenderer'; | ||
| import TableColumnAlignmentContext from './TableColumnAlignmentContext'; | ||
| import TableContentWidthContext from './TableContentWidthContext'; | ||
|
|
||
| /** | ||
| * Derives the horizontal alignment for each column from the table body. Concierge expense tables put the amount in | ||
| * the last column and carry no alignment attributes, so the last column is right-aligned to match how amounts are | ||
| * shown elsewhere in the app; every other column stays left-aligned. | ||
| */ | ||
| function getColumnAlignments(tableNode: TNode): CellHorizontalAlignment[] { | ||
| const bodyRows = getElementChildren(tableNode) | ||
| .filter((section) => section.tagName === 'tbody') | ||
| .flatMap((section) => getElementChildren(section)); | ||
| if (bodyRows.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| const columnCount = bodyRows.reduce((max, row) => Math.max(max, getElementChildren(row).length), 0); | ||
| return Array.from({length: columnCount}, (_, columnIndex) => (columnIndex === columnCount - 1 ? 'right' : 'left')); | ||
| } | ||
|
|
||
| function TableRenderer({tnode}: CustomRendererProps<TBlock>) { | ||
| const columnAlignments = useMemo(() => getColumnAlignments(tnode), [tnode]); | ||
|
|
||
| // The comment-level width fills the message exactly; fall back to the HTML content width when the table is rendered | ||
| // outside a comment. A concrete number is required because the scroller needs a fixed viewport and content width. | ||
| const measuredContentWidth = useContext(TableContentWidthContext); | ||
| const fallbackContentWidth = useContentWidth(); | ||
| const viewportWidth = measuredContentWidth || fallbackContentWidth; | ||
| const columnsWidth = columnAlignments.length * variables.htmlTableColumnMaxWidth + 2 * variables.tableRowPaddingHorizontal; | ||
| const contentWidth = Math.max(viewportWidth, columnsWidth); | ||
|
|
||
| return ( | ||
| <TableColumnAlignmentContext.Provider value={columnAlignments}> | ||
| <HTMLTableScroll | ||
| viewportWidth={viewportWidth} | ||
| contentWidth={contentWidth} | ||
| > | ||
| <TableChildrenRenderer tnode={tnode} /> | ||
| </HTMLTableScroll> | ||
| </TableColumnAlignmentContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export default TableRenderer; |
33 changes: 33 additions & 0 deletions
33
src/components/HTMLEngineProvider/HTMLRenderers/TableRowRenderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
|
||
| import type {CustomRendererProps, TBlock, TNode} from 'react-native-render-html'; | ||
|
|
||
| import {View} from 'react-native'; | ||
|
|
||
| import TableChildrenRenderer, {getElementChildren} from './TableChildrenRenderer'; | ||
|
|
||
| function isLastRowOfTable(tnode: TNode): boolean { | ||
| const section = tnode.parent; | ||
| const table = section?.parent; | ||
| if (!section || !table) { | ||
| return false; | ||
| } | ||
| const sectionRows = getElementChildren(section); | ||
| const tableSections = getElementChildren(table); | ||
| return sectionRows.at(-1) === tnode && tableSections.at(-1) === section; | ||
| } | ||
|
|
||
| function TableRowRenderer({tnode}: CustomRendererProps<TBlock>) { | ||
| const styles = useThemeStyles(); | ||
|
|
||
| // Header rows (inside <thead>) use header padding; body rows use the compact min-height. | ||
| const isHeaderRow = tnode.parent?.tagName === 'thead'; | ||
|
|
||
| return ( | ||
| <View style={[isHeaderRow ? styles.htmlTableHeaderRow : styles.htmlTableRow, isLastRowOfTable(tnode) && styles.htmlTableLastRow]}> | ||
| <TableChildrenRenderer tnode={tnode} /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| export default TableRowRenderer; |
13 changes: 13 additions & 0 deletions
13
src/components/HTMLEngineProvider/HTMLRenderers/TableSectionRenderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type {CustomRendererProps, TBlock} from 'react-native-render-html'; | ||
|
|
||
| import TableChildrenRenderer from './TableChildrenRenderer'; | ||
|
|
||
| /** | ||
| * Renders an HTML <thead>/<tbody> by rendering its <tr> children directly into the table | ||
| * container, without adding an extra wrapping view, so rows stack with no gaps. | ||
| */ | ||
| function TableSectionRenderer({tnode}: CustomRendererProps<TBlock>) { | ||
| return <TableChildrenRenderer tnode={tnode} />; | ||
| } | ||
|
|
||
| export default TableSectionRenderer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.