ag-common 0.0.232 → 0.0.235

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,18 +1,3 @@
1
1
  /// <reference types="react" />
2
- export declare function DropdownList<T>({ options, value, onChange, placeholder, className, renderF, children, shadow, maxHeight, }: {
3
- options: T[];
4
- value?: T;
5
- onChange: (v: T, index: number) => void;
6
- placeholder?: string;
7
- className?: string;
8
- renderF: (v: T) => string;
9
- children?: JSX.Element;
10
- /**
11
- * colour of dropdown shadow. default #555
12
- */
13
- shadow?: string;
14
- /**
15
- * max height of items list. default 50vh
16
- */
17
- maxHeight?: string;
18
- }): JSX.Element;
2
+ import { IDropdownList } from './types';
3
+ export declare function DropdownList<T>(p: IDropdownList<T>): JSX.Element;
@@ -24,11 +24,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.DropdownList = void 0;
27
- const colours_1 = require("../../styles/colours");
28
27
  const Icon_1 = require("../Icon");
29
28
  const dom_1 = require("../../helpers/dom");
30
29
  const common_1 = require("../../styles/common");
31
30
  const useOnClickOutside_1 = require("../../helpers/useOnClickOutside");
31
+ const colours_1 = require("../../styles/colours");
32
32
  const styled_components_1 = __importStar(require("styled-components"));
33
33
  const react_1 = __importStar(require("react"));
34
34
  const SBase = styled_components_1.default.div `
@@ -40,12 +40,12 @@ const SBase = styled_components_1.default.div `
40
40
  cursor: pointer;
41
41
  flex-grow: 0;
42
42
  `;
43
- const SItems = styled_components_1.default.div `
43
+ const DropItems = styled_components_1.default.div `
44
44
  flex-flow: column;
45
45
  z-index: 5;
46
46
 
47
47
  display: none;
48
- position: absolute;
48
+
49
49
  background-color: white;
50
50
  cursor: default;
51
51
  width: 100%;
@@ -56,36 +56,38 @@ const SItems = styled_components_1.default.div `
56
56
  `}
57
57
 
58
58
  overflow-y: auto;
59
- ${({ open }) => open &&
60
- (0, styled_components_1.css) `
61
- display: flex;
62
- `}
63
- ${({ shadow }) => shadow && (0, common_1.Shadow)(shadow)}
59
+ &[data-open='true'] {
60
+ display: flex;
61
+ }
62
+ &[data-defaultopen='false'] {
63
+ position: absolute;
64
+ ${({ shadow }) => shadow && (0, common_1.Shadow)(shadow)}
65
+ }
64
66
  `;
65
67
  const Dots = (react_1.default.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", fillRule: "evenodd", clipRule: "evenodd" },
66
68
  react_1.default.createElement("path", { d: "M16 12a3.001 3.001 0 016 0 3.001 3.001 0 01-6 0zm1 0a2 2 0 114.001.001A2 2 0 0117 12zm-8 0a3.001 3.001 0 016 0 3.001 3.001 0 01-6 0zm1 0a2 2 0 114.001.001A2 2 0 0110 12zm-8 0a3.001 3.001 0 016 0 3.001 3.001 0 01-6 0zm1 0a2 2 0 114.001.001A2 2 0 013 12z" })));
67
- const SItem = styled_components_1.default.div `
69
+ const IconStyled = (0, styled_components_1.default)(Icon_1.Icon) `
70
+ position: absolute;
71
+ `;
72
+ const SList = styled_components_1.default.div `
68
73
  z-index: 1;
69
74
  font-weight: 500;
70
75
  padding-left: 0.5rem;
71
76
  flex-grow: 1;
72
77
  padding: 1rem;
73
78
  cursor: pointer;
