Skip to content

Commit bcda348

Browse files
committed
feat: define default sort property on table
1 parent f74e236 commit bcda348

File tree

1 file changed

+31
-20
lines changed
  • packages/components-vue/src/components/table

1 file changed

+31
-20
lines changed

packages/components-vue/src/components/table/Simple.vue

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
:size="size"
5959
@update:model-value="toggleAll"
6060
/>
61-
<span v-if="isReadOnly || !canSort">#</span>
61+
<span v-if="!canSort">#</span>
6262
<ActionLink
6363
v-else
6464
:theme="theme || themeValues"
@@ -71,44 +71,47 @@
7171
>
7272
<span>#</span>
7373
<template v-if="!!ordering['id']">
74-
<IconFa v-if="ordering.asc" name="arrow-down" />
75-
<IconFa v-if="!ordering.asc" name="arrow-up" />
74+
<IconFa v-if="ordering['id'] === 'asc'" name="arrow-down" />
75+
<IconFa v-else name="arrow-up" />
7676
</template>
7777
</ActionLink>
7878
</div>
7979
</th>
8080
<td
81-
v-for="(propertyName, propertyNameIndex) in propertiesMeta"
82-
:key="propertyNameIndex"
81+
v-for="(meta, metaIndex) in propertiesMeta"
82+
:key="metaIndex"
8383
class="--maxWidth-440"
8484
:class="[
8585
`--txtSize-${size}`,
86-
{ ['is--selected']: canSort && !!ordering[propertyName.value] },
86+
{ ['is--selected']: meta.canSort && !!ordering[meta.value] },
8787
]"
88-
:data-column-name="propertyName.value"
89-
:data-column="propertyName.alias"
88+
:data-column-name="meta.value"
89+
:data-column="meta.alias"
9090
:width="
91-
extraCols && propertyNameIndex === propertiesMeta.length - 1
91+
extraCols && metaIndex === propertiesMeta.length - 1
9292
? '99%'
9393
: 'auto'
9494
"
9595
>
96-
<span v-if="!canSort" :title="propertyName.value">
97-
{{ propertyName.alias }}
96+
<span v-if="!meta.canSort" :title="meta.value">
97+
{{ meta.alias }}
9898
</span>
9999
<ActionLink
100100
v-else
101101
:theme="theme || themeValues"
102-
:title="propertyName.value"
103-
:tooltip="t('table_sort_by_name', { name: propertyName.alias })"
102+
:title="meta.value"
103+
:tooltip="t('table_sort_by_name', { name: meta.alias })"
104104
tooltip-as-text
105105
tooltip-position="bottom"
106106
:size="size"
107-
@click="setOrdering(propertyName.value)"
107+
@click="setOrdering(meta.value)"
108108
>
109-
<span>{{ propertyName.alias }}</span>
110-
<template v-if="!!ordering[propertyName.value]">
111-
<IconFa v-if="ordering.asc" name="arrow-down" />
109+
<span>{{ meta.alias }}</span>
110+
<template v-if="!!ordering[meta.value]">
111+
<IconFa
112+
v-if="ordering[meta.value] === 'asc'"
113+
name="arrow-down"
114+
/>
112115
<IconFa v-else name="arrow-up" />
113116
</template>
114117
</ActionLink>
@@ -344,6 +347,7 @@
344347
iProperty,
345348
iSelectOption,
346349
tOrder,
350+
tOrderBy,
347351
tProps,
348352
tSizeModifier,
349353
} from "@open-xamu-co/ui-common-types";
@@ -388,7 +392,7 @@
388392
/**
389393
* Do nodes support pagination?
390394
*/
391-
canSort?: boolean;
395+
canSort?: boolean | tOrderBy;
392396
/**
393397
* Function used to update a node
394398
*/
@@ -467,7 +471,11 @@
467471
* TODO: require & use order getter fn instead
468472
*/
469473
const ordering = computed(() => {
470-
let orderBy: Record<string, tOrder> = { id: "desc" };
474+
let [sortKey = "id", sortValue = "desc"] = Array.isArray(props.canSort)
475+
? props.canSort
476+
: [];
477+
478+
let orderBy: Record<string, tOrder> = { [sortKey]: sortValue };
471479
472480
if (router) {
473481
const route = router.currentRoute.value;
@@ -495,6 +503,7 @@
495503
496504
interface iPropertyMeta extends iSelectOption {
497505
value: string;
506+
canSort: boolean;
498507
}
499508
500509
/**
@@ -513,15 +522,17 @@
513522
514523
return 0;
515524
})
516-
.map(([key]): iPropertyMeta => {
525+
.map(([key, value]): iPropertyMeta => {
517526
const options = (props.properties || []).map(toOption);
518527
const property = toOption(options.find((p) => p.value === key) || key);
519528
const aliasKey = _.snakeCase(key);
529+
const canSort = typeof value === "string" || typeof value === "number";
520530
521531
return {
522532
...property,
523533
value: String(property.value),
524534
alias: _.capitalize(_.startCase(property.alias || tet(aliasKey))),
535+
canSort: !!props.canSort && canSort,
525536
};
526537
})
527538
.filter((property) => property.value !== "id");

0 commit comments

Comments
 (0)