ag-common 0.0.237 → 0.0.240
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/api/helpers/validateOpenApi.d.ts +3 -1
- package/dist/api/helpers/validateOpenApi.js +3 -2
- package/dist/api/helpers/validations.d.ts +9 -8
- package/dist/common/helpers/string.d.ts +1 -0
- package/dist/common/helpers/string.js +12 -1
- package/dist/ui/components/CodeBlock/CodeBlock.d.ts +3 -0
- package/dist/ui/components/CodeBlock/CodeBlock.js +77 -0
- package/dist/ui/components/CodeBlock/helpers/common.d.ts +21 -0
- package/dist/ui/components/CodeBlock/helpers/common.js +107 -0
- package/dist/ui/components/CodeBlock/helpers/security.d.ts +8 -0
- package/dist/ui/components/CodeBlock/helpers/security.js +37 -0
- package/dist/ui/components/CodeBlock/index.d.ts +2 -0
- package/dist/ui/components/CodeBlock/index.js +18 -0
- package/dist/ui/components/CodeBlock/types.d.ts +9 -0
- package/dist/ui/components/CodeBlock/types.js +2 -0
- package/dist/ui/components/DropdownList/index.js +27 -40
- package/dist/ui/components/DropdownList/types.d.ts +14 -3
- package/dist/ui/components/KebabDots/index.d.ts +4 -0
- package/dist/ui/components/KebabDots/index.js +16 -0
- package/dist/ui/components/index.d.ts +2 -1
- package/dist/ui/components/index.js +2 -1
- package/package.json +1 -1
- package/dist/ui/components/Dropdown/Modal.d.ts +0 -12
- package/dist/ui/components/Dropdown/Modal.js +0 -20
- package/dist/ui/components/Dropdown/index.d.ts +0 -13
- package/dist/ui/components/Dropdown/index.js +0 -70
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TGetAndValidateToken } from './validations';
|
|
1
2
|
import { User } from '../../ui/helpers/jwt';
|
|
2
3
|
import { TLang } from '../../common/helpers/i18n';
|
|
3
4
|
import { APIGatewayEvent, APIGatewayProxyResult } from '../types';
|
|
@@ -8,7 +9,8 @@ export declare type NextType<T> = ({ event, body, params, userProfile, lang, }:
|
|
|
8
9
|
userProfile?: User;
|
|
9
10
|
lang: TLang;
|
|
10
11
|
}) => Promise<APIGatewayProxyResult>;
|
|
11
|
-
export declare function validateOpenApi<T>({ event, next, authorized, schema, COGNITO_USER_POOL_ID, jwksRegion, }: {
|
|
12
|
+
export declare function validateOpenApi<T>({ event, next, authorized, schema, COGNITO_USER_POOL_ID, jwksRegion, getAndValidateTokenOverride, }: {
|
|
13
|
+
getAndValidateTokenOverride?: TGetAndValidateToken;
|
|
12
14
|
COGNITO_USER_POOL_ID: string;
|
|
13
15
|
schema: any;
|
|
14
16
|
event: APIGatewayEvent;
|
|
@@ -43,7 +43,7 @@ const getOperation = ({ path, method, resource, schema, }) => {
|
|
|
43
43
|
const pathParams = (re === null || re === void 0 ? void 0 : re.groups) && JSON.parse(JSON.stringify(re === null || re === void 0 ? void 0 : re.groups));
|
|
44
44
|
return { operation, pathParams };
|
|
45
45
|
};
|
|
46
|
-
function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID, jwksRegion = 'ap-southeast-2', }) {
|
|
46
|
+
function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID, jwksRegion = 'ap-southeast-2', getAndValidateTokenOverride, }) {
|
|
47
47
|
var _a, _b, _c, _d, _e;
|
|
48
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
49
49
|
if (!schema) {
|
|
@@ -104,7 +104,8 @@ function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID
|
|
|
104
104
|
let error;
|
|
105
105
|
const authHeader = ((_c = event.headers) === null || _c === void 0 ? void 0 : _c.Authorization) || ((_d = event.headers) === null || _d === void 0 ? void 0 : _d.authorization);
|
|
106
106
|
if (authorized === true || (authorized === 'optional' && authHeader)) {
|
|
107
|
-
|
|
107
|
+
const vf = getAndValidateTokenOverride !== null && getAndValidateTokenOverride !== void 0 ? getAndValidateTokenOverride : validations_1.getAndValidateToken;
|
|
108
|
+
({ error, userProfile } = yield vf({
|
|
108
109
|
tokenRaw: authHeader,
|
|
109
110
|
COGNITO_USER_POOL_ID,
|
|
110
111
|
jwksRegion,
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { error } from '../../common/helpers/log';
|
|
2
1
|
import { User } from '../../ui/helpers/jwt';
|
|
3
2
|
import { APIGatewayProxyResult } from '../types';
|
|
4
|
-
export
|
|
3
|
+
export interface IGetAndValidateToken {
|
|
5
4
|
/**
|
|
6
5
|
* default ap-southeast-2
|
|
7
6
|
*/
|
|
8
|
-
jwksRegion?: string
|
|
9
|
-
tokenRaw?: string
|
|
7
|
+
jwksRegion?: string;
|
|
8
|
+
tokenRaw?: string;
|
|
10
9
|
COGNITO_USER_POOL_ID: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
}
|
|
11
|
+
export declare type TGetAndValidateToken = (p: IGetAndValidateToken) => Promise<{
|
|
12
|
+
error?: APIGatewayProxyResult;
|
|
13
|
+
token?: string;
|
|
14
|
+
userProfile?: User;
|
|
15
15
|
}>;
|
|
16
|
+
export declare const getAndValidateToken: TGetAndValidateToken;
|
|
@@ -60,3 +60,4 @@ export declare const chunkString: (str: string, length: number) => string[];
|
|
|
60
60
|
* @param splitKeys eg &
|
|
61
61
|
*/
|
|
62
62
|
export declare function stringToObject(raw: string, splitKeyValue: string, splitKeys: string): Record<string, string>;
|
|
63
|
+
export declare const indexOfNumber: (str: string, char: string, num?: number) => number | undefined;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stringToObject = exports.chunkString = exports.safeStringify = exports.containsInsensitive = exports.containsInsensitiveIndex = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = exports.fromBase64 = exports.toBase64 = void 0;
|
|
3
|
+
exports.indexOfNumber = exports.stringToObject = exports.chunkString = exports.safeStringify = exports.containsInsensitive = exports.containsInsensitiveIndex = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = exports.fromBase64 = exports.toBase64 = void 0;
|
|
4
4
|
const toBase64 = (str) => Buffer.from(str).toString('base64');
|
|
5
5
|
exports.toBase64 = toBase64;
|
|
6
6
|
const fromBase64 = (str) => Buffer.from(decodeURIComponent(str), 'base64').toString();
|
|
@@ -186,3 +186,14 @@ function stringToObject(raw, splitKeyValue, splitKeys) {
|
|
|
186
186
|
return ret;
|
|
187
187
|
}
|
|
188
188
|
exports.stringToObject = stringToObject;
|
|
189
|
+
const indexOfNumber = (str, char, num = 0) => {
|
|
190
|
+
let ret = -1;
|
|
191
|
+
for (let c = 0; c <= num; c += 1) {
|
|
192
|
+
ret = str.indexOf(char, ret + 1);
|
|
193
|
+
}
|
|
194
|
+
if (ret === -1) {
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
return ret;
|
|
198
|
+
};
|
|
199
|
+
exports.indexOfNumber = indexOfNumber;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CodeBlock = void 0;
|
|
7
|
+
const common_1 = require("./helpers/common");
|
|
8
|
+
const FlexRow_1 = require("../FlexRow");
|
|
9
|
+
const react_1 = __importDefault(require("react"));
|
|
10
|
+
const styled_components_1 = __importDefault(require("styled-components"));
|
|
11
|
+
const Base = styled_components_1.default.div `
|
|
12
|
+
display: flex;
|
|
13
|
+
flex-flow: column;
|
|
14
|
+
width: 100%;
|
|
15
|
+
flex-grow: 1;
|
|
16
|
+
border: solid 1px #333;
|
|
17
|
+
margin: 1rem;
|
|
18
|
+
padding: 1rem;
|
|
19
|
+
`;
|
|
20
|
+
const Verb = styled_components_1.default.div `
|
|
21
|
+
background-color: #333;
|
|
22
|
+
color: white;
|
|
23
|
+
margin-right: 0.5rem;
|
|
24
|
+
padding: 0.25rem;
|
|
25
|
+
font-size: 0.8rem;
|
|
26
|
+
`;
|
|
27
|
+
const ApiName = styled_components_1.default.div `
|
|
28
|
+
font-weight: bold;
|
|
29
|
+
font-size: 1.5rem;
|
|
30
|
+
`;
|
|
31
|
+
const BlockTitle = styled_components_1.default.div `
|
|
32
|
+
color: #333;
|
|
33
|
+
font-size: 1.2rem;
|
|
34
|
+
margin-top: 2rem;
|
|
35
|
+
`;
|
|
36
|
+
const Block = styled_components_1.default.div `
|
|
37
|
+
background-color: rgba(0, 0, 0, 0.1);
|
|
38
|
+
padding: 1rem;
|
|
39
|
+
margin-top: 0.5rem;
|
|
40
|
+
`;
|
|
41
|
+
const Curl = styled_components_1.default.div `
|
|
42
|
+
white-space: pre;
|
|
43
|
+
`;
|
|
44
|
+
const CodeBlock = (p) => {
|
|
45
|
+
var _a;
|
|
46
|
+
const { path, verb, error, headerLines, fullApiUrl, bodyLine, operation } = (0, common_1.getLines)(p);
|
|
47
|
+
if (error || !verb) {
|
|
48
|
+
return react_1.default.createElement(Base, null, error);
|
|
49
|
+
}
|
|
50
|
+
return (react_1.default.createElement(Base, null,
|
|
51
|
+
react_1.default.createElement(FlexRow_1.FlexRow, { center: true },
|
|
52
|
+
react_1.default.createElement(Verb, null, verb),
|
|
53
|
+
react_1.default.createElement(ApiName, null, (_a = operation.description) !== null && _a !== void 0 ? _a : path)),
|
|
54
|
+
react_1.default.createElement(BlockTitle, null, "Definition"),
|
|
55
|
+
react_1.default.createElement(Block, null,
|
|
56
|
+
verb.toUpperCase(),
|
|
57
|
+
" ",
|
|
58
|
+
path),
|
|
59
|
+
react_1.default.createElement(BlockTitle, null, "Example Request"),
|
|
60
|
+
react_1.default.createElement(Block, null,
|
|
61
|
+
react_1.default.createElement(Curl, null,
|
|
62
|
+
"curl --request ",
|
|
63
|
+
verb.toUpperCase(),
|
|
64
|
+
"\u00A0\\",
|
|
65
|
+
react_1.default.createElement("br", null),
|
|
66
|
+
"--url ",
|
|
67
|
+
react_1.default.createElement(common_1.Highlight, null,
|
|
68
|
+
"'",
|
|
69
|
+
fullApiUrl,
|
|
70
|
+
"'"),
|
|
71
|
+
"\u00A0\\",
|
|
72
|
+
react_1.default.createElement("br", null),
|
|
73
|
+
react_1.default.createElement(react_1.default.Fragment, null, headerLines),
|
|
74
|
+
react_1.default.createElement("br", null),
|
|
75
|
+
bodyLine))));
|
|
76
|
+
};
|
|
77
|
+
exports.CodeBlock = CodeBlock;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { ICodeBlock } from '../types';
|
|
3
|
+
export declare const Highlight: import("styled-components").StyledComponent<"span", any, {}, never>;
|
|
4
|
+
export declare const Highlight2: import("styled-components").StyledComponent<"span", any, {}, never>;
|
|
5
|
+
export declare const getLines: <TDefaultApi>(p: ICodeBlock<TDefaultApi>) => {
|
|
6
|
+
error: string;
|
|
7
|
+
verb?: undefined;
|
|
8
|
+
path?: undefined;
|
|
9
|
+
headerLines?: undefined;
|
|
10
|
+
fullApiUrl?: undefined;
|
|
11
|
+
bodyLine?: undefined;
|
|
12
|
+
operation?: undefined;
|
|
13
|
+
} | {
|
|
14
|
+
verb: string | undefined;
|
|
15
|
+
path: string | undefined;
|
|
16
|
+
error: undefined;
|
|
17
|
+
headerLines: JSX.Element[];
|
|
18
|
+
fullApiUrl: any;
|
|
19
|
+
bodyLine: JSX.Element;
|
|
20
|
+
operation: any;
|
|
21
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getLines = exports.Highlight2 = exports.Highlight = void 0;
|
|
7
|
+
const security_1 = require("./security");
|
|
8
|
+
const string_1 = require("../../../../common/helpers/string");
|
|
9
|
+
const styled_components_1 = __importDefault(require("styled-components"));
|
|
10
|
+
const react_1 = __importDefault(require("react"));
|
|
11
|
+
exports.Highlight = styled_components_1.default.span `
|
|
12
|
+
color: green;
|
|
13
|
+
`;
|
|
14
|
+
exports.Highlight2 = styled_components_1.default.span `
|
|
15
|
+
color: indianred;
|
|
16
|
+
`;
|
|
17
|
+
const getApiUrl = (p) => {
|
|
18
|
+
return p.schema.servers[0].url;
|
|
19
|
+
};
|
|
20
|
+
const getFunctionName = (p) => {
|
|
21
|
+
let ret = p.funcF.toString();
|
|
22
|
+
const i1 = ret.indexOf('.') + 1;
|
|
23
|
+
const i2 = (0, string_1.indexOfNumber)(ret, '(', 1);
|
|
24
|
+
ret = ret.substring(i1, i2);
|
|
25
|
+
return ret;
|
|
26
|
+
};
|
|
27
|
+
const getOperation = (p) => {
|
|
28
|
+
let path;
|
|
29
|
+
let verb;
|
|
30
|
+
const func = getFunctionName(p);
|
|
31
|
+
Object.entries(p.schema.paths).forEach(([pathN, ops]) =>
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
Object.entries(ops).forEach(([verbN, op]) => {
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
if (op.operationId === func) {
|
|
36
|
+
path = pathN;
|
|
37
|
+
verb = verbN;
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
if (!path || !verb) {
|
|
41
|
+
return { error: 'operation not found' };
|
|
42
|
+
}
|
|
43
|
+
const operation = p.schema.paths[path][verb];
|
|
44
|
+
return { operation, verb, path };
|
|
45
|
+
};
|
|
46
|
+
const getBody = (p) => {
|
|
47
|
+
const body = p.funcF.toString();
|
|
48
|
+
const bstart = (0, string_1.indexOfNumber)(body, '(', 1);
|
|
49
|
+
const bend = !bstart ? undefined : body.lastIndexOf(')');
|
|
50
|
+
if (!bstart || !bend) {
|
|
51
|
+
return { content: react_1.default.createElement(react_1.default.Fragment, null), header: react_1.default.createElement(react_1.default.Fragment, null) };
|
|
52
|
+
}
|
|
53
|
+
const slice = body.substring(bstart + 1, bend);
|
|
54
|
+
const json = slice.replace(/([a-zA-Z0-9-]+[^"]):/gim, '"$1":');
|
|
55
|
+
const nice = JSON.stringify(JSON.parse(json), null, 2);
|
|
56
|
+
const content = (react_1.default.createElement(react_1.default.Fragment, null,
|
|
57
|
+
"-d @- <<'EOF'",
|
|
58
|
+
react_1.default.createElement("br", null),
|
|
59
|
+
react_1.default.createElement(exports.Highlight2, null, nice),
|
|
60
|
+
react_1.default.createElement("br", null),
|
|
61
|
+
"EOF"));
|
|
62
|
+
const header = (react_1.default.createElement(react_1.default.Fragment, null,
|
|
63
|
+
react_1.default.createElement("span", null, "--header "),
|
|
64
|
+
react_1.default.createElement(exports.Highlight, null,
|
|
65
|
+
react_1.default.createElement(exports.Highlight, null, "'Content-Type: application/json'"))));
|
|
66
|
+
return {
|
|
67
|
+
content,
|
|
68
|
+
header,
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const getLines = (p) => {
|
|
72
|
+
var _a, _b;
|
|
73
|
+
const ops = getOperation(p);
|
|
74
|
+
if (ops.error) {
|
|
75
|
+
return { error: ops.error };
|
|
76
|
+
}
|
|
77
|
+
const { operation, path, verb } = ops;
|
|
78
|
+
const apiUrl = getApiUrl(p);
|
|
79
|
+
const fullApiUrl = apiUrl + path;
|
|
80
|
+
const secline = (0, security_1.getSecurityLine)(p, { operation });
|
|
81
|
+
if (secline.error) {
|
|
82
|
+
return { error: secline.error };
|
|
83
|
+
}
|
|
84
|
+
const bodyLines = (_a = getBody(p)) !== null && _a !== void 0 ? _a : undefined;
|
|
85
|
+
const headerLines = [];
|
|
86
|
+
if (secline.content) {
|
|
87
|
+
headerLines.push(secline.content);
|
|
88
|
+
headerLines.push(react_1.default.createElement(react_1.default.Fragment, null,
|
|
89
|
+
' ',
|
|
90
|
+
"\\",
|
|
91
|
+
react_1.default.createElement("br", null)));
|
|
92
|
+
}
|
|
93
|
+
if (bodyLines.header) {
|
|
94
|
+
headerLines.push(bodyLines.header);
|
|
95
|
+
headerLines.push(react_1.default.createElement(react_1.default.Fragment, null, " \\"));
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
verb,
|
|
99
|
+
path,
|
|
100
|
+
error: undefined,
|
|
101
|
+
headerLines,
|
|
102
|
+
fullApiUrl,
|
|
103
|
+
bodyLine: (_b = bodyLines === null || bodyLines === void 0 ? void 0 : bodyLines.content) !== null && _b !== void 0 ? _b : undefined,
|
|
104
|
+
operation,
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
exports.getLines = getLines;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getSecurityLine = void 0;
|
|
7
|
+
const common_1 = require("./common");
|
|
8
|
+
const string_1 = require("../../../../common/helpers/string");
|
|
9
|
+
const react_1 = __importDefault(require("react"));
|
|
10
|
+
const getSecurityLine = (p,
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
{ operation }) => {
|
|
13
|
+
var _a;
|
|
14
|
+
const security1 = (_a = operation.security) === null || _a === void 0 ? void 0 : _a[0];
|
|
15
|
+
const security2 = !security1 ? undefined : Object.keys(security1)[0];
|
|
16
|
+
const security = !security2
|
|
17
|
+
? undefined
|
|
18
|
+
: p.schema.components.securitySchemes[security2];
|
|
19
|
+
if (security) {
|
|
20
|
+
if (security.in !== 'header') {
|
|
21
|
+
return { error: 'not supported sec in', content: react_1.default.createElement(react_1.default.Fragment, null) };
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
content: (react_1.default.createElement(react_1.default.Fragment, null,
|
|
25
|
+
react_1.default.createElement("span", null, "--header "),
|
|
26
|
+
react_1.default.createElement(common_1.Highlight, null,
|
|
27
|
+
"'",
|
|
28
|
+
(0, string_1.toTitleCase)(security.name),
|
|
29
|
+
":",
|
|
30
|
+
' ',
|
|
31
|
+
react_1.default.createElement(common_1.Highlight2, null, p.apiKey || '(API KEY)'),
|
|
32
|
+
"'"))),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return { content: react_1.default.createElement(react_1.default.Fragment, null) };
|
|
36
|
+
};
|
|
37
|
+
exports.getSecurityLine = getSecurityLine;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./CodeBlock"), exports);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
@@ -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 Icon_1 = require("../Icon");
|
|
28
27
|
const dom_1 = require("../../helpers/dom");
|
|
29
28
|
const common_1 = require("../../styles/common");
|
|
30
29
|
const useOnClickOutside_1 = require("../../helpers/useOnClickOutside");
|
|
31
30
|
const colours_1 = require("../../styles/colours");
|
|
31
|
+
const KebabDots_1 = require("../KebabDots");
|
|
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 `
|
|
@@ -43,13 +43,12 @@ const SBase = styled_components_1.default.div `
|
|
|
43
43
|
const DropItems = styled_components_1.default.div `
|
|
44
44
|
flex-flow: column;
|
|
45
45
|
z-index: 5;
|
|
46
|
-
|
|
47
46
|
display: none;
|
|
48
|
-
|
|
49
47
|
background-color: white;
|
|
50
48
|
cursor: default;
|
|
51
49
|
width: 100%;
|
|
52
|
-
|
|
50
|
+
position: absolute;
|
|
51
|
+
${({ shadow }) => shadow && (0, common_1.Shadow)(shadow)}
|
|
53
52
|
${({ maxHeight }) => maxHeight &&
|
|
54
53
|
(0, styled_components_1.css) `
|
|
55
54
|
max-height: ${maxHeight};
|
|
@@ -59,25 +58,19 @@ const DropItems = styled_components_1.default.div `
|
|
|
59
58
|
&[data-open='true'] {
|
|
60
59
|
display: flex;
|
|
61
60
|
}
|
|
62
|
-
&[data-defaultopen='false'] {
|
|
63
|
-
position: absolute;
|
|
64
|
-
${({ shadow }) => shadow && (0, common_1.Shadow)(shadow)}
|
|
65
|
-
}
|
|
66
|
-
`;
|
|
67
|
-
const Dots = (react_1.default.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", fillRule: "evenodd", clipRule: "evenodd" },
|
|
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" })));
|
|
69
|
-
const IconStyled = (0, styled_components_1.default)(Icon_1.Icon) `
|
|
70
|
-
position: absolute;
|
|
71
61
|
`;
|
|
72
|
-
const
|
|
62
|
+
const ListItemStyle = styled_components_1.default.div `
|
|
73
63
|
z-index: 1;
|
|
74
64
|
font-weight: 500;
|
|
75
65
|
padding-left: 0.5rem;
|
|
76
66
|
flex-grow: 1;
|
|
77
67
|
padding: 1rem;
|
|
78
68
|
cursor: pointer;
|
|
69
|
+
display: flex;
|
|
70
|
+
overflow: hidden;
|
|
71
|
+
justify-content: center;
|
|
72
|
+
align-items: center;
|
|
79
73
|
&[data-selected='true'] {
|
|
80
|
-
cursor: default;
|
|
81
74
|
opacity: 1 !important;
|
|
82
75
|
background-color: ${colours_1.colours.orangeRed} !important;
|
|
83
76
|
}
|
|
@@ -95,20 +88,18 @@ const SList = styled_components_1.default.div `
|
|
|
95
88
|
background-color: rgba(0, 0, 0, 0.2);
|
|
96
89
|
}
|
|
97
90
|
`;
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
} }, renderF(s))))));
|
|
105
|
-
};
|
|
91
|
+
const ListItem = ({ render, onChange, selected, }) => (react_1.default.createElement(ListItemStyle, { "data-selected": selected, onClick: (e) => {
|
|
92
|
+
if (!selected) {
|
|
93
|
+
onChange === null || onChange === void 0 ? void 0 : onChange();
|
|
94
|
+
}
|
|
95
|
+
e.preventDefault();
|
|
96
|
+
} }, render));
|
|
106
97
|
function DropdownList(p) {
|
|
107
|
-
const { options, value, placeholder, className, renderF,
|
|
98
|
+
const { options, value, placeholder, className, renderF, shadow = '#555', maxHeight = '50vh', } = p;
|
|
108
99
|
const ref = (0, react_1.useRef)(null);
|
|
109
100
|
const [state, setState] = (0, react_1.useState)(value);
|
|
110
|
-
const [open, setOpen] = (0, react_1.useState)(
|
|
111
|
-
(0, useOnClickOutside_1.useOnClickOutside)({ disabled: !open
|
|
101
|
+
const [open, setOpen] = (0, react_1.useState)(false);
|
|
102
|
+
(0, useOnClickOutside_1.useOnClickOutside)({ disabled: !open, ref, moveMouseOutside: false }, () => {
|
|
112
103
|
setOpen(false);
|
|
113
104
|
});
|
|
114
105
|
(0, react_1.useEffect)(() => {
|
|
@@ -122,7 +113,7 @@ function DropdownList(p) {
|
|
|
122
113
|
var _a, _b, _c, _d;
|
|
123
114
|
const maxLen = Math.max(...options.map((s) => renderF(s).length));
|
|
124
115
|
const newStyle = {
|
|
125
|
-
|
|
116
|
+
minWidth: `calc(${maxLen}ch + 2rem)`,
|
|
126
117
|
};
|
|
127
118
|
const minPx = (0, dom_1.convertRemToPixels)(2 + maxLen / 2);
|
|
128
119
|
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;
|
|
@@ -130,7 +121,7 @@ function DropdownList(p) {
|
|
|
130
121
|
newStyle.left = '0';
|
|
131
122
|
}
|
|
132
123
|
else {
|
|
133
|
-
newStyle.right = '
|
|
124
|
+
newStyle.right = '0';
|
|
134
125
|
}
|
|
135
126
|
const b = (_d = (_c = ref === null || ref === void 0 ? void 0 : ref.current) === null || _c === void 0 ? void 0 : _c.getBoundingClientRect()) !== null && _d !== void 0 ? _d : { bottom: 0 };
|
|
136
127
|
const ih = typeof window !== 'undefined' ? window.innerHeight : 0;
|
|
@@ -139,26 +130,22 @@ function DropdownList(p) {
|
|
|
139
130
|
newStyle.bottom = '1rem';
|
|
140
131
|
}
|
|
141
132
|
else {
|
|
142
|
-
newStyle.top = '
|
|
133
|
+
newStyle.top = '0';
|
|
143
134
|
}
|
|
144
135
|
if (JSON.stringify(style) !== JSON.stringify(newStyle)) {
|
|
145
136
|
setStyle(newStyle);
|
|
146
137
|
}
|
|
147
|
-
}, [
|
|
138
|
+
}, [open, options, renderF, style]);
|
|
139
|
+
const defaultRender = !p.value ? react_1.default.createElement(KebabDots_1.KebabDots, null) : react_1.default.createElement(react_1.default.Fragment, null, p.renderF(p.value));
|
|
140
|
+
const defaultKey = !p.value ? '(noval)' : p.renderF(p.value);
|
|
141
|
+
const openDisplay = p.children || (react_1.default.createElement(ListItem, { selected: true, render: defaultRender, key: defaultKey }));
|
|
148
142
|
return (react_1.default.createElement(SBase, { className: className, ref: ref, title: placeholder, onClick: (e) => {
|
|
149
|
-
if (defaultOpen) {
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
143
|
e.stopPropagation();
|
|
153
144
|
e.preventDefault();
|
|
154
145
|
setOpen(!open);
|
|
155
146
|
} },
|
|
156
|
-
react_1.default.createElement(DropItems, { "data-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
e.stopPropagation();
|
|
160
|
-
e.preventDefault();
|
|
161
|
-
setOpen(!open);
|
|
162
|
-
} }, Dots)))));
|
|
147
|
+
react_1.default.createElement(DropItems, { "data-open": open, style: style, shadow: shadow, maxHeight: maxHeight }, open &&
|
|
148
|
+
options.map((s, i) => (react_1.default.createElement(ListItem, { key: p.renderF(s), render: p.renderF(s), onChange: () => p.onChange(s, i), selected: s === state })))),
|
|
149
|
+
openDisplay));
|
|
163
150
|
}
|
|
164
151
|
exports.DropdownList = DropdownList;
|
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
export interface IDropdownList<T> {
|
|
3
|
+
/**
|
|
4
|
+
* all items that can be in dropdown
|
|
5
|
+
*/
|
|
3
6
|
options: T[];
|
|
7
|
+
/**
|
|
8
|
+
* selected item from options.
|
|
9
|
+
*/
|
|
4
10
|
value?: T;
|
|
5
11
|
onChange: (v: T, index: number) => void;
|
|
12
|
+
/**
|
|
13
|
+
* placeholder title for list
|
|
14
|
+
*/
|
|
6
15
|
placeholder?: string;
|
|
7
16
|
className?: string;
|
|
17
|
+
/**
|
|
18
|
+
* function to render value
|
|
19
|
+
*/
|
|
8
20
|
renderF: (v: T) => string;
|
|
9
|
-
children?: JSX.Element;
|
|
10
21
|
/**
|
|
11
22
|
* colour of dropdown shadow. default #555
|
|
12
23
|
*/
|
|
@@ -16,7 +27,7 @@ export interface IDropdownList<T> {
|
|
|
16
27
|
*/
|
|
17
28
|
maxHeight?: string;
|
|
18
29
|
/**
|
|
19
|
-
* if
|
|
30
|
+
* if not provided, will default display value, then kebab dots
|
|
20
31
|
*/
|
|
21
|
-
|
|
32
|
+
children?: JSX.Element;
|
|
22
33
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.KebabDots = void 0;
|
|
7
|
+
const Icon_1 = require("../Icon");
|
|
8
|
+
const react_1 = __importDefault(require("react"));
|
|
9
|
+
const styled_components_1 = __importDefault(require("styled-components"));
|
|
10
|
+
const Dots = (react_1.default.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", fillRule: "evenodd", clipRule: "evenodd" },
|
|
11
|
+
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" })));
|
|
12
|
+
const IconStyled = (0, styled_components_1.default)(Icon_1.Icon) `
|
|
13
|
+
position: absolute;
|
|
14
|
+
`;
|
|
15
|
+
const KebabDots = ({ onClick }) => (react_1.default.createElement(IconStyled, { width: "2rem", height: "2rem", onClick: () => onClick === null || onClick === void 0 ? void 0 : onClick() }, Dots));
|
|
16
|
+
exports.KebabDots = KebabDots;
|
|
@@ -2,13 +2,14 @@ export * from './BorderGradient';
|
|
|
2
2
|
export * from './Button';
|
|
3
3
|
export * from './Chevron';
|
|
4
4
|
export * from './Close';
|
|
5
|
+
export * from './CodeBlock';
|
|
5
6
|
export * from './Confirm/Dialog';
|
|
6
|
-
export * from './Dropdown';
|
|
7
7
|
export * from './DropdownList';
|
|
8
8
|
export * from './FlexColumn';
|
|
9
9
|
export * from './FlexRow';
|
|
10
10
|
export * from './HeadersRaw';
|
|
11
11
|
export * from './Icon';
|
|
12
|
+
export * from './KebabDots';
|
|
12
13
|
export * from './Loader';
|
|
13
14
|
export * from './LoginButton';
|
|
14
15
|
export * from './LogoutButton';
|
|
@@ -18,13 +18,14 @@ __exportStar(require("./BorderGradient"), exports);
|
|
|
18
18
|
__exportStar(require("./Button"), exports);
|
|
19
19
|
__exportStar(require("./Chevron"), exports);
|
|
20
20
|
__exportStar(require("./Close"), exports);
|
|
21
|
+
__exportStar(require("./CodeBlock"), exports);
|
|
21
22
|
__exportStar(require("./Confirm/Dialog"), exports);
|
|
22
|
-
__exportStar(require("./Dropdown"), exports);
|
|
23
23
|
__exportStar(require("./DropdownList"), exports);
|
|
24
24
|
__exportStar(require("./FlexColumn"), exports);
|
|
25
25
|
__exportStar(require("./FlexRow"), exports);
|
|
26
26
|
__exportStar(require("./HeadersRaw"), exports);
|
|
27
27
|
__exportStar(require("./Icon"), exports);
|
|
28
|
+
__exportStar(require("./KebabDots"), exports);
|
|
28
29
|
__exportStar(require("./Loader"), exports);
|
|
29
30
|
__exportStar(require("./LoginButton"), exports);
|
|
30
31
|
__exportStar(require("./LogoutButton"), exports);
|
package/package.json
CHANGED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
export declare const DropdownModal: (p: {
|
|
3
|
-
options: (string | JSX.Element)[];
|
|
4
|
-
onSelect?: ((i: number, e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined;
|
|
5
|
-
position?: "left" | "right" | undefined;
|
|
6
|
-
topPosition?: "top" | "bottom" | undefined;
|
|
7
|
-
open: boolean;
|
|
8
|
-
setOpen: (b: boolean) => void;
|
|
9
|
-
closeOnMoveMouseOutside?: boolean | undefined;
|
|
10
|
-
showCloseButton?: boolean | undefined;
|
|
11
|
-
className?: string | undefined;
|
|
12
|
-
}) => JSX.Element;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DropdownModal = void 0;
|
|
7
|
-
const Modal_1 = require("../Modal");
|
|
8
|
-
const react_1 = __importDefault(require("react"));
|
|
9
|
-
const styled_components_1 = __importDefault(require("styled-components"));
|
|
10
|
-
const ModalDropdownStyled = (0, styled_components_1.default)(Modal_1.Modal) `
|
|
11
|
-
flex-flow: column;
|
|
12
|
-
`;
|
|
13
|
-
const DropdownModal = (p) => (react_1.default.createElement(ModalDropdownStyled, Object.assign({}, p, { className: p.className }), p.options.map((option, index) => (react_1.default.createElement(Modal_1.ModalItem, { key: option, onClick: (e) => {
|
|
14
|
-
var _a;
|
|
15
|
-
e.stopPropagation();
|
|
16
|
-
e.preventDefault();
|
|
17
|
-
(_a = p.onSelect) === null || _a === void 0 ? void 0 : _a.call(p, index, e);
|
|
18
|
-
p.setOpen(false);
|
|
19
|
-
} }, option)))));
|
|
20
|
-
exports.DropdownModal = DropdownModal;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
export declare const Dropdown: ({ options, position, topPosition, onSelect, className, enableHoverOpen, children, }: {
|
|
3
|
-
className?: string | undefined;
|
|
4
|
-
options: (string | JSX.Element)[];
|
|
5
|
-
topPosition?: "top" | "bottom" | undefined;
|
|
6
|
-
position?: "left" | "right" | undefined;
|
|
7
|
-
onSelect: (i: number, e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
|
|
8
|
-
enableHoverOpen?: boolean | undefined;
|
|
9
|
-
/**
|
|
10
|
-
* if not provided, will show ... dots to click to open (default)
|
|
11
|
-
*/
|
|
12
|
-
children?: JSX.Element | undefined;
|
|
13
|
-
}) => JSX.Element;
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.Dropdown = void 0;
|
|
30
|
-
const Modal_1 = require("./Modal");
|
|
31
|
-
const common_1 = require("../../styles/common");
|
|
32
|
-
const react_1 = __importStar(require("react"));
|
|
33
|
-
const styled_components_1 = __importDefault(require("styled-components"));
|
|
34
|
-
const Base = styled_components_1.default.div `
|
|
35
|
-
cursor: pointer;
|
|
36
|
-
`;
|
|
37
|
-
const Dots = (react_1.default.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", fillRule: "evenodd", clipRule: "evenodd" },
|
|
38
|
-
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" })));
|
|
39
|
-
const Icon = styled_components_1.default.div `
|
|
40
|
-
display: flex;
|
|
41
|
-
justify-content: center;
|
|
42
|
-
align-content: center;
|
|
43
|
-
align-items: center;
|
|
44
|
-
&[data-fixedsize='true'] {
|
|
45
|
-
width: 1.5rem;
|
|
46
|
-
height: 1.5rem;
|
|
47
|
-
top: -0.5rem;
|
|
48
|
-
right: -0.5rem;
|
|
49
|
-
position: absolute;
|
|
50
|
-
}
|
|
51
|
-
border: solid 1px #ccc;
|
|
52
|
-
border-radius: 50%;
|
|
53
|
-
background-color: white;
|
|
54
|
-
`;
|
|
55
|
-
const Dropdown = ({ options, position = 'left', topPosition, onSelect, className, enableHoverOpen = false, children, }) => {
|
|
56
|
-
const [open, setOpen] = (0, react_1.useState)(false);
|
|
57
|
-
const child = !children ? (react_1.default.createElement(Icon, { "data-fixedsize": true, onClick: (e) => {
|
|
58
|
-
e.stopPropagation();
|
|
59
|
-
e.preventDefault();
|
|
60
|
-
setOpen(!open);
|
|
61
|
-
} }, Dots)) : (react_1.default.createElement("div", { tabIndex: -1, role: "button", onKeyDown: (e) => e.key === 'Enter' && setOpen(!open), "data-fixedsize": false, onClick: (e) => {
|
|
62
|
-
e.stopPropagation();
|
|
63
|
-
e.preventDefault();
|
|
64
|
-
setOpen(!open);
|
|
65
|
-
} }, children));
|
|
66
|
-
return (react_1.default.createElement(Base, Object.assign({ onKeyPress: (e) => e.key === 'Enter' && setOpen(!open), onMouseEnter: () => enableHoverOpen && setOpen(true), onMouseLeave: () => enableHoverOpen && setOpen(false), className: className }, common_1.noDrag),
|
|
67
|
-
child,
|
|
68
|
-
react_1.default.createElement(Modal_1.DropdownModal, { position: position, topPosition: topPosition, options: options, open: open, setOpen: setOpen, onSelect: onSelect, showCloseButton: false })));
|
|
69
|
-
};
|
|
70
|
-
exports.Dropdown = Dropdown;
|