Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/@react-spectrum/s2/src/TableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export const Column = forwardRef(function Column(props: ColumnProps, ref: DOMRef
{children}
</ColumnWithMenu>
) : (
<ColumnContents allowsSorting={allowsSorting} sortDirection={sortDirection}>
<ColumnContents align={align} allowsSorting={allowsSorting} sortDirection={sortDirection}>
{children}
</ColumnContents>
)
Expand All @@ -577,7 +577,14 @@ const columnContentWrapper = style({
minWidth: 0,
display: 'flex',
alignItems: 'center',
width: 'full'
width: 'full',
justifyContent: {
align: {
default: 'start',
center: 'center',
end: 'end'
}
}
});

const sortIcon = style({
Expand All @@ -594,13 +601,13 @@ const sortIcon = style({
}
});

interface ColumnContentProps extends Pick<ColumnRenderProps, 'allowsSorting' | 'sortDirection'>, Pick<ColumnProps, 'children'> {}
interface ColumnContentProps extends Pick<ColumnRenderProps, 'allowsSorting' | 'sortDirection'>, Pick<ColumnProps, 'align' | 'children'> {}

function ColumnContents(props: ColumnContentProps) {
let {allowsSorting, sortDirection, children} = props;
let {align, allowsSorting, sortDirection, children} = props;

return (
<div className={columnContentWrapper}>
<div className={columnContentWrapper({align})}>
{allowsSorting && (
<Provider
values={[
Expand All @@ -613,7 +620,7 @@ function ColumnContents(props: ColumnContentProps) {
)}
</Provider>
)}
<span className={style({truncate: true, width: 'full'})}>
<span className={columnHeaderText}>
{children}
</span>
</div>
Expand Down
21 changes: 19 additions & 2 deletions packages/@react-spectrum/s2/stories/TableView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,28 @@ let alignColumns = [
];

const TextAlign = (args: TableViewProps): ReactElement => {
let [items, setItems] = useState(sortItems);
let [sortDescriptor, setSortDescriptor] = useState<SortDescriptor | undefined>(undefined);
let onSortChange = (sortDescriptor: SortDescriptor) => {
let {direction = 'ascending', column = 'name'} = sortDescriptor;

let sorted = items.slice().sort((a, b) => {
let cmp = a[column] < b[column] ? -1 : 1;
if (direction === 'descending') {
cmp *= -1;
}
return cmp;
});

setItems(sorted);
setSortDescriptor(sortDescriptor);
};

return (
<TableView aria-label="Show Dividers table" {...args} styles={style({width: 320, height: 208})}>
<TableView aria-label="Show Dividers table" {...args} sortDescriptor={sortDescriptor} onSortChange={onSortChange} styles={style({width: 320, height: 208})}>
<TableHeader columns={alignColumns}>
{(column) => (
<Column width={150} minWidth={150} isRowHeader={column.isRowHeader} align={column?.align as 'start' | 'center' | 'end'}>{column.name}</Column>
<Column allowsSorting width={150} minWidth={150} isRowHeader={column.isRowHeader} align={column?.align as 'start' | 'center' | 'end'}>{column.name}</Column>
)}
</TableHeader>
<TableBody items={items}>
Expand Down