@neovici/cosmoz-omnitable 9.0.1 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable import/group-exports */
2
2
  import { props } from '@neovici/cosmoz-utils/lib/object';
3
3
 
4
- export const sgProps = ['sortOn', 'descending', 'groupOn', 'groupOn'];
4
+ export const sgProps = ['sortOn', 'descending', 'groupOn', 'groupOnDescending'];
5
5
 
6
6
  const byName = (name) => (item) => item.name === name;
7
7
 
@@ -13,7 +13,7 @@ const storagePrefix = 'omnitable-',
13
13
  }
14
14
  };
15
15
  // eslint-disable-next-line max-lines-per-function
16
- export default (settingsId, settings, setSettings) => {
16
+ export default (settingsId, settings, setSettings, onReset) => {
17
17
  const [counter, setCounter] = useState(0),
18
18
  savedSettings = useMemo(() => read(settingsId), [settingsId, counter]);
19
19
 
@@ -40,18 +40,22 @@ export default (settingsId, settings, setSettings) => {
40
40
  }
41
41
  }, [settings]),
42
42
 
43
- onReset: (e) => {
44
- setSettings();
43
+ onReset: useCallback(
44
+ (e) => {
45
+ setSettings();
45
46
 
46
- if (e.shiftKey) {
47
- try {
48
- localStorage.removeItem(storagePrefix + settingsId);
49
- setCounter((counter) => counter + 1);
50
- } catch (err) {
51
- // ignore error
47
+ if (e.shiftKey) {
48
+ try {
49
+ localStorage.removeItem(storagePrefix + settingsId);
50
+ setCounter((counter) => counter + 1);
51
+ } catch (err) {
52
+ // ignore error
53
+ }
52
54
  }
53
- }
54
- },
55
+ onReset?.();
56
+ },
57
+ [onReset]
58
+ ),
55
59
 
56
60
  hasChanges: settings != null,
57
61
  };
@@ -1,15 +1,24 @@
1
- import { useMemo, useState } from 'haunted';
1
+ import { useMemo, useState, useRef, useCallback } from 'haunted';
2
2
 
3
3
  import useSavedSettings from './use-saved-settings';
4
- import normalize from './normalize';
4
+ import normalize, { sgProps } from './normalize';
5
5
 
6
- export default ({ settingsId, ...thru }) => {
7
- const [settings, setSettings] = useState(),
6
+ export default ({ settingsId, initial: ini, ...thru }) => {
7
+ const initial = useMemo(
8
+ () => Object.fromEntries(sgProps.map((k) => [k, ini[k]])),
9
+ []
10
+ ),
11
+ resetRef = useRef(),
12
+ onReset = useCallback(() => {
13
+ resetRef.current?.(initial);
14
+ }, [initial]),
15
+ [settings, setSettings] = useState(),
8
16
  [opened, setOpened] = useState({ columns: true, sort: true }),
9
17
  { savedSettings, ...rest } = useSavedSettings(
10
18
  settingsId,
11
19
  settings,
12
- setSettings
20
+ setSettings,
21
+ onReset
13
22
  ),
14
23
  { columns } = thru,
15
24
  normalizedSettings = useMemo(
@@ -17,6 +26,7 @@ export default ({ settingsId, ...thru }) => {
17
26
  normalize({
18
27
  settings,
19
28
  savedSettings,
29
+ initial,
20
30
  ...thru,
21
31
  }),
22
32
  [settings, savedSettings, ...Object.values(thru)]
@@ -36,5 +46,6 @@ export default ({ settingsId, ...thru }) => {
36
46
  settings: normalizedSettings,
37
47
  columns: normalizedColumns,
38
48
  setSettings,
49
+ resetRef,
39
50
  };
40
51
  };
@@ -5,15 +5,21 @@ import { useSettings } from './settings';
5
5
  import { useDOMColumns } from './use-dom-columns';
6
6
  import { useSortAndGroupOptions } from './use-sort-and-group-options';
7
7
  import { onItemChange } from './utils-data';
8
- import { useNotifyProperty } from '@neovici/cosmoz-utils/lib/hooks/use-notify-property'
8
+ import { useNotifyProperty } from '@neovici/cosmoz-utils/lib/hooks/use-notify-property';
9
9
 
10
10
  // eslint-disable-next-line max-lines-per-function
11
11
  export const useOmnitable = (host) => {
12
12
  const { enabledColumns, hashParam, settingsId } = host,
13
13
  _columns = useDOMColumns(host, { enabledColumns }),
14
14
  settingS = useSettings({ columns: _columns, settingsId, initial: host }),
15
- { settings, setSettings, columns } = settingS,
16
- sortAndGroupOptions = useSortAndGroupOptions(columns, hashParam, settings, setSettings),
15
+ { settings, setSettings, columns, resetRef } = settingS,
16
+ sortAndGroupOptions = useSortAndGroupOptions(
17
+ columns,
18
+ hashParam,
19
+ settings,
20
+ setSettings,
21
+ resetRef
22
+ ),
17
23
  { groupOnColumn, groupOnDescending, sortOnColumn, descending } =
18
24
  sortAndGroupOptions,
19
25
  { data, resizeSpeedFactor } = host,
@@ -4,6 +4,7 @@ import {
4
4
  component,
5
5
  useContext,
6
6
  useCallback,
7
+ useEffect,
7
8
  } from 'haunted';
8
9
  import { useHashState } from './use-hash-state';
9
10
 
@@ -23,7 +24,8 @@ export const useSortAndGroupOptions = (
23
24
  columns,
24
25
  hashParam,
25
26
  settings,
26
- setSettings
27
+ setSettings,
28
+ resetRef
27
29
  ) => {
28
30
  const [sortOn, setSortOn] = useHashState(settings.sortOn, hashParam, {
29
31
  suffix: '-sortOn',
@@ -73,7 +75,16 @@ export const useSortAndGroupOptions = (
73
75
 
74
76
  columns,
75
77
  },
76
- sortAndGroup = useMemo(() => sortAndGroup_, Object.values(sortAndGroup_));
78
+ sortAndGroup = useMemo(() => sortAndGroup_, Object.values(sortAndGroup_)),
79
+ setSG = useCallback((c) => {
80
+ setSortOn(c.sortOn);
81
+ setGroupOn(c.groupOn);
82
+ setDescending(c.descending);
83
+ setGroupOnDescending(c.groupOnDescending);
84
+ }, []);
85
+
86
+ // eslint-disable-next-line no-void
87
+ useEffect(() => void (resetRef.current = setSG), []);
77
88
 
78
89
  return {
79
90
  ...sortAndGroup,
@@ -84,7 +95,6 @@ export const useSortAndGroupOptions = (
84
95
  },
85
96
  SortAndGroupContext = createContext();
86
97
 
87
-
88
98
  customElements.define('sort-and-group-provider', SortAndGroupContext.Provider);
89
99
  customElements.define(
90
100
  'sort-and-group-consumer',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-omnitable",
3
- "version": "9.0.1",
3
+ "version": "9.1.0",
4
4
  "description": "[![Build Status](https://travis-ci.org/Neovici/cosmoz-omnitable.svg?branch=master)](https://travis-ci.org/Neovici/cosmoz-omnitable)",
5
5
  "keywords": [
6
6
  "web-components"