Skip to content

Commit

Permalink
added docstring comments
Browse files Browse the repository at this point in the history
  • Loading branch information
BabyElias committed Aug 21, 2024
1 parent b0b8a93 commit 4e1c3b9
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/composables/useSorting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@ export const DATA_TYPE_NUMERIC = 'numeric';
export const DATA_TYPE_DATE = 'date';
export const DATA_TYPE_OTHERS = 'others';

/**
* Custom hook for handling sorting logic in a table.
*
* @param {Ref<Array>} headers - Reactive reference to the table headers.
* @param {Ref<Array>} rows - Reactive reference to the table rows.
* @param {Ref<Boolean>} useLocalSorting - Reactive reference to a boolean indicating if local sorting should be used.
* @returns {Object} - An object containing reactive references and methods for sorting.
*/
export default function useSorting(headers, rows, useLocalSorting) {
const sortKey = ref(null);
const sortOrder = ref(null);

/**
* Computed property that returns the sorted rows based on the current sort key and order.
* If local sorting is disabled or no sort key is set, it returns the original rows.
*/
const sortedRows = computed(() => {
if (!useLocalSorting.value || sortKey.value === null) return rows.value;

return _.orderBy(rows.value, [row => row[sortKey.value]], [sortOrder.value]);
});

/**
* Method to handle sorting when a column header is clicked.
*
* @param {Number} index - The index of the column to sort by.
*/
const handleSort = index => {
if (headers.value[index].dataType === DATA_TYPE_OTHERS) return;

Expand All @@ -28,7 +43,12 @@ export default function useSorting(headers, rows, useLocalSorting) {
sortOrder.value = SORT_ORDER_ASC;
}
};

/**
* Method to get the ARIA sort attribute value for a column header.
*
* @param {Number} index - The index of the column.
* @returns {String} - The ARIA sort attribute value ('ascending', 'descending', or 'none').
*/
const getAriaSort = index => {
if (sortKey.value === index) {
return sortOrder.value === SORT_ORDER_ASC ? 'ascending' : 'descending';
Expand Down

0 comments on commit 4e1c3b9

Please sign in to comment.