74
- ${({ selected }) => selected &&
75
- (0, styled_components_1.css) `
76
- opacity: 1 !important;
77
- `}
78
- &:hover {
79
- opacity: 0.9 !important;
80
- background-color: ${colours_1.colours.orange} !important;
79
+ &[data-selected='true'] {
80
+ cursor: default;
81
+ opacity: 1 !important;
82
+ background-color: ${colours_1.colours.orangeRed} !important;
81
83
  }
82
- ${({ selected }) => selected &&
83
- (0, styled_components_1.css) `
84
- background-color: ${colours_1.colours.orangeRed} !important;
85
- &:hover {
86
- background-color: ${colours_1.colours.orangeRed} !important;
87
- }
88
- `}
84
+ &[data-selected='false'] {
85
+ &:hover {
86
+ opacity: 0.9 !important;
87
+ background-color: ${colours_1.colours.orange} !important;
88
+ }
89
+ }
90
+
89
91
  &:nth-child(even) {
90
92
  background-color: rgba(0, 0, 0, 0.1);
91
93
  }
@@ -93,11 +95,20 @@ const SItem = styled_components_1.default.div `
93
95
  background-color: rgba(0, 0, 0, 0.2);
94
96
  }
95
97
  `;
96
- function DropdownList({ options, value, onChange, placeholder, className, renderF, children, shadow = '#555', maxHeight = '50vh', }) {
98
+ const List = ({ options, renderF, onChange, state, }) => {
99
+ return (react_1.default.createElement(react_1.default.Fragment, null, options.map((s, i) => (react_1.default.createElement(SList, { key: renderF(s), "data-selected": s === state, onClick: (e) => {
100
+ if (s !== state) {
101
+ onChange(s, i);
102
+ }
103
+ e.preventDefault();
104
+ } }, renderF(s))))));
105
+ };
106
+ function DropdownList(p) {
107
+ const { options, value, placeholder, className, renderF, children, shadow = '#555', maxHeight = '50vh', defaultOpen = false, } = p;
97
108
  const ref = (0, react_1.useRef)(null);
98
109
  const [state, setState] = (0, react_1.useState)(value);
99
- const [open, setOpen] = (0, react_1.useState)(false);
100
- (0, useOnClickOutside_1.useOnClickOutside)({ disabled: !open, ref, moveMouseOutside: false }, () => {
110
+ const [open, setOpen] = (0, react_1.useState)(defaultOpen);
111
+ (0, useOnClickOutside_1.useOnClickOutside)({ disabled: !open || defaultOpen, ref, moveMouseOutside: false }, () => {
101
112
  setOpen(false);
102
113
  });
103
114
  (0, react_1.useEffect)(() => {
@@ -111,7 +122,7 @@ function DropdownList({ options, value, onChange, placeholder, className, render
111
122
  var _a, _b, _c, _d;
112
123
  const maxLen = Math.max(...options.map((s) => renderF(s).length));
113
124
  const newStyle = {
114
- width: `calc(${maxLen}ch + 2rem)`,
125
+ width: defaultOpen ? '100%' : `calc(${maxLen}ch + 2rem)`,
115
126
  };
116
127
  const minPx = (0, dom_1.convertRemToPixels)(2 + maxLen / 2);
117
128
  const offsetLeft = (_b = (_a = ref === null || ref === void 0 ? void 0 : ref.current) === null || _a === void 0 ? void 0 : _a.offsetLeft) !== null && _b !== void 0 ? _b : 0;
@@ -133,23 +144,21 @@ function DropdownList({ options, value, onChange, placeholder, className, render
133
144
  if (JSON.stringify(style) !== JSON.stringify(newStyle)) {
134
145
  setStyle(newStyle);
135
146
  }
136
- }, [open, options, renderF, style]);
147
+ }, [defaultOpen, open, options, renderF, style]);
137
148
  return (react_1.default.createElement(SBase, { className: className, ref: ref, title: placeholder, onClick: (e) => {
149
+ if (defaultOpen) {
150
+ return;
151
+ }
138
152
  e.stopPropagation();
139
153
  e.preventDefault();
140
154
  setOpen(!open);
141
155
  } },
142
- react_1.default.createElement(SItems, { open: open, style: style, shadow: shadow, maxHeight: maxHeight }, open &&
143
- options.map((s, i) => (react_1.default.createElement(SItem, { key: renderF(s), selected: s === state, onClick: (e) => {
144
- if (s !== state) {
145
- onChange(s, i);
146
- }
156
+ react_1.default.createElement(DropItems, { "data-defaultopen": p.defaultOpen, "data-open": open, style: style, shadow: shadow, maxHeight: maxHeight }, open && react_1.default.createElement(List, Object.assign({}, p, { state: state }))),
157
+ children ||
158
+ (!defaultOpen && (react_1.default.createElement(IconStyled, { width: "2rem", height: "2rem", onClick: (e) => {
159
+ e.stopPropagation();
147
160
  e.preventDefault();
148
- } }, renderF(s))))),
149
- children || (react_1.default.createElement(Icon_1.Icon, { width: "2rem", height: "2rem", onClick: (e) => {
150
- e.stopPropagation();
151
- e.preventDefault();
152
- setOpen(!open);
153
- } }, Dots))));
161
+ setOpen(!open);
162
+ } }, Dots)))));
154
163
  }
155
164
  exports.DropdownList = DropdownList;
@@ -0,0 +1,22 @@
1
+ /// <reference types="react" />
2
+ export interface IDropdownList<T> {
3
+ options: T[];
4
+ value?: T;
5
+ onChange: (v: T, index: number) => void;
6
+ placeholder?: string;
7
+ className?: string;
8
+ renderF: (v: T) => string;
9
+ children?: JSX.Element;
10
+ /**
11
+ * colour of dropdown shadow. default #555
12
+ */
13
+ shadow?: string;
14
+ /**
15
+ * max height of items list. default 50vh
16
+ */
17
+ maxHeight?: string;
18
+ /**
19
+ * if true, will show expanded. default false
20
+ */
21
+ defaultOpen?: boolean;
22
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -139,9 +139,9 @@ const TextEdit = ({ defaultValue = '', defaultEditing, disableEdit = false, plac
139
139
  : ValueTextArea;
140
140
  return (react_1.default.createElement(ValueBoxEdit, Object.assign({}, common_2.noDrag, { className: className, "data-editing": "true",
141
141
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
142
- ref: ref, tabIndex: editing ? 0 : undefined, "data-nogrow": noGrow }, attributes),
142
+ ref: ref, tabIndex: -1, "data-nogrow": noGrow }, attributes),
143
143
  leftContent || null,
144
- react_1.default.createElement(Comp, { "data-editing": "true", "data-valuechange": valueChange.toString(), ref: taref, "data-type": "text", value: value, onChange: (v) => {
144
+ react_1.default.createElement(Comp, { tabIndex: editing ? 0 : undefined, "data-editing": "true", "data-valuechange": valueChange.toString(), ref: taref, "data-type": "text", value: value, onChange: (v) => {
145
145
  setValue(v.currentTarget.value);
146
146
  if (!allowUndo) {
147
147
  onSubmit(v.currentTarget.value, false);
@@ -22,6 +22,8 @@ export declare type TCallOpenApiCached<T, TDefaultApi> = ICallOpenApi<T, TDefaul
22
22
  export declare const setOpenApiCacheRaw: <T>(p: {
23
23
  cacheKey: string;
24
24
  overrideAuth?: OverrideAuth | undefined;
25
+ ssrCacheItems?: any;
26
+ cacheTtl?: number;
25
27
  }, data: T) => Promise<void>;
26
28
  /**
27
29
  * sync call to callOpenApiCache.
@@ -16,26 +16,39 @@ exports.callOpenApiCached = exports.callOpenApiCachedRaw = exports.setOpenApiCac
16
16
  const direct_1 = require("./direct");
17
17
  const cookie_1 = require("../cookie");
18
18
  const string_1 = require("../../../common/helpers/string");
19
+ const common_1 = require("../../../common");
19
20
  const node_cache_1 = __importDefault(require("node-cache"));
20
21
  let callOpenApiCache;
21
- function getCacheKey({ cacheKey, overrideAuth, }) {
22
- const authkeyPrefix = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) ||
22
+ /**
23
+ * cache differs per user and per SSR provided data
24
+ * @param param0
25
+ * @returns
26
+ */
27
+ function getCacheKey({ cacheKey, overrideAuth, ssrCacheItems, }) {
28
+ const authkeyPrefix1 = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) ||
23
29
  (0, cookie_1.getCookieString)({
24
30
  name: 'id_token',
25
31
  defaultValue: '',
26
32
  });
33
+ const authPref = !authkeyPrefix1 ? '' : (0, common_1.hashCode)((0, string_1.toBase64)(authkeyPrefix1));
34
+ const ssrCachePref = !ssrCacheItems
35
+ ? ''
36
+ : (0, common_1.hashCode)((0, string_1.toBase64)(JSON.stringify(ssrCacheItems)));
27
37
  let cacheKeyRet;
28
38
  if (cacheKey) {
29
- const pref = !authkeyPrefix ? '' : (0, string_1.toBase64)(authkeyPrefix);
30
- cacheKeyRet = cacheKey + '||' + pref;
39
+ cacheKeyRet = `${cacheKey}||${authPref}||${ssrCachePref}`;
31
40
  }
32
41
  return cacheKeyRet;
33
42
  }
34
43
  const setOpenApiCacheRaw = (p, data) => __awaiter(void 0, void 0, void 0, function* () {
35
44
  const userPrefixedCacheKey = getCacheKey(p);
36
- if (callOpenApiCache && userPrefixedCacheKey) {
37
- callOpenApiCache.set(userPrefixedCacheKey, data);
45
+ if (!userPrefixedCacheKey) {
46
+ return;
47
+ }
48
+ if (!callOpenApiCache) {
49
+ callOpenApiCache = new node_cache_1.default({ stdTTL: p.cacheTtl || 120 });
38
50
  }
51
+ callOpenApiCache.set(userPrefixedCacheKey, data);
39
52
  });
40
53
  exports.setOpenApiCacheRaw = setOpenApiCacheRaw;
41
54
  /**
@@ -6,10 +6,6 @@ export declare type TUseCallOpenApi<T> = AxiosWrapper<T> & {
6
6
  loaded: boolean;
7
7
  loadcount: number;
8
8
  setData: (d: TUseCallOpenApiDispatch<T | undefined>) => void;
9
- /**
10
- * call when you want to refetch, but at a later point (ie same hook/cachekey across different components)
11
- */
12
- invalidateCacheKey: () => void;
13
9
  };
14
10
  declare type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
15
11
  cacheKey: string;
@@ -51,7 +51,7 @@ const useCallOpenApi = (pIn) => {
51
51
  if (ng) {
52
52
  return;
53
53
  }
54
- setData((d) => (Object.assign(Object.assign({}, d), { loading: true })));
54
+ setData((d) => [d[0], Object.assign(Object.assign({}, d[1]), { loading: true })]);
55
55
  void run();
56
56
  }, [data, setData]);
57
57
  const ret = Object.assign(Object.assign({}, data[1]), { reFetch: () => __awaiter(void 0, void 0, void 0, function* () { return setData([data[0], defaultState(data[0], true)]); }), setData: (d) => {
@@ -59,7 +59,7 @@ const useCallOpenApi = (pIn) => {
59
59
  data[0],
60
60
  Object.assign(Object.assign({}, data[1]), { data: d(data[1].data), datetime: new Date().getTime() }),
61
61
  ]);
62
- }, invalidateCacheKey: () => (0, cached_1.setOpenApiCacheRaw)(data[0], undefined) });
62
+ } });
63
63
  return ret;
64
64
  };
65
65
  exports.useCallOpenApi = useCallOpenApi;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.232",
3
+ "version": "0.0.235",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",