@stack-spot/citric-react 0.15.4 → 0.16.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.
package/dist/index.d.ts CHANGED
@@ -49,5 +49,7 @@ export * from './components/Textarea.js';
49
49
  export * from './components/Tooltip.js';
50
50
  export * from './context/CitricProvider.js';
51
51
  export type { CitricController } from './types.js';
52
+ export * from './utils/checkbox.js';
53
+ export * from './utils/radio.js';
52
54
  export * from './utils/ValueController.js';
53
55
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA;AACtC,cAAc,oBAAoB,CAAA;AAClC,cAAc,2BAA2B,CAAA;AACzC,cAAc,qBAAqB,CAAA;AACnC,cAAc,0BAA0B,CAAA;AACxC,cAAc,oBAAoB,CAAA;AAClC,cAAc,yBAAyB,CAAA;AACvC,cAAc,yBAAyB,CAAA;AACvC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA;AACrC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,qBAAqB,CAAA;AACnC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,sBAAsB,CAAA;AACpC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,uBAAuB,CAAA;AACrC,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA;AACjC,cAAc,wBAAwB,CAAA;AACtC,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,0BAA0B,CAAA;AACxC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,sBAAsB,CAAA;AACpC,cAAc,4BAA4B,CAAA;AAC1C,cAAc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,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,0BAA0B,CAAA;AACxC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,sBAAsB,CAAA;AACpC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,yBAAyB,CAAA;AACvC,cAAc,0BAA0B,CAAA;AACxC,cAAc,+BAA+B,CAAA;AAC7C,cAAc,yBAAyB,CAAA;AACvC,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AACnC,cAAc,wBAAwB,CAAA;AACtC,cAAc,uBAAuB,CAAA;AACrC,cAAc,qBAAqB,CAAA;AACnC,cAAc,yBAAyB,CAAA;AACvC,cAAc,sBAAsB,CAAA;AACpC,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iCAAiC,CAAA;AAC/C,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA;AACrC,cAAc,sBAAsB,CAAA;AACpC,cAAc,0BAA0B,CAAA;AACxC,YAAY,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAC/C,cAAc,kBAAkB,CAAA;AAChC,cAAc,eAAe,CAAA;AAC7B,cAAc,yBAAyB,CAAA"}
package/dist/index.js CHANGED
@@ -48,5 +48,7 @@ export * from './components/Text.js';
48
48
  export * from './components/Textarea.js';
49
49
  export * from './components/Tooltip.js';
50
50
  export * from './context/CitricProvider.js';
51
+ export * from './utils/checkbox.js';
52
+ export * from './utils/radio.js';
51
53
  export * from './utils/ValueController.js';
