ag-common 0.0.65 → 0.0.66

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.
@@ -120,10 +120,11 @@ function DropdownList({ options, value, onChange, placeholder, className, render
120
120
  setOpen(!open);
121
121
  } },
122
122
  react_1.default.createElement(SItems, { open: open, style: style }, open &&
123
- options.map((s, i) => (react_1.default.createElement(SItem, { key: renderF(s), selected: s === state, onClick: () => {
123
+ options.map((s, i) => (react_1.default.createElement(SItem, { key: renderF(s), selected: s === state, onClick: (e) => {
124
124
  if (s !== state) {
125
125
  onChange(s, i);
126
126
  }
127
+ e.preventDefault();
127
128
  } }, renderF(s))))),
128
129
  children || (react_1.default.createElement(Icon_1.Icon, { width: "2rem", height: "2rem", onClick: (e) => {
129
130
  e.stopPropagation();
@@ -1,12 +1,18 @@
1
1
  /// <reference types="react" />
2
- export declare const ValueReadonly: import("styled-components").StyledComponent<"div", any, {}, never>;
3
- export declare const TextEdit: ({ defaultValue, defaultEditing, onSubmit, canEdit, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine, }: {
2
+ import { StyledComponent } from 'styled-components';
3
+ export declare const ValueReadonly: StyledComponent<"div", any, {}, never>;
4
+ export declare const TextEdit: ({ defaultValue, defaultEditing, onSubmit, disableEdit, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine, }: {
4
5
  singleLine?: boolean | undefined;
5
6
  className?: string | undefined;
6
7
  defaultValue: string;
7
- defaultEditing?: boolean | undefined;
8
+ /**
9
+ * if truthy will enable. if focus is true, will also focus on open
10
+ */
11
+ defaultEditing?: {
12
+ focus: boolean;
13
+ } | undefined;
8
14
  onSubmit: (val: string) => void;
9
- canEdit?: boolean | undefined;
15
+ disableEdit?: boolean | undefined;
10
16
  placeholder?: string | undefined;
11
17
  onEditingChange?: ((editing: boolean) => void) | undefined;
12
18
  onClickOutsideWithNoValue?: (() => void) | undefined;
@@ -18,16 +18,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };
21
- var __importDefault = (this && this.__importDefault) || function (mod) {
22
- return (mod && mod.__esModule) ? mod : { "default": mod };
23
- };
24
21
  Object.defineProperty(exports, "__esModule", { value: true });
25
22
  exports.TextEdit = exports.ValueReadonly = void 0;
26
23
  /* eslint-disable jsx-a11y/no-onchange */
27
24
  const react_1 = __importStar(require("react"));
28
25
  const images_1 = require("./images");
29
26
  const common_1 = require("./common");
30
- const styled_components_1 = __importDefault(require("styled-components"));
27
+ const styled_components_1 = __importStar(require("styled-components"));
31
28
  const useOnClickOutside_1 = require("../../helpers/useOnClickOutside");
32
29
  const common_2 = require("../../styles/common");
33
30
  exports.ValueReadonly = styled_components_1.default.div `
@@ -35,17 +32,24 @@ exports.ValueReadonly = styled_components_1.default.div `
35
32
  word-break: break-word;
36
33
  flex-basis: calc(100% - 3rem);
37
34
  `;
38
- const ValueTextArea = styled_components_1.default.textarea `
35
+ const basecss = (0, styled_components_1.css) `
36
+ outline: none;
39
37
  border: 0;
40
38
  word-break: break-word;
41
39
  ${common_1.valueCss};
42
40
  resize: none;
43
41
  overflow: hidden;
44
42
  background-color: white;
43
+ `;
44
+ const ValueTextArea = styled_components_1.default.textarea `
45
+ ${basecss}
45
46
  &[data-editing='true'] {
46
47
  min-height: 5rem;
47
48
  }
48
49
  `;
50
+ const ValueTextBox = styled_components_1.default.input `
51
+ ${basecss}
52
+ `;
49
53
  const ValueBoxEdit = (0, styled_components_1.default)(common_1.ValueBox) `
50
54
  border: solid 1px #ccc;
51
55
  `;
@@ -61,18 +65,18 @@ const Icon = styled_components_1.default.div `
61
65
  filter: saturate(3);
62
66
  }
63
67
  `;
64
- const TextEdit = ({ defaultValue, defaultEditing = false, onSubmit, canEdit = true, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine = false, }) => {
68
+ const TextEdit = ({ defaultValue, defaultEditing, onSubmit, disableEdit = false, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine = false, }) => {
65
69
  const ref = (0, react_1.useRef)(null);
66
70
  const taref = (0, react_1.useRef)(null);
67
71
  const [value, setValue] = (0, react_1.useState)(defaultValue);
68
- const [editing, setEditingRaw] = (0, react_1.useState)(defaultEditing);
72
+ const [editing, setEditingRaw] = (0, react_1.useState)(!!defaultEditing);
69
73
  const valueChange = value !== defaultValue;
70
74
  (0, useOnClickOutside_1.useOnClickOutside)({ ref, moveMouseOutside: false }, () => {
71
75
  if (valueChange) {
72
76
  onSubmit(value);
73
77
  }
74
78
  else {
75
- if (canEdit && editing && defaultEditing) {
79
+ if (!disableEdit && editing && defaultEditing) {
76
80
  return;
77
81
  }
78
82
  if (onClickOutsideWithNoValue) {
@@ -89,20 +93,19 @@ const TextEdit = ({ defaultValue, defaultEditing = false, onSubmit, canEdit = tr
89
93
  };
90
94
  (0, react_1.useEffect)(() => {
91
95
  setValue(defaultValue);
92
- //setEditing(false);
93
96
  // eslint-disable-next-line react-hooks/exhaustive-deps
94
97
  }, [defaultValue]);
95
98
  (0, react_1.useEffect)(() => {
96
- if (defaultEditing && taref.current) {
99
+ if ((defaultEditing === null || defaultEditing === void 0 ? void 0 : defaultEditing.focus) && taref.current) {
97
100
  taref.current.focus();
98
101
  }
99
- setEditing(defaultEditing);
102
+ setEditing(!!defaultEditing);
100
103
  // eslint-disable-next-line react-hooks/exhaustive-deps
101
104
  }, [defaultEditing]);
102
- if (!editing || !canEdit) {
105
+ if (!editing || disableEdit) {
103
106
  return (react_1.default.createElement(common_1.ValueBox, Object.assign({}, common_2.noDrag, { className: className, "data-editing": "false", onClick: () => onClickNotEditing === null || onClickNotEditing === void 0 ? void 0 : onClickNotEditing(), "data-pointer": onClickNotEditing ? 'true' : 'false' }),
104
107
  react_1.default.createElement(exports.ValueReadonly, { "data-type": "text" }, value),
105
- react_1.default.createElement(Right, null, canEdit && (react_1.default.createElement(Icon, { style: common_1.iconRight, onClick: (e) => {
108
+ react_1.default.createElement(Right, null, !disableEdit && (react_1.default.createElement(Icon, { style: common_1.iconRight, onClick: (e) => {
106
109
  e.stopPropagation();
107
110
  setEditing(true);
108
111
  } },
@@ -111,13 +114,20 @@ const TextEdit = ({ defaultValue, defaultEditing = false, onSubmit, canEdit = tr
111
114
  if (!open) {
112
115
  return react_1.default.createElement(react_1.default.Fragment, null);
113
116
  }
114
- return (react_1.default.createElement(ValueBoxEdit, Object.assign({ "data-editing": "true" }, common_2.noDrag, { ref: ref, tabIndex: editing ? 0 : undefined, className: className }),
115
- react_1.default.createElement(ValueTextArea, { "data-editing": "true", "data-valuechange": valueChange.toString(), ref: taref, "data-type": "text", value: value, onChange: (v) => setValue(v.currentTarget.value), placeholder: placeholder, rows: singleLine ? 1 : undefined, onKeyDown: (e) => singleLine && e.code.endsWith('Enter') && onSubmit(value) && false }),
117
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
118
+ const Comp = singleLine
119
+ ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
+ ValueTextBox
121
+ : ValueTextArea;
122
+ return (react_1.default.createElement(ValueBoxEdit, Object.assign({ "data-editing": "true" }, common_2.noDrag, {
123
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
+ ref: ref, tabIndex: editing ? 0 : undefined, className: className }),
125
+ react_1.default.createElement(Comp, { "data-editing": "true", "data-valuechange": valueChange.toString(), ref: taref, "data-type": "text", value: value, onChange: (v) => setValue(v.currentTarget.value), placeholder: placeholder, rows: singleLine ? 1 : undefined, onKeyDown: (e) => singleLine && e.code.endsWith('Enter') && onSubmit(value) && false }),
116
126
  react_1.default.createElement(Right, null,
117
127
  valueChange && (react_1.default.createElement(Icon, { style: common_1.iconLeft, onClick: () => valueChange && onSubmit(value) },
118
128
  react_1.default.createElement(images_1.SaveIcon, null))),
119
- (valueChange || editing !== defaultEditing) && (react_1.default.createElement(Icon, { style: common_1.iconRight, onClick: () => {
120
- setEditing(defaultEditing);
129
+ (valueChange || editing !== !!defaultEditing) && (react_1.default.createElement(Icon, { style: common_1.iconRight, onClick: () => {
130
+ setEditing(!!defaultEditing);
121
131
  setValue(defaultValue);
122
132
  } },
123
133
  react_1.default.createElement(images_1.UndoIcon, null))))));
@@ -44,7 +44,6 @@ exports.valueCss = (0, styled_components_1.css) `
44
44
  width: 100%;
45
45
  height: 100%;
46
46
  word-break: break-all;
47
- font-size: 1rem;
48
47
 
49
48
  &[data-type='checkbox'] {
50
49
  cursor: pointer;
@@ -13,6 +13,7 @@ export * from './mutex';
13
13
  export * from './mutexData';
14
14
  export * from './plural';
15
15
  export * from './routes';
16
+ export * from './useContextMenu';
16
17
  export * from './useLocalStorage';
17
18
  export * from './useLockBodyScroll';
18
19
  export * from './useOnClickOutside';
@@ -25,6 +25,7 @@ __exportStar(require("./mutex"), exports);
25
25
  __exportStar(require("./mutexData"), exports);
26
26
  __exportStar(require("./plural"), exports);
27
27
  __exportStar(require("./routes"), exports);
28
+ __exportStar(require("./useContextMenu"), exports);
28
29
  __exportStar(require("./useLocalStorage"), exports);
29
30
  __exportStar(require("./useLockBodyScroll"), exports);
30
31
  __exportStar(require("./useOnClickOutside"), exports);
@@ -0,0 +1,7 @@
1
+ export declare const useContextMenu: ({ ref, }: {
2
+ ref: React.RefObject<HTMLDivElement>;
3
+ }) => {
4
+ xPos: string;
5
+ yPos: string;
6
+ showMenu: boolean;
7
+ };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useContextMenu = void 0;
4
+ const react_1 = require("react");
5
+ const useContextMenu = ({ ref, }) => {
6
+ const [xPos, setXPos] = (0, react_1.useState)('0px');
7
+ const [yPos, setYPos] = (0, react_1.useState)('0px');
8
+ const [showMenu, setShowMenu] = (0, react_1.useState)(false);
9
+ const handleContextMenu = (0, react_1.useCallback)((e) => {
10
+ var _a;
11
+ if (!((_a = ref.current) === null || _a === void 0 ? void 0 : _a.contains(e.target))) {
12
+ return;
13
+ }
14
+ e.preventDefault();
15
+ setXPos(`${e.pageX}px`);
16
+ setYPos(`${e.pageY}px`);
17
+ setShowMenu(true);
18
+ }, [ref]);
19
+ const handleClick = (0, react_1.useCallback)(() => {
20
+ if (!showMenu) {
21
+ return;
22
+ }
23
+ setShowMenu(false);
24
+ }, [showMenu]);
25
+ (0, react_1.useEffect)(() => {
26
+ document.addEventListener('click', handleClick);
27
+ document.addEventListener('contextmenu', handleContextMenu);
28
+ return () => {
29
+ document.addEventListener('click', handleClick);
30
+ document.removeEventListener('contextmenu', handleContextMenu);
31
+ };
32
+ });
33
+ return { xPos, yPos, showMenu };
34
+ };
35
+ exports.useContextMenu = useContextMenu;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.65",
3
+ "version": "0.0.66",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",