Skip to content
Merged
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
17 changes: 10 additions & 7 deletions packages/raystack/components/data-table/components/ordering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ import {
export interface OrderingProps {
columnList: ColumnData[];
onChange: (columnId: string, order: SortOrdersValues) => void;
value: DataTableSort;
value?: DataTableSort;
}

export function Ordering({ columnList, onChange, value }: OrderingProps) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a const currentOrder = value?.order ?? SortOrders.ASC value here, then reuse it in this component

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

const currentOrder = value?.order ?? SortOrders.ASC;

function handleColumnChange(columnId: string) {
onChange(columnId, value.order);
onChange(columnId, currentOrder);
}

function handleOrderChange() {
if (!value) return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this and not block this behaviour

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept the guard here intentionally — with defaultSort now optional, value can be undefined when no column is selected. Removing if (!value) return would deref value.name a couple lines down and throw on the toggle click. So the toggle safely no-ops until a column is picked. Applied the currentOrder refactor from the other two comments. 👍

const newOrder =
value.order === SortOrders.ASC ? SortOrders.DESC : SortOrders.ASC;
currentOrder === SortOrders.ASC ? SortOrders.DESC : SortOrders.ASC;
onChange(value.name, newOrder);
}

Expand All @@ -48,7 +51,7 @@ export function Ordering({ columnList, onChange, value }: OrderingProps) {
>
<Select
onValueChange={handleColumnChange}
value={value.name}
value={value?.name}
disabled={columnList.length === 0}
>
<Select.Trigger
Expand All @@ -70,12 +73,12 @@ export function Ordering({ columnList, onChange, value }: OrderingProps) {
size={4}
disabled={columnList.length === 0}
>
{value.order === SortOrders?.ASC ? (
{currentOrder === SortOrders.ASC ? (
<TextAlignTopIcon className={styles['display-popover-sort-icon']} />
) : (
<TextAlignBottomIcon
className={styles['display-popover-sort-icon']}
/>
) : (
<TextAlignTopIcon className={styles['display-popover-sort-icon']} />
)}
</IconButton>
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion packages/raystack/components/data-table/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function DataTableRoot<TData, TValue>({
setTableQuery(prev => ({
...prev,
...defaultTableQuery,
sort: [defaultSort],
sort: defaultSort ? [defaultSort] : [],
group_by: [defaultGroupOption.id]
}));
handleColumnVisibilityChange(initialColumnVisibility);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export type TableContextType<TData, TValue> = {
isLoading?: boolean;
loadMoreData: () => void;
mode: DataTableMode;
defaultSort: DataTableSort;
defaultSort?: DataTableSort;
tableQuery?: InternalQuery;
totalRowCount?: number;
loadingRowCount?: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/raystack/components/data-table/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const isSearchChanged = (oldSearch?: string, newSearch?: string): boolean => {
*/
export const hasActiveQuery = (
tableQuery: InternalQuery,
defaultSort: DataTableSort
defaultSort?: DataTableSort
): boolean => {
const hasFilters =
(tableQuery?.filters && tableQuery.filters.length > 0) || false;
Expand Down Expand Up @@ -345,7 +345,7 @@ export function hasActiveTableFiltering<T>(table: Table<T>): boolean {
}

export function getDefaultTableQuery(
defaultSort: DataTableSort,
defaultSort?: DataTableSort,
oldQuery: DataTableQuery = {}
): InternalQuery {
// Convert DataTableQuery to InternalQuery
Expand Down
Loading