52
54
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,C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AAA;AACvC,cAAc,sBAAsB,CAAA;AACpC,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iCAAiC,CAAA;AAC/C,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA;AACrC,cAAc,sBAAsB,CAAA;AACpC,cAAc,0BAA0B,CAAA;AAExC,cAAc,kBAAkB,CAAA;AAChC,cAAc,eAAe,CAAA;AAC7B,cAAc,yBAAyB,CAAA"}
@@ -0,0 +1,75 @@
1
+ export interface CheckboxGroupHookParams<T, F = string> {
2
+ /**
3
+ * The initial value for the checkbox group.
4
+ *
5
+ * @default []
6
+ */
7
+ initialValue?: T[];
8
+ /**
9
+ * A function to apply a filter to an option. Must return true if the option respects the filter and false otherwise.
10
+ * @param filter the current filter.
11
+ * @param option the current option.
12
+ * @returns true if the option should pass the filter, false if the option should be discarded.
13
+ */
14
+ applyFilter?: (filter: F, option: T) => boolean;
15
+ /**
16
+ * The full set of options for the checkbox group.
17
+ */
18
+ options: T[];
19
+ /**
20
+ * A function that produces a unique id for an option.
21
+ * @param option the current option.
22
+ * @returns a unique key.
23
+ */
24
+ renderKey: (option: T) => string | number | undefined;
25
+ }
26
+ /**
27
+ * Use this hook to easily implement filtering and selection controls for a checkbox group.
28
+ * @param params the parameters to create the controls.
29
+ * @returns the checkbox controls.
30
+ */
31
+ export declare function useCheckboxGroupControls<T, F = string>(params: CheckboxGroupHookParams<T, F>): {
32
+ /**
33
+ * Selects all the options currently visible.
34
+ */
35
+ selectAll: () => void;
36
+ /**
37
+ * Removes all options from the selection.
38
+ */
39
+ removeSelection: () => void;
40
+ /**
41
+ * The current filter applied.
42
+ */
43
+ filter: F | undefined;
44
+ /**
45
+ * Apply a new filter.
46
+ */
47
+ setFilter: import("react").Dispatch<import("react").SetStateAction<F | undefined>>;
48
+ /**
49
+ * The options that should be passed to the checkbox group.
50
+ */
51
+ options: T[];
52
+ /**
53
+ * The value that should be passed to the checkbox group.
54
+ */
55
+ value: T[];
56
+ /**
57
+ * Changes the current value, should be passed to the property `onChange` of the checkbox group.
58
+ */
59
+ setValue: import("react").Dispatch<import("react").SetStateAction<T[]>>;
60
+ /**
61
+ * A function to render a unique key for an option. Should be passed to the property `renderKey` of the checkbox group.
62
+ */
63
+ renderKey: (option: T) => string | number | undefined;
64
+ /**
65
+ * A function that returns true if the option is filtered out, but checked.
66
+ * @param option the option to check.
67
+ * @returns true if the option was filtered out, but is checked; false otherwise.
68
+ */
69
+ isUnfilteredButChecked: (o: T) => boolean;
70
+ /**
71
+ * True if all options showing are selected, false otherwise.
72
+ */
73
+ isAllSelected: boolean;
74
+ };
75
+ //# sourceMappingURL=checkbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkbox.d.ts","sourceRoot":"","sources":["../../src/utils/checkbox.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,uBAAuB,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM;IACpD;;;;OAIG;IACH,YAAY,CAAC,EAAE,CAAC,EAAE,CAAC;IACnB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC;IAChD;;OAEG;IACH,OAAO,EAAE,CAAC,EAAE,CAAC;IACb;;;;OAIG;IACH,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACvD;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC;IA+BzF;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;8BArEqB,MAAM,GAAG,MAAM,GAAG,SAAS;IAuEnD;;;;OAIG;gCAhDmF,CAAC;IAkDvF;;OAEG;;EAGN"}
@@ -0,0 +1,81 @@
1
+ import { useCallback, useMemo, useState } from 'react';
2
+ import { defaultRenderKey } from './options.js';
3
+ /**
4
+ * Use this hook to easily implement filtering and selection controls for a checkbox group.
5
+ * @param params the parameters to create the controls.
6
+ * @returns the checkbox controls.
7
+ */
8
+ export function useCheckboxGroupControls(params) {
9
+ const [value, setValue] = useState(params.initialValue ?? []);
10
+ const [filter, setFilter] = useState();
11
+ const renderKey = params.renderKey ?? defaultRenderKey;
12
+ const { options, isUnfilteredButChecked } = useMemo(() => {
13
+ if (!params.applyFilter || !filter)
14
+ return { options: params.options, isUnfilteredButChecked: () => false };
15
+ const filtered = [];
16
+ const unfilteredButChecked = [];
17
+ const map = new Map();
18
+ const valueKeys = value.map(o => renderKey(o));
19
+ for (const o of params.options) {
20
+ const key = renderKey(o);
21
+ if (params.applyFilter(filter, o))
22
+ filtered.push(o);
23
+ else if (valueKeys.includes(key)) {
24
+ unfilteredButChecked.push(o);
25
+ map.set(key, true);
26
+ }
27
+ }
28
+ return { options: [...unfilteredButChecked, ...filtered], isUnfilteredButChecked: (o) => map.has(renderKey(o)) };
29
+ }, [params.options, filter]);
30
+ const selectAll = useCallback(() => {
31
+ setValue([...options]);
32
+ }, [options]);
33
+ const removeSelection = useCallback(() => {
34
+ setValue([]);
35
+ }, []);
36
+ return {
37
+ /**
38
+ * Selects all the options currently visible.
39
+ */
40
+ selectAll,
41
+ /**
42
+ * Removes all options from the selection.
43
+ */
44
+ removeSelection,
45
+ /**
46
+ * The current filter applied.
47
+ */
48
+ filter,
49
+ /**
50
+ * Apply a new filter.
51
+ */
52
+ setFilter: setFilter,
53
+ /**
54
+ * The options that should be passed to the checkbox group.
55
+ */
56
+ options,
57
+ /**
58
+ * The value that should be passed to the checkbox group.
59
+ */
60
+ value,
61
+ /**
62
+ * Changes the current value, should be passed to the property `onChange` of the checkbox group.
63
+ */
64
+ setValue,
65
+ /**
66
+ * A function to render a unique key for an option. Should be passed to the property `renderKey` of the checkbox group.
67
+ */
68
+ renderKey,
69
+ /**
70
+ * A function that returns true if the option is filtered out, but checked.
71
+ * @param option the option to check.
72
+ * @returns true if the option was filtered out, but is checked; false otherwise.
73
+ */
74
+ isUnfilteredButChecked,
75
+ /**
76
+ * True if all options showing are selected, false otherwise.
77
+ */
78
+ isAllSelected: value.length === options.length,
79
+ };
80
+ }
81
+ //# sourceMappingURL=checkbox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkbox.js","sourceRoot":"","sources":["../../src/utils/checkbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAA;AA4B5C;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAgB,MAAqC;IAC3F,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC,CAAA;IAC7D,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,EAAiB,CAAA;IACrD,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,gBAAgB,CAAA;IAEtD,MAAM,EAAE,OAAO,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE;QACvD,IAAI,CAAC,MAAM,CAAC,WAAW,IAAI,CAAC,MAAM;YAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,sBAAsB,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAA;QAC3G,MAAM,QAAQ,GAAQ,EAAE,CAAA;QACxB,MAAM,oBAAoB,GAAQ,EAAE,CAAA;QACpC,MAAM,GAAG,GAAG,IAAI,GAAG,EAAwC,CAAA;QAC3D,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAA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ACT;;;;WAIG;QACH,sBAAsB;QACtB;;WAEG;QACH,aAAa,EAAE,KAAK,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM;KAC/C,CAAA;AACH,CAAC"}
@@ -0,0 +1,61 @@
1
+ export interface RadioGroupHookParams<T, F = string> {
2
+ /**
3
+ * The initial value for the radio group.
4
+ */
5
+ initialValue?: T;
6
+ /**
7
+ * A function to apply a filter to an option. Must return true if the option respects the filter and false otherwise.
8
+ * @param filter the current filter.
9
+ * @param option the current option.
10
+ * @returns true if the option should pass the filter, false if the option should be discarded.
11
+ */
12
+ applyFilter?: (filter: F, option: T) => boolean;
13
+ /**
14
+ * The full set of options for the radio group.
15
+ */
16
+ options: T[];
17
+ /**
18
+ * A function that produces a unique id for an option.
19
+ * @param option the current option.
20
+ * @returns a unique key.
21
+ */
22
+ renderKey: (option: T) => string | number | undefined;
23
+ }
24
+ /**
25
+ * Use this hook to easily implement filtering for a radio group.
26
+ * @param params the parameters to create the controls.
27
+ * @returns the checkbox controls.
28
+ */
29
+ export declare function useRadioGroupControls<T, F = string>(params: RadioGroupHookParams<T, F>): {
30
+ /**
31
+ * The current filter applied.
32
+ */
33
+ filter: F | undefined;
34
+ /**
35
+ * Apply a new filter.
36
+ */
37
+ setFilter: import("react").Dispatch<import("react").SetStateAction<F | undefined>>;
38
+ /**
39
+ * The options that should be passed to the radio group.
40
+ */
41
+ options: T[];
42
+ /**
43
+ * The value that should be passed to the radio group.
44
+ */
45
+ value: T | undefined;
46
+ /**
47
+ * Changes the current value, should be passed to the property `onChange` of the radio group.
48
+ */
49
+ setValue: import("react").Dispatch<import("react").SetStateAction<T | undefined>>;
50
+ /**
51
+ * A function to render a unique key for an option. Should be passed to the property `renderKey` of the radio group.
52
+ */
53
+ renderKey: (option: T) => string | number | undefined;
54
+ /**
55
+ * A function that returns true if the option is filtered out, but checked.
56
+ * @param option the option to check.
57
+ * @returns true if the option was filtered out, but is checked; false otherwise.
58
+ */
59
+ isUnfilteredButChecked: (o: T) => boolean | undefined;
60
+ };
61
+ //# sourceMappingURL=radio.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"radio.d.ts","sourceRoot":"","sources":["../../src/utils/radio.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,oBAAoB,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM;IACjD;;OAEG;IACH,YAAY,CAAC,EAAE,CAAC,CAAC;IACjB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC;IAChD;;OAEG;IACH,OAAO,EAAE,CAAC,EAAE,CAAC;IACb;;;;OAIG;IACH,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACvD;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC;IAiBnF;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;8BA/CqB,MAAM,GAAG,MAAM,GAAG,SAAS;IAiDnD;;;;OAIG;gCAjC2B,CAAC;EAoClC"}
@@ -0,0 +1,56 @@
1
+ import { useMemo, useState } from 'react';
2
+ import { defaultRenderKey } from './options.js';
3
+ /**
4
+ * Use this hook to easily implement filtering for a radio group.
5
+ * @param params the parameters to create the controls.
6
+ * @returns the checkbox controls.
7
+ */
8
+ export function useRadioGroupControls(params) {
9
+ const [value, setValue] = useState(params.initialValue);
10
+ const [filter, setFilter] = useState();
11
+ const renderKey = params.renderKey ?? defaultRenderKey;
12
+ const { options, isUnfilteredButChecked } = useMemo(() => {
13
+ if (!params.applyFilter || !filter)
14
+ return { options: params.options, isUnfilteredButChecked: () => false };
15
+ const valueKey = value ? renderKey(value) : undefined;
16
+ const isValueFilteredOut = value && !params.applyFilter?.(filter, value);
17
+ const filtered = params.options.filter(o => params.applyFilter?.(filter, o));
18
+ return {
19
+ options: isValueFilteredOut ? [value, ...filtered] : filtered,
20
+ isUnfilteredButChecked: (o) => isValueFilteredOut && renderKey(o) === valueKey,
21
+ };
22
+ }, [params.options, filter]);
23
+ return {
24
+ /**
25
+ * The current filter applied.
26
+ */
27
+ filter,
28
+ /**
29
+ * Apply a new filter.
30
+ */
31
+ setFilter: setFilter,
32
+ /**
33
+ * The options that should be passed to the radio group.
34
+ */
35
+ options,
36
+ /**
37
+ * The value that should be passed to the radio group.
38
+ */
39
+ value,
40
+ /**
41
+ * Changes the current value, should be passed to the property `onChange` of the radio group.
42
+ */
43
+ setValue,
44
+ /**
45
+ * A function to render a unique key for an option. Should be passed to the property `renderKey` of the radio group.
46
+ */
47
+ renderKey,
48
+ /**
49
+ * A function that returns true if the option is filtered out, but checked.
50
+ * @param option the option to check.
51
+ * @returns true if the option was filtered out, but is checked; false otherwise.
52
+ */
53
+ isUnfilteredButChecked,
54
+ };
55
+ }
56
+ //# sourceMappingURL=radio.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"radio.js","sourceRoot":"","sources":["../../src/utils/radio.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAA;AA0B5C;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAgB,MAAkC;IACrF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;IACvD,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,EAAiB,CAAA;IACrD,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,gBAAgB,CAAA;IAEtD,MAAM,EAAE,OAAO,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE;QACvD,IAAI,CAAC,MAAM,CAAC,WAAW,IAAI,CAAC,MAAM;YAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,sBAAsB,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAA;QAC3G,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QACrD,MAAM,kBAAkB,GAAG,KAAK,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;QACxE,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAA;QAC5E,OAAO;YACL,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ;YAC7D,sBAAsB,EAAE,CAAC,CAAI,EAAE,EAAE,CAAC,kBAAkB,IAAI,SAAS,CAAC,CAAC,CAAC,KAAK,QAAQ;SAClF,CAAA;IACH,CAAC,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAA;IAE5B,OAAO;QACL;;WAEG;QACH,MAAM;QACN;;WAEG;QACH,SAAS,EAAE,SAAS;QACpB;;WAEG;QACH,OAAO;QACP;;WAEG;QACH,KAAK;QACL;;WAEG;QACH,QAAQ;QACR;;WAEG;QACH,SAAS;QACT;;;;WAIG;QACH,sBAAsB;KACvB,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stack-spot/citric-react",
3
- "version": "0.15.4",
3
+ "version": "0.16.0",
4
4
  "author": "StackSpot",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -49,4 +49,6 @@ export * from './components/Textarea'
