UNPKG

svelte-ux

Version:

- Increment version in `package.json` and commit as `Version bump to x.y.z` - `npm run publish`

29 lines (28 loc) 1.06 kB
import { writable } from 'svelte/store'; import { isFunction } from 'lodash-es'; export default function tableOrderStore(props) { var _a, _b; const state = writable({ by: (_a = props === null || props === void 0 ? void 0 : props.initialBy) !== null && _a !== void 0 ? _a : '', direction: (_b = props === null || props === void 0 ? void 0 : props.initialDirection) !== null && _b !== void 0 ? _b : 'asc', handler: null, }); function onHeaderClick(column) { state.update((prevState) => { const by = typeof column.orderBy === 'string' ? column.orderBy : typeof column.value === 'string' ? column.value : column.name; return { by, direction: prevState.by === by && prevState.direction === 'asc' ? 'desc' : 'asc', handler: isFunction(column.orderBy) ? column.orderBy : null, }; }); } return { ...state, onHeaderClick, }; }