Skip to content

Commit ab0c121

Browse files
committed
feat(ui-table): add column ordering to caption
INSTUI-4699
1 parent c827833 commit ab0c121

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

packages/ui-table/src/Table/index.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,43 @@ class Table extends Component<TableProps> {
101101
})
102102
}
103103

104+
getSortedHeaderInfo() {
105+
const [headChild] = Children.toArray(this.props.children)
106+
if (!headChild || !isValidElement(headChild)) return null
107+
const [firstRow] = Children.toArray(headChild.props.children)
108+
if (!firstRow || !isValidElement(firstRow)) return null
109+
const colHeaders = Children.toArray(firstRow.props.children)
110+
for (const colHeader of colHeaders) {
111+
if (
112+
isValidElement(colHeader) &&
113+
colHeader.props.sortDirection &&
114+
colHeader.props.sortDirection !== 'none'
115+
) {
116+
const headerText =
117+
typeof colHeader.props.children === 'string'
118+
? colHeader.props.children
119+
: colHeader.props.children?.props?.children ?? ''
120+
return {
121+
header: headerText,
122+
direction: colHeader.props.sortDirection
123+
}
124+
}
125+
}
126+
return null
127+
}
128+
104129
render() {
105130
const { margin, layout, caption, children, hover, styles } = this.props
106131
const isStacked = layout === 'stacked'
107132
const headers = isStacked ? this.getHeaders() : undefined
108133

134+
// Add sorting info to caption for screen readers
135+
const sortedHeaderInfo = this.getSortedHeaderInfo()
136+
let captionText = caption as string
137+
if (sortedHeaderInfo) {
138+
captionText += `Sorted by ${sortedHeaderInfo.header} (${sortedHeaderInfo.direction})`
139+
}
140+
109141
return (
110142
<TableContext.Provider
111143
value={{
@@ -124,11 +156,11 @@ class Table extends Component<TableProps> {
124156
elementRef={this.handleRef}
125157
css={styles?.table}
126158
role={isStacked ? 'table' : undefined}
127-
aria-label={isStacked ? (caption as string) : undefined}
159+
aria-label={isStacked ? captionText : undefined}
128160
>
129161
{!isStacked && (
130162
<caption>
131-
<ScreenReaderContent>{caption}</ScreenReaderContent>
163+
<ScreenReaderContent>{captionText}</ScreenReaderContent>
132164
</caption>
133165
)}
134166
{Children.map(children, (child) => {

0 commit comments

Comments
 (0)