49
49
  export * from './components/Tooltip'
50
50
  export * from './context/CitricProvider'
51
51
  export type { CitricController } from './types'
52
+ export * from './utils/checkbox'
53
+ export * from './utils/radio'
52
54
  export * from './utils/ValueController'
@@ -0,0 +1,109 @@
1
+ import { useCallback, useMemo, useState } from 'react'
2
+ import { defaultRenderKey } from './options'
3
+
4
+ export interface CheckboxGroupHookParams<T, F = string> {
5
+ /**
6
+ * The initial value for the checkbox group.
7
+ *
8
+ * @default []
9
+ */
10
+ initialValue?: T[],
11
+ /**
12
+ * A function to apply a filter to an option. Must return true if the option respects the filter and false otherwise.
13
+ * @param filter the current filter.
14
+ * @param option the current option.
15
+ * @returns true if the option should pass the filter, false if the option should be discarded.
16
+ */
17
+ applyFilter?: (filter: F, option: T) => boolean,
18
+ /**
19
+ * The full set of options for the checkbox group.
20
+ */
21
+ options: T[],
22
+ /**
23
+ * A function that produces a unique id for an option.
24
+ * @param option the current option.
25
+ * @returns a unique key.
26
+ */
27
+ renderKey: (option: T) => string | number | undefined,
28
+ }
29
+
30
+ /**
31
+ * Use this hook to easily implement filtering and selection controls for a checkbox group.
32
+ * @param params the parameters to create the controls.
33
+ * @returns the checkbox controls.
34
+ */
35
+ export function useCheckboxGroupControls<T, F = string>(params: CheckboxGroupHookParams<T, F>) {
36
+ const [value, setValue] = useState(params.initialValue ?? [])
37
+ const [filter, setFilter] = useState<F | undefined>()
38
+ const renderKey = params.renderKey ?? defaultRenderKey
39
+
40
+ const { options, isUnfilteredButChecked } = useMemo(() => {
41
+ if (!params.applyFilter || !filter) return { options: params.options, isUnfilteredButChecked: () => false }
42
+ const filtered: T[] = []
43
+ const unfilteredButChecked: T[] = []
44
+ const map = new Map<number | string | undefined, boolean>()
45
+ const valueKeys = value.map(o => renderKey(o))
46
+ for (const o of params.options) {
47
+ const key = renderKey(o)
48
+ if (params.applyFilter(filter, o)) filtered.push(o)
49
+ else if (valueKeys.includes(key)) {
50
+ unfilteredButChecked.push(o)
51
+ map.set(key, true)
52
+ }
53
+ }
54
+ return { options: [...unfilteredButChecked, ...filtered], isUnfilteredButChecked: (o: T) => map.has(renderKey(o)) }
55
+ }, [params.options, filter])
56
+
57
+ const selectAll = useCallback(() => {
58
+ setValue([...options])
59
+ }, [options])
60
+
61
+ const removeSelection = useCallback(() => {
62
+ setValue([])
63
+ }, [])
64
+
65
+ return {
66
+ /**
67
+ * Selects all the options currently visible.
68
+ */
69
+ selectAll,
70
+ /**
71
+ * Removes all options from the selection.
72
+ */
73
+ removeSelection,
74
+ /**
75
+ * The current filter applied.
76
+ */
77
+ filter,
78
+ /**
79
+ * Apply a new filter.
80
+ */
81
+ setFilter: setFilter,
82
+ /**
83
+ * The options that should be passed to the checkbox group.
84
+ */
85
+ options,
86
+ /**
87
+ * The value that should be passed to the checkbox group.
88
+ */
89
+ value,
90
+ /**
91
+ * Changes the current value, should be passed to the property `onChange` of the checkbox group.
92
+ */
93
+ setValue,
94
+ /**
95
+ * A function to render a unique key for an option. Should be passed to the property `renderKey` of the checkbox group.
96
+ */
97
+ renderKey,
98
+ /**
99
+ * A function that returns true if the option is filtered out, but checked.
100
+ * @param option the option to check.
101
+ * @returns true if the option was filtered out, but is checked; false otherwise.
102
+ */
103
+ isUnfilteredButChecked,
104
+ /**
105
+ * True if all options showing are selected, false otherwise.
106
+ */
107
+ isAllSelected: value.length === options.length,
108
+ }
109
+ }
@@ -0,0 +1,81 @@
1
+ import { useMemo, useState } from 'react'
2
+ import { defaultRenderKey } from './options'
3
+
4
+ export interface RadioGroupHookParams<T, F = string> {
5
+ /**
6
+ * The initial value for the radio group.
7
+ */
8
+ initialValue?: T,
9
+ /**
10
+ * A function to apply a filter to an option. Must return true if the option respects the filter and false otherwise.
11
+ * @param filter the current filter.
12
+ * @param option the current option.
13
+ * @returns true if the option should pass the filter, false if the option should be discarded.
14
+ */
15
+ applyFilter?: (filter: F, option: T) => boolean,
16
+ /**
17
+ * The full set of options for the radio group.
18
+ */
19
+ options: T[],
20
+ /**
21
+ * A function that produces a unique id for an option.
22
+ * @param option the current option.
23
+ * @returns a unique key.
24
+ */
25
+ renderKey: (option: T) => string | number | undefined,
26
+ }
27
+
28
+ /**
29
+ * Use this hook to easily implement filtering for a radio group.
30
+ * @param params the parameters to create the controls.
31
+ * @returns the checkbox controls.
32
+ */
33
+ export function useRadioGroupControls<T, F = string>(params: RadioGroupHookParams<T, F>) {
34
+ const [value, setValue] = useState(params.initialValue)
35
+ const [filter, setFilter] = useState<F | undefined>()
36
+ const renderKey = params.renderKey ?? defaultRenderKey
37
+
38
+ const { options, isUnfilteredButChecked } = useMemo(() => {
39
+ if (!params.applyFilter || !filter) return { options: params.options, isUnfilteredButChecked: () => false }
40
+ const valueKey = value ? renderKey(value) : undefined
41
+ const isValueFilteredOut = value && !params.applyFilter?.(filter, value)
42
+ const filtered = params.options.filter(o => params.applyFilter?.(filter, o))
43
+ return {
44
+ options: isValueFilteredOut ? [value, ...filtered] : filtered,
45
+ isUnfilteredButChecked: (o: T) => isValueFilteredOut && renderKey(o) === valueKey,
46
+ }
47
+ }, [params.options, filter])
48
+
49
+ return {
50
+ /**
51
+ * The current filter applied.
52
+ */
53
+ filter,
54
+ /**
55
+ * Apply a new filter.
56
+ */
57
+ setFilter: setFilter,
58
+ /**
59
+ * The options that should be passed to the radio group.
60
+ */
61
+ options,
62
+ /**
63
+ * The value that should be passed to the radio group.
64
+ */
65
+ value,
66
+ /**
67
+ * Changes the current value, should be passed to the property `onChange` of the radio group.
68
+ */
69
+ setValue,
70
+ /**
71
+ * A function to render a unique key for an option. Should be passed to the property `renderKey` of the radio group.
72
+ */
73
+ renderKey,
74
+ /**
75
+ * A function that returns true if the option is filtered out, but checked.
76
+ * @param option the option to check.
77
+ * @returns true if the option was filtered out, but is checked; false otherwise.
78
+ */
79
+ isUnfilteredButChecked,
80
+ }
81
+ }