@next-shared/form 0.8.4 → 0.9.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/cjs/TextareaAutoResize.js +18 -2
- package/dist/cjs/TextareaAutoResize.js.map +1 -1
- package/dist/cjs/getCaretPositionInTextarea.js +28 -0
- package/dist/cjs/getCaretPositionInTextarea.js.map +1 -0
- package/dist/cjs/getContentRectInTextarea.js +25 -0
- package/dist/cjs/getContentRectInTextarea.js.map +1 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/calculateAutoSizeStyle.js +2 -16
- package/dist/cjs/utils/calculateAutoSizeStyle.js.map +1 -1
- package/dist/cjs/utils/getMirroredStyle.js +29 -0
- package/dist/cjs/utils/getMirroredStyle.js.map +1 -0
- package/dist/esm/TextareaAutoResize.js +19 -3
- package/dist/esm/TextareaAutoResize.js.map +1 -1
- package/dist/esm/getCaretPositionInTextarea.js +22 -0
- package/dist/esm/getCaretPositionInTextarea.js.map +1 -0
- package/dist/esm/getContentRectInTextarea.js +19 -0
- package/dist/esm/getContentRectInTextarea.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/calculateAutoSizeStyle.js +2 -16
- package/dist/esm/utils/calculateAutoSizeStyle.js.map +1 -1
- package/dist/esm/utils/getMirroredStyle.js +23 -0
- package/dist/esm/utils/getMirroredStyle.js.map +1 -0
- package/dist/types/TextareaAutoResize.d.ts +10 -0
- package/dist/types/getCaretPositionInTextarea.d.ts +4 -0
- package/dist/types/getContentRectInTextarea.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/utils/getMirroredStyle.d.ts +4 -0
- package/package.json +2 -2
|
@@ -24,6 +24,7 @@ function LegacyTextareaAutoResize({
|
|
|
24
24
|
value: propValue,
|
|
25
25
|
style,
|
|
26
26
|
submitWhen,
|
|
27
|
+
desiredSelectionRef,
|
|
27
28
|
onChange,
|
|
28
29
|
onSubmit,
|
|
29
30
|
onKeyDown,
|
|
@@ -45,7 +46,8 @@ function LegacyTextareaAutoResize({
|
|
|
45
46
|
textarea.focus();
|
|
46
47
|
valueLength && textarea.setSelectionRange(valueLength, valueLength);
|
|
47
48
|
}
|
|
48
|
-
}
|
|
49
|
+
},
|
|
50
|
+
element: textareaRef.current
|
|
49
51
|
}), []);
|
|
50
52
|
const doAutoResize = (0, _react.useCallback)(() => {
|
|
51
53
|
const textarea = textareaRef.current;
|
|
@@ -90,13 +92,27 @@ function LegacyTextareaAutoResize({
|
|
|
90
92
|
// Ignore key events during composition
|
|
91
93
|
return;
|
|
92
94
|
}
|
|
95
|
+
if ((onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(e)) === false) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
93
98
|
if (e.key === "Enter" && (submitWhen === "enter-without-shift" ? !e.shiftKey : submitWhen === "enter-with-mod" && e[modKey])) {
|
|
94
99
|
e.preventDefault();
|
|
95
100
|
e.stopPropagation();
|
|
96
101
|
onSubmit === null || onSubmit === void 0 || onSubmit(e);
|
|
97
102
|
}
|
|
98
|
-
onKeyDown === null || onKeyDown === void 0 || onKeyDown(e);
|
|
99
103
|
}, [onKeyDown, onSubmit, submitWhen]);
|
|
104
|
+
(0, _react.useLayoutEffect)(() => {
|
|
105
|
+
const desiredSelection = desiredSelectionRef === null || desiredSelectionRef === void 0 ? void 0 : desiredSelectionRef.current;
|
|
106
|
+
const textarea = textareaRef.current;
|
|
107
|
+
if (desiredSelection && textarea) {
|
|
108
|
+
const {
|
|
109
|
+
start,
|
|
110
|
+
end
|
|
111
|
+
} = desiredSelection;
|
|
112
|
+
desiredSelectionRef.current = null;
|
|
113
|
+
textarea.setSelectionRange(start, end);
|
|
114
|
+
}
|
|
115
|
+
}, [desiredSelectionRef, value]);
|
|
100
116
|
|
|
101
117
|
// istanbul ignore next
|
|
102
118
|
(0, _react.useEffect)(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TextareaAutoResize.js","names":["_react","_interopRequireWildcard","require","_reactDom","_resizeObserverPolyfill","_interopRequireDefault","_calculateAutoSizeStyle","modKey","test","navigator","platform","TextareaAutoResize","exports","React","forwardRef","LegacyTextareaAutoResize","autoResize","_autoResize","minRows","maxRows","borderSize","paddingSize","containerRef","value","propValue","style","submitWhen","onChange","onSubmit","onKeyDown","onCompositionStart","onCompositionEnd","props","ref","setValue","useState","textareaRef","useRef","autoStyle","setAutoStyle","useImperativeHandle","focus","textarea","current","_textarea$value","valueLength","length","setSelectionRange","doAutoResize","useCallback","calculateAutoSizeStyle","process","env","NODE_ENV","flushSync","handleInputChange","e","target","useEffect","compositionRef","handleCompositionStart","handleCompositionEnd","handleKeyDown","key","shiftKey","preventDefault","stopPropagation","container","previousInlineSize","observer","ResizeObserver","entries","entry","currentInlineSize","contentBoxSize","inlineSize","contentRect","width","undefined","isInitial","requestAnimationFrame","observe","disconnect","default","createElement","_extends2"],"sources":["../../src/TextareaAutoResize.tsx"],"sourcesContent":["import React, {\n useCallback,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\nimport { flushSync } from \"react-dom\";\nimport ResizeObserver from \"resize-observer-polyfill\";\nimport calculateAutoSizeStyle from \"./utils/calculateAutoSizeStyle.js\";\n\n// istanbul ignore next\nconst modKey = /Mac|iPod|iPhone|iPad/.test(navigator.platform)\n ? \"metaKey\"\n : \"ctrlKey\";\n\nexport interface TextareaAutoResizeProps\n extends React.DetailedHTMLProps<\n React.TextareaHTMLAttributes<HTMLTextAreaElement>,\n HTMLTextAreaElement\n > {\n /** @default true */\n autoResize?: boolean;\n minRows?: number | null;\n maxRows?: number | null;\n /** @default 2 */\n borderSize?: number;\n /** @default 8 */\n paddingSize?: number;\n containerRef?: React.RefObject<HTMLElement>;\n submitWhen?: \"enter-without-shift\" | \"enter-with-mod\";\n}\n\nexport interface TextareaAutoResizeRef {\n focus(): void;\n}\n\nexport const TextareaAutoResize = React.forwardRef<\n TextareaAutoResizeRef,\n TextareaAutoResizeProps\n>(LegacyTextareaAutoResize);\n\nfunction LegacyTextareaAutoResize(\n {\n autoResize: _autoResize,\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n containerRef,\n value: propValue,\n style,\n submitWhen,\n onChange,\n onSubmit,\n onKeyDown,\n onCompositionStart,\n onCompositionEnd,\n ...props\n }: TextareaAutoResizeProps,\n ref: React.ForwardedRef<TextareaAutoResizeRef>\n): React.JSX.Element {\n const autoResize = _autoResize ?? true;\n const [value, setValue] = useState(propValue ?? \"\");\n const textareaRef = useRef<HTMLTextAreaElement>(null);\n const [autoStyle, setAutoStyle] = useState<React.CSSProperties | null>(null);\n\n useImperativeHandle(\n ref,\n () => ({\n focus: () => {\n const textarea = textareaRef.current;\n // istanbul ignore else: defensive check\n if (textarea) {\n const valueLength = textarea.value?.length;\n textarea.focus();\n valueLength && textarea.setSelectionRange(valueLength, valueLength);\n }\n },\n }),\n []\n );\n\n const doAutoResize = useCallback(() => {\n const textarea = textareaRef.current;\n if (textarea && autoResize) {\n const style = calculateAutoSizeStyle(textarea, {\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n });\n // istanbul ignore next\n if (process.env.NODE_ENV === \"test\") {\n setAutoStyle(style);\n } else {\n flushSync(() => {\n setAutoStyle(style);\n });\n }\n }\n }, [autoResize, maxRows, minRows, borderSize, paddingSize]);\n\n const handleInputChange = (\n e: React.ChangeEvent<HTMLTextAreaElement>\n ): void => {\n setValue(e.target.value);\n onChange?.(e);\n };\n\n useEffect(() => {\n setValue(propValue ?? \"\");\n }, [propValue]);\n\n useEffect(() => {\n doAutoResize();\n }, [doAutoResize, value]);\n\n const compositionRef = useRef(false);\n\n const handleCompositionStart = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = true;\n onCompositionStart?.(e);\n },\n [onCompositionStart]\n );\n\n const handleCompositionEnd = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = false;\n onCompositionEnd?.(e);\n },\n [onCompositionEnd]\n );\n\n const handleKeyDown = useCallback(\n (e: React.KeyboardEvent<HTMLTextAreaElement>) => {\n if (compositionRef.current) {\n // Ignore key events during composition\n return;\n }\n\n if (\n e.key === \"Enter\" &&\n (submitWhen === \"enter-without-shift\"\n ? !e.shiftKey\n : submitWhen === \"enter-with-mod\" && e[modKey])\n ) {\n e.preventDefault();\n e.stopPropagation();\n onSubmit?.(e);\n }\n\n onKeyDown?.(e);\n },\n [onKeyDown, onSubmit, submitWhen]\n );\n\n // istanbul ignore next\n useEffect(() => {\n const container = containerRef?.current;\n if (!container || !autoResize) {\n return;\n }\n let previousInlineSize: number | undefined;\n const observer = new ResizeObserver((entries) => {\n for (const entry of entries) {\n if (entry.target === container) {\n // istanbul ignore next: compatibility\n const currentInlineSize = entry.contentBoxSize\n ? entry.contentBoxSize[0]\n ? entry.contentBoxSize[0].inlineSize\n : (entry.contentBoxSize as unknown as ResizeObserverSize)\n .inlineSize\n : entry.contentRect.width;\n if (\n currentInlineSize !== undefined &&\n currentInlineSize !== previousInlineSize\n ) {\n const isInitial = !previousInlineSize;\n previousInlineSize = currentInlineSize;\n if (!isInitial) {\n requestAnimationFrame(doAutoResize);\n }\n }\n }\n }\n });\n observer.observe(container);\n return () => {\n observer.disconnect();\n };\n }, [autoResize, containerRef, doAutoResize]);\n\n return (\n <textarea\n {...props}\n ref={textareaRef}\n value={value}\n style={{\n ...style,\n ...autoStyle,\n }}\n onChange={handleInputChange}\n onCompositionStart={handleCompositionStart}\n onCompositionEnd={handleCompositionEnd}\n onKeyDown={handleKeyDown}\n />\n );\n}\n"],"mappings":";;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAOA,IAAAC,SAAA,GAAAD,OAAA;AACA,IAAAE,uBAAA,GAAAC,sBAAA,CAAAH,OAAA;AACA,IAAAI,uBAAA,GAAAD,sBAAA,CAAAH,OAAA;AAE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ACrBF,KAAK,CAACE,cAAc,CAAC,CAAC,CAAC,CAACC,UAAU,GACjCH,KAAK,CAACE,cAAc,CAClBC,UAAU,GACfH,KAAK,CAACI,WAAW,CAACC,KAAK;UAC3B,IACEJ,iBAAiB,KAAKK,SAAS,IAC/BL,iBAAiB,KAAKL,kBAAkB,EACxC;YACA,MAAMW,SAAS,GAAG,CAACX,kBAAkB;YACrCA,kBAAkB,GAAGK,iBAAiB;YACtC,IAAI,CAACM,SAAS,EAAE;cACdC,qBAAqB,CAAChC,YAAY,CAAC;YACrC;UACF;QACF;MACF;IACF,CAAC,CAAC;IACFqB,QAAQ,CAACY,OAAO,CAACd,SAAS,CAAC;IAC3B,OAAO,MAAM;MACXE,QAAQ,CAACa,UAAU,CAAC,CAAC;IACvB,CAAC;EACH,CAAC,EAAE,CAAClE,UAAU,EAAEM,YAAY,EAAE0B,YAAY,CAAC,CAAC;EAE5C,oBACEhD,MAAA,CAAAmF,OAAA,CAAAC,aAAA,iBAAAC,SAAA,CAAAF,OAAA,MACMnD,KAAK;IACTC,GAAG,EAAEG,WAAY;IACjBb,KAAK,EAAEA,KAAM;IACbE,KAAK,EAAE;MACL,GAAGA,KAAK;MACR,GAAGa;IACL,CAAE;IACFX,QAAQ,EAAE4B,iBAAkB;IAC5BzB,kBAAkB,EAAE8B,sBAAuB;IAC3C7B,gBAAgB,EAAE8B,oBAAqB;IACvChC,SAAS,EAAEiC;EAAc,EAC1B,CAAC;AAEN","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"TextareaAutoResize.js","names":["_react","_interopRequireWildcard","require","_reactDom","_resizeObserverPolyfill","_interopRequireDefault","_calculateAutoSizeStyle","modKey","test","navigator","platform","TextareaAutoResize","exports","React","forwardRef","LegacyTextareaAutoResize","autoResize","_autoResize","minRows","maxRows","borderSize","paddingSize","containerRef","value","propValue","style","submitWhen","desiredSelectionRef","onChange","onSubmit","onKeyDown","onCompositionStart","onCompositionEnd","props","ref","setValue","useState","textareaRef","useRef","autoStyle","setAutoStyle","useImperativeHandle","focus","textarea","current","_textarea$value","valueLength","length","setSelectionRange","element","doAutoResize","useCallback","calculateAutoSizeStyle","process","env","NODE_ENV","flushSync","handleInputChange","e","target","useEffect","compositionRef","handleCompositionStart","handleCompositionEnd","handleKeyDown","key","shiftKey","preventDefault","stopPropagation","useLayoutEffect","desiredSelection","start","end","container","previousInlineSize","observer","ResizeObserver","entries","entry","currentInlineSize","contentBoxSize","inlineSize","contentRect","width","undefined","isInitial","requestAnimationFrame","observe","disconnect","default","createElement","_extends2"],"sources":["../../src/TextareaAutoResize.tsx"],"sourcesContent":["import React, {\n useCallback,\n useEffect,\n useImperativeHandle,\n useLayoutEffect,\n useRef,\n useState,\n} from \"react\";\nimport { flushSync } from \"react-dom\";\nimport ResizeObserver from \"resize-observer-polyfill\";\nimport calculateAutoSizeStyle from \"./utils/calculateAutoSizeStyle.js\";\n\n// istanbul ignore next\nconst modKey = /Mac|iPod|iPhone|iPad/.test(navigator.platform)\n ? \"metaKey\"\n : \"ctrlKey\";\n\nexport interface TextareaAutoResizeProps\n extends React.DetailedHTMLProps<\n React.TextareaHTMLAttributes<HTMLTextAreaElement>,\n HTMLTextAreaElement\n > {\n /** @default true */\n autoResize?: boolean;\n minRows?: number | null;\n maxRows?: number | null;\n /** @default 2 */\n borderSize?: number;\n /** @default 8 */\n paddingSize?: number;\n containerRef?: React.RefObject<HTMLElement>;\n submitWhen?: \"enter-without-shift\" | \"enter-with-mod\";\n desiredSelectionRef?: React.MutableRefObject<{\n start: number;\n end: number;\n } | null>;\n onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;\n /**\n * The handler can return false to prevent default behavior (submit)\n */\n onKeyDown?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void | false;\n}\n\nexport interface TextareaAutoResizeRef {\n focus(): void;\n element: HTMLTextAreaElement | null;\n}\n\nexport const TextareaAutoResize = React.forwardRef<\n TextareaAutoResizeRef,\n TextareaAutoResizeProps\n>(LegacyTextareaAutoResize);\n\nfunction LegacyTextareaAutoResize(\n {\n autoResize: _autoResize,\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n containerRef,\n value: propValue,\n style,\n submitWhen,\n desiredSelectionRef,\n onChange,\n onSubmit,\n onKeyDown,\n onCompositionStart,\n onCompositionEnd,\n ...props\n }: TextareaAutoResizeProps,\n ref: React.ForwardedRef<TextareaAutoResizeRef>\n): React.JSX.Element {\n const autoResize = _autoResize ?? true;\n const [value, setValue] = useState(propValue ?? \"\");\n const textareaRef = useRef<HTMLTextAreaElement>(null);\n const [autoStyle, setAutoStyle] = useState<React.CSSProperties | null>(null);\n\n useImperativeHandle(\n ref,\n () => ({\n focus: () => {\n const textarea = textareaRef.current;\n // istanbul ignore else: defensive check\n if (textarea) {\n const valueLength = textarea.value?.length;\n textarea.focus();\n valueLength && textarea.setSelectionRange(valueLength, valueLength);\n }\n },\n element: textareaRef.current,\n }),\n []\n );\n\n const doAutoResize = useCallback(() => {\n const textarea = textareaRef.current;\n if (textarea && autoResize) {\n const style = calculateAutoSizeStyle(textarea, {\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n });\n // istanbul ignore next\n if (process.env.NODE_ENV === \"test\") {\n setAutoStyle(style);\n } else {\n flushSync(() => {\n setAutoStyle(style);\n });\n }\n }\n }, [autoResize, maxRows, minRows, borderSize, paddingSize]);\n\n const handleInputChange = (\n e: React.ChangeEvent<HTMLTextAreaElement>\n ): void => {\n setValue(e.target.value);\n onChange?.(e);\n };\n\n useEffect(() => {\n setValue(propValue ?? \"\");\n }, [propValue]);\n\n useEffect(() => {\n doAutoResize();\n }, [doAutoResize, value]);\n\n const compositionRef = useRef(false);\n\n const handleCompositionStart = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = true;\n onCompositionStart?.(e);\n },\n [onCompositionStart]\n );\n\n const handleCompositionEnd = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = false;\n onCompositionEnd?.(e);\n },\n [onCompositionEnd]\n );\n\n const handleKeyDown = useCallback(\n (e: React.KeyboardEvent<HTMLTextAreaElement>) => {\n if (compositionRef.current) {\n // Ignore key events during composition\n return;\n }\n\n if (onKeyDown?.(e) === false) {\n return;\n }\n\n if (\n e.key === \"Enter\" &&\n (submitWhen === \"enter-without-shift\"\n ? !e.shiftKey\n : submitWhen === \"enter-with-mod\" && e[modKey])\n ) {\n e.preventDefault();\n e.stopPropagation();\n onSubmit?.(e);\n }\n },\n [onKeyDown, onSubmit, submitWhen]\n );\n\n useLayoutEffect(() => {\n const desiredSelection = desiredSelectionRef?.current;\n const textarea = textareaRef.current;\n if (desiredSelection && textarea) {\n const { start, end } = desiredSelection;\n desiredSelectionRef.current = null;\n textarea.setSelectionRange(start, end);\n }\n }, [desiredSelectionRef, value]);\n\n // istanbul ignore next\n useEffect(() => {\n const container = containerRef?.current;\n if (!container || !autoResize) {\n return;\n }\n let previousInlineSize: number | undefined;\n const observer = new ResizeObserver((entries) => {\n for (const entry of entries) {\n if (entry.target === container) {\n // istanbul ignore next: compatibility\n const currentInlineSize = entry.contentBoxSize\n ? entry.contentBoxSize[0]\n ? entry.contentBoxSize[0].inlineSize\n : (entry.contentBoxSize as unknown as ResizeObserverSize)\n .inlineSize\n : entry.contentRect.width;\n if (\n currentInlineSize !== undefined &&\n currentInlineSize !== previousInlineSize\n ) {\n const isInitial = !previousInlineSize;\n previousInlineSize = currentInlineSize;\n if (!isInitial) {\n requestAnimationFrame(doAutoResize);\n }\n }\n }\n }\n });\n observer.observe(container);\n return () => {\n observer.disconnect();\n };\n }, [autoResize, containerRef, doAutoResize]);\n\n return (\n <textarea\n {...props}\n ref={textareaRef}\n value={value}\n style={{\n ...style,\n ...autoStyle,\n }}\n onChange={handleInputChange}\n onCompositionStart={handleCompositionStart}\n onCompositionEnd={handleCompositionEnd}\n onKeyDown={handleKeyDown}\n />\n );\n}\n"],"mappings":";;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,O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AAC;EAE5C,oBACElD,MAAA,CAAAyF,OAAA,CAAAC,aAAA,iBAAAC,SAAA,CAAAF,OAAA,MACMxD,KAAK;IACTC,GAAG,EAAEG,WAAY;IACjBd,KAAK,EAAEA,KAAM;IACbE,KAAK,EAAE;MACL,GAAGA,KAAK;MACR,GAAGc;IACL,CAAE;IACFX,QAAQ,EAAE6B,iBAAkB;IAC5B1B,kBAAkB,EAAE+B,sBAAuB;IAC3C9B,gBAAgB,EAAE+B,oBAAqB;IACvCjC,SAAS,EAAEkC;EAAc,EAC1B,CAAC;AAEN","ignoreList":[]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getCaretPositionInTextarea = getCaretPositionInTextarea;
|
|
7
|
+
var _getMirroredStyle = require("./utils/getMirroredStyle.js");
|
|
8
|
+
let hiddenElement;
|
|
9
|
+
function getCaretPositionInTextarea(textarea, index) {
|
|
10
|
+
if (!hiddenElement) {
|
|
11
|
+
hiddenElement = document.createElement("div");
|
|
12
|
+
hiddenElement.setAttribute("tab-index", "-1");
|
|
13
|
+
hiddenElement.setAttribute("aria-hidden", "true");
|
|
14
|
+
document.body.appendChild(hiddenElement);
|
|
15
|
+
}
|
|
16
|
+
hiddenElement.setAttribute("style", (0, _getMirroredStyle.getMirroredStyle)(textarea));
|
|
17
|
+
hiddenElement.textContent = textarea.value.slice(0, index);
|
|
18
|
+
const span = document.createElement("span");
|
|
19
|
+
span.textContent = "\u200b"; // 占位符
|
|
20
|
+
hiddenElement.appendChild(span);
|
|
21
|
+
const rect = span.getBoundingClientRect();
|
|
22
|
+
span.remove();
|
|
23
|
+
return {
|
|
24
|
+
top: rect.top,
|
|
25
|
+
left: rect.left
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=getCaretPositionInTextarea.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCaretPositionInTextarea.js","names":["_getMirroredStyle","require","hiddenElement","getCaretPositionInTextarea","textarea","index","document","createElement","setAttribute","body","appendChild","getMirroredStyle","textContent","value","slice","span","rect","getBoundingClientRect","remove","top","left"],"sources":["../../src/getCaretPositionInTextarea.ts"],"sourcesContent":["import { getMirroredStyle } from \"./utils/getMirroredStyle.js\";\n\nlet hiddenElement: HTMLDivElement | undefined;\n\nexport function getCaretPositionInTextarea(\n textarea: HTMLTextAreaElement,\n index: number\n) {\n if (!hiddenElement) {\n hiddenElement = document.createElement(\"div\");\n hiddenElement.setAttribute(\"tab-index\", \"-1\");\n hiddenElement.setAttribute(\"aria-hidden\", \"true\");\n document.body.appendChild(hiddenElement);\n }\n\n hiddenElement.setAttribute(\"style\", getMirroredStyle(textarea));\n hiddenElement.textContent = textarea.value.slice(0, index);\n\n const span = document.createElement(\"span\");\n span.textContent = \"\\u200b\"; // 占位符\n hiddenElement.appendChild(span);\n\n const rect = span.getBoundingClientRect();\n span.remove();\n return {\n top: rect.top,\n left: rect.left,\n };\n}\n"],"mappings":";;;;;;AAAA,IAAAA,iBAAA,GAAAC,OAAA;AAEA,IAAIC,aAAyC;AAEtC,SAASC,0BAA0BA,CACxCC,QAA6B,EAC7BC,KAAa,EACb;EACA,IAAI,CAACH,aAAa,EAAE;IAClBA,aAAa,GAAGI,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;IAC7CL,aAAa,CAACM,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC;IAC7CN,aAAa,CAACM,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC;IACjDF,QAAQ,CAACG,IAAI,CAACC,WAAW,CAACR,aAAa,CAAC;EAC1C;EAEAA,aAAa,CAACM,YAAY,CAAC,OAAO,EAAE,IAAAG,kCAAgB,EAACP,QAAQ,CAAC,CAAC;EAC/DF,aAAa,CAACU,WAAW,GAAGR,QAAQ,CAACS,KAAK,CAACC,KAAK,CAAC,CAAC,EAAET,KAAK,CAAC;EAE1D,MAAMU,IAAI,GAAGT,QAAQ,CAACC,aAAa,CAAC,MAAM,CAAC;EAC3CQ,IAAI,CAACH,WAAW,GAAG,QAAQ,CAAC,CAAC;EAC7BV,aAAa,CAACQ,WAAW,CAACK,IAAI,CAAC;EAE/B,MAAMC,IAAI,GAAGD,IAAI,CAACE,qBAAqB,CAAC,CAAC;EACzCF,IAAI,CAACG,MAAM,CAAC,CAAC;EACb,OAAO;IACLC,GAAG,EAAEH,IAAI,CAACG,GAAG;IACbC,IAAI,EAAEJ,IAAI,CAACI;EACb,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getContentRectInTextarea = getContentRectInTextarea;
|
|
7
|
+
var _getMirroredStyle = require("./utils/getMirroredStyle.js");
|
|
8
|
+
let hiddenElement;
|
|
9
|
+
function getContentRectInTextarea(textarea, prefix, measureContent) {
|
|
10
|
+
if (!hiddenElement) {
|
|
11
|
+
hiddenElement = document.createElement("div");
|
|
12
|
+
hiddenElement.setAttribute("tab-index", "-1");
|
|
13
|
+
hiddenElement.setAttribute("aria-hidden", "true");
|
|
14
|
+
document.body.appendChild(hiddenElement);
|
|
15
|
+
}
|
|
16
|
+
hiddenElement.setAttribute("style", (0, _getMirroredStyle.getMirroredStyle)(textarea));
|
|
17
|
+
hiddenElement.textContent = prefix;
|
|
18
|
+
const span = document.createElement("span");
|
|
19
|
+
span.textContent = measureContent;
|
|
20
|
+
hiddenElement.appendChild(span);
|
|
21
|
+
const rect = span.getBoundingClientRect();
|
|
22
|
+
span.remove();
|
|
23
|
+
return rect;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=getContentRectInTextarea.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getContentRectInTextarea.js","names":["_getMirroredStyle","require","hiddenElement","getContentRectInTextarea","textarea","prefix","measureContent","document","createElement","setAttribute","body","appendChild","getMirroredStyle","textContent","span","rect","getBoundingClientRect","remove"],"sources":["../../src/getContentRectInTextarea.ts"],"sourcesContent":["import { getMirroredStyle } from \"./utils/getMirroredStyle.js\";\n\nlet hiddenElement: HTMLDivElement | undefined;\n\nexport function getContentRectInTextarea(\n textarea: HTMLTextAreaElement,\n prefix: string,\n measureContent: string\n) {\n if (!hiddenElement) {\n hiddenElement = document.createElement(\"div\");\n hiddenElement.setAttribute(\"tab-index\", \"-1\");\n hiddenElement.setAttribute(\"aria-hidden\", \"true\");\n document.body.appendChild(hiddenElement);\n }\n hiddenElement.setAttribute(\"style\", getMirroredStyle(textarea));\n\n hiddenElement.textContent = prefix;\n\n const span = document.createElement(\"span\");\n span.textContent = measureContent;\n hiddenElement.appendChild(span);\n\n const rect = span.getBoundingClientRect();\n span.remove();\n return rect;\n}\n"],"mappings":";;;;;;AAAA,IAAAA,iBAAA,GAAAC,OAAA;AAEA,IAAIC,aAAyC;AAEtC,SAASC,wBAAwBA,CACtCC,QAA6B,EAC7BC,MAAc,EACdC,cAAsB,EACtB;EACA,IAAI,CAACJ,aAAa,EAAE;IAClBA,aAAa,GAAGK,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;IAC7CN,aAAa,CAACO,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC;IAC7CP,aAAa,CAACO,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC;IACjDF,QAAQ,CAACG,IAAI,CAACC,WAAW,CAACT,aAAa,CAAC;EAC1C;EACAA,aAAa,CAACO,YAAY,CAAC,OAAO,EAAE,IAAAG,kCAAgB,EAACR,QAAQ,CAAC,CAAC;EAE/DF,aAAa,CAACW,WAAW,GAAGR,MAAM;EAElC,MAAMS,IAAI,GAAGP,QAAQ,CAACC,aAAa,CAAC,MAAM,CAAC;EAC3CM,IAAI,CAACD,WAAW,GAAGP,cAAc;EACjCJ,aAAa,CAACS,WAAW,CAACG,IAAI,CAAC;EAE/B,MAAMC,IAAI,GAAGD,IAAI,CAACE,qBAAqB,CAAC,CAAC;EACzCF,IAAI,CAACG,MAAM,CAAC,CAAC;EACb,OAAOF,IAAI;AACb","ignoreList":[]}
|
package/dist/cjs/index.js
CHANGED
|
@@ -69,4 +69,26 @@ Object.keys(_TextareaAutoResize).forEach(function (key) {
|
|
|
69
69
|
}
|
|
70
70
|
});
|
|
71
71
|
});
|
|
72
|
+
var _getCaretPositionInTextarea = require("./getCaretPositionInTextarea.js");
|
|
73
|
+
Object.keys(_getCaretPositionInTextarea).forEach(function (key) {
|
|
74
|
+
if (key === "default" || key === "__esModule") return;
|
|
75
|
+
if (key in exports && exports[key] === _getCaretPositionInTextarea[key]) return;
|
|
76
|
+
Object.defineProperty(exports, key, {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
get: function () {
|
|
79
|
+
return _getCaretPositionInTextarea[key];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
var _getContentRectInTextarea = require("./getContentRectInTextarea.js");
|
|
84
|
+
Object.keys(_getContentRectInTextarea).forEach(function (key) {
|
|
85
|
+
if (key === "default" || key === "__esModule") return;
|
|
86
|
+
if (key in exports && exports[key] === _getContentRectInTextarea[key]) return;
|
|
87
|
+
Object.defineProperty(exports, key, {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function () {
|
|
90
|
+
return _getContentRectInTextarea[key];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
72
94
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["_Form","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_FormItemElement","_FormStore","_interfaces","_pickFormItemProps","_TextareaAutoResize"],"sources":["../../src/index.ts"],"sourcesContent":["export * from \"./Form.js\";\nexport * from \"./FormItemElement.js\";\nexport * from \"./FormSt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,IAAA,CAAAS,WAAA,EAAAR,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAO,WAAA,CAAAP,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAG,WAAA,CAAAP,GAAA;IAAA;EAAA;AAAA;AACA,IAAAQ,kBAAA,GAAAZ,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAU,kBAAA,EAAAT,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAQ,kBAAA,CAAAR,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAI,kBAAA,CAAAR,GAAA;IAAA;EAAA;AAAA;AACA,IAAAS,mBAAA,GAAAb,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAW,mBAAA,EAAAV,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAS,mBAAA,CAAAT,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAK,mBAAA,CAAAT,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["_Form","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_FormItemElement","_FormStore","_interfaces","_pickFormItemProps","_TextareaAutoResize","_getCaretPositionInTextarea","_getContentRectInTextarea"],"sources":["../../src/index.ts"],"sourcesContent":["export * from \"./Form.js\";\nexport * from \"./FormItemElement.js\";\nexport * from \"./FormStore.js\";\nexport * from \"./interfaces.js\";\nexport * from \"./pickFormItemProps.js\";\nexport * from \"./TextareaAutoResize.js\";\nexport * from \"./getCaretPositionInTextarea.js\";\nexport * from \"./getContentRectInTextarea.js\";\n"],"mappings":";;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,KAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,KAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,KAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,gBAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,gBAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,gBAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,gBAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,UAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,UAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,UAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA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AAAX,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAU,2BAAA,CAAAV,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAM,2BAAA,CAAAV,GAAA;IAAA;EAAA;AAAA;AACA,IAAAW,yBAAA,GAAAf,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAa,yBAAA,EAAAZ,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAW,yBAAA,CAAAX,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAO,yBAAA,CAAAX,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
|
@@ -4,21 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = calculateAutoSizeStyle;
|
|
7
|
+
var _getMirroredStyle = require("./getMirroredStyle.js");
|
|
7
8
|
// istanbul ignore file
|
|
8
9
|
|
|
9
|
-
const HIDDEN_TEXTAREA_STYLE = `
|
|
10
|
-
min-height: 0!important;
|
|
11
|
-
max-height: none!important;
|
|
12
|
-
height: 0!important;
|
|
13
|
-
visibility: hidden!important;
|
|
14
|
-
overflow: hidden!important;
|
|
15
|
-
position: absolute!important;
|
|
16
|
-
z-index: -1000!important;
|
|
17
|
-
top: 0!important;
|
|
18
|
-
right: 0!important;
|
|
19
|
-
pointer-events: none!important;
|
|
20
|
-
`;
|
|
21
|
-
const SIZING_STYLE = ["letter-spacing", "line-height", "padding-top", "padding-bottom", "font-family", "font-weight", "font-size", "font-variant", "text-rendering", "text-transform", "width", "text-indent", "padding-left", "padding-right", "border-width", "box-sizing", "word-break", "white-space"];
|
|
22
10
|
let hiddenTextarea;
|
|
23
11
|
/**
|
|
24
12
|
* 计算 textarea 高度
|
|
@@ -37,11 +25,9 @@ function calculateAutoSizeStyle(uiTextNode, options) {
|
|
|
37
25
|
hiddenTextarea.setAttribute("aria-hidden", "true");
|
|
38
26
|
document.body.appendChild(hiddenTextarea);
|
|
39
27
|
}
|
|
40
|
-
const uiTextNodeStyle = window.getComputedStyle(uiTextNode);
|
|
41
|
-
const sizingStyle = SIZING_STYLE.map(name => `${name}:${uiTextNodeStyle.getPropertyValue(name)}`).join(";");
|
|
42
28
|
|
|
43
29
|
// equal style
|
|
44
|
-
hiddenTextarea.setAttribute("style",
|
|
30
|
+
hiddenTextarea.setAttribute("style", (0, _getMirroredStyle.getMirroredStyle)(uiTextNode));
|
|
45
31
|
hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || "";
|
|
46
32
|
let minHeight;
|
|
47
33
|
let maxHeight;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calculateAutoSizeStyle.js","names":["
|
|
1
|
+
{"version":3,"file":"calculateAutoSizeStyle.js","names":["_getMirroredStyle","require","hiddenTextarea","calculateAutoSizeStyle","uiTextNode","options","minRows","maxRows","borderSize","paddingSize","document","createElement","setAttribute","body","appendChild","getMirroredStyle","value","placeholder","minHeight","maxHeight","overflowY","height","scrollHeight","singleRowHeight","process","env","NODE_ENV","parseFloat","window","getComputedStyle","getPropertyValue","Math","max","min","style","resize"],"sources":["../../../src/utils/calculateAutoSizeStyle.ts"],"sourcesContent":["// istanbul ignore file\nimport type React from \"react\";\nimport { getMirroredStyle } from \"./getMirroredStyle.js\";\n\nlet hiddenTextarea: HTMLTextAreaElement | undefined;\n\nexport interface AutoSizeOptions {\n minRows?: number | null;\n maxRows?: number | null;\n borderSize?: number;\n paddingSize?: number;\n}\n\n/**\n * 计算 textarea 高度\n * https://github.com/react-component/textarea/blob/1c0026fbe30e5f7dff1fca695b2cf262246381ca/src/calculateNodeHeight.tsx\n */\nexport default function calculateAutoSizeStyle(\n uiTextNode: HTMLTextAreaElement,\n options?: AutoSizeOptions\n): React.CSSProperties {\n const {\n minRows = null,\n maxRows = null,\n borderSize = 2,\n paddingSize = 8,\n } = options ?? {};\n\n if (!hiddenTextarea) {\n hiddenTextarea = document.createElement(\"textarea\");\n hiddenTextarea.setAttribute(\"tab-index\", \"-1\");\n hiddenTextarea.setAttribute(\"aria-hidden\", \"true\");\n document.body.appendChild(hiddenTextarea);\n }\n\n // equal style\n hiddenTextarea.setAttribute(\"style\", getMirroredStyle(uiTextNode));\n hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || \"\";\n\n let minHeight: number | undefined;\n let maxHeight: number | undefined;\n let overflowY: React.CSSProperties[\"overflowY\"];\n\n let height = hiddenTextarea.scrollHeight + borderSize;\n\n if (minRows !== null || maxRows !== null) {\n const singleRowHeight =\n process.env.NODE_ENV === \"test\"\n ? 22\n : parseFloat(\n window\n .getComputedStyle(hiddenTextarea)\n .getPropertyValue(\"line-height\")\n );\n if (minRows !== null) {\n minHeight = singleRowHeight * minRows + paddingSize + borderSize;\n height = Math.max(minHeight, height);\n }\n if (maxRows !== null) {\n maxHeight = singleRowHeight * maxRows + paddingSize + borderSize;\n if (height <= maxHeight) {\n overflowY = \"hidden\";\n }\n height = Math.min(maxHeight, height);\n }\n }\n\n const style: React.CSSProperties = {\n height,\n overflowY,\n resize: \"none\",\n };\n\n if (minHeight) {\n style.minHeight = minHeight;\n }\n if (maxHeight) {\n style.maxHeight = maxHeight;\n }\n\n return style;\n}\n"],"mappings":";;;;;;AAEA,IAAAA,iBAAA,GAAAC,OAAA;AAFA;;AAIA,IAAIC,cAA+C;AASnD;AACA;AACA;AACA;AACe,SAASC,sBAAsBA,CAC5CC,UAA+B,EAC/BC,OAAyB,EACJ;EACrB,MAAM;IACJC,OAAO,GAAG,IAAI;IACd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,SAAS,EAAE;QACvBC,SAAS,GAAG,QAAQ;MACtB;MACAC,MAAM,GAAGU,IAAI,CAACE,GAAG,CAACd,SAAS,EAAEE,MAAM,CAAC;IACtC;EACF;EAEA,MAAMa,KAA0B,GAAG;IACjCb,MAAM;IACND,SAAS;IACTe,MAAM,EAAE;EACV,CAAC;EAED,IAAIjB,SAAS,EAAE;IACbgB,KAAK,CAAChB,SAAS,GAAGA,SAAS;EAC7B;EACA,IAAIC,SAAS,EAAE;IACbe,KAAK,CAACf,SAAS,GAAGA,SAAS;EAC7B;EAEA,OAAOe,KAAK;AACd","ignoreList":[]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getMirroredStyle = getMirroredStyle;
|
|
7
|
+
const HIDDEN_STYLE = `
|
|
8
|
+
min-height: 0!important;
|
|
9
|
+
max-height: none!important;
|
|
10
|
+
height: 0!important;
|
|
11
|
+
visibility: hidden!important;
|
|
12
|
+
overflow: hidden!important;
|
|
13
|
+
position: absolute!important;
|
|
14
|
+
z-index: -1000!important;
|
|
15
|
+
top: 0!important;
|
|
16
|
+
left: 0!important;
|
|
17
|
+
pointer-events: none!important;
|
|
18
|
+
`;
|
|
19
|
+
const SIZING_STYLE = ["letter-spacing", "line-height", "padding-top", "padding-bottom", "font-family", "font-weight", "font-size", "font-variant", "text-rendering", "text-transform", "width", "text-indent", "padding-left", "padding-right", "border-width", "box-sizing", "word-break", "white-space"];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 创建一个镜像元素,用于计算光标位置或文本尺寸。
|
|
23
|
+
*/
|
|
24
|
+
function getMirroredStyle(element) {
|
|
25
|
+
const uiTextNodeStyle = window.getComputedStyle(element);
|
|
26
|
+
const sizingStyle = SIZING_STYLE.map(name => `${name}:${uiTextNodeStyle.getPropertyValue(name)}`).join(";");
|
|
27
|
+
return `${sizingStyle};${HIDDEN_STYLE}`;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=getMirroredStyle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMirroredStyle.js","names":["HIDDEN_STYLE","SIZING_STYLE","getMirroredStyle","element","uiTextNodeStyle","window","getComputedStyle","sizingStyle","map","name","getPropertyValue","join"],"sources":["../../../src/utils/getMirroredStyle.ts"],"sourcesContent":["const HIDDEN_STYLE = `\n min-height: 0!important;\n max-height: none!important;\n height: 0!important;\n visibility: hidden!important;\n overflow: hidden!important;\n position: absolute!important;\n z-index: -1000!important;\n top: 0!important;\n left: 0!important;\n pointer-events: none!important;\n`;\n\nconst SIZING_STYLE = [\n \"letter-spacing\",\n \"line-height\",\n \"padding-top\",\n \"padding-bottom\",\n \"font-family\",\n \"font-weight\",\n \"font-size\",\n \"font-variant\",\n \"text-rendering\",\n \"text-transform\",\n \"width\",\n \"text-indent\",\n \"padding-left\",\n \"padding-right\",\n \"border-width\",\n \"box-sizing\",\n \"word-break\",\n \"white-space\",\n];\n\n/**\n * 创建一个镜像元素,用于计算光标位置或文本尺寸。\n */\nexport function getMirroredStyle(element: HTMLElement) {\n const uiTextNodeStyle = window.getComputedStyle(element);\n const sizingStyle = SIZING_STYLE.map(\n (name) => `${name}:${uiTextNodeStyle.getPropertyValue(name)}`\n ).join(\";\");\n return `${sizingStyle};${HIDDEN_STYLE}`;\n}\n"],"mappings":";;;;;;AAAA,MAAMA,YAAY,GAAG;AACrB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AAED,MAAMC,YAAY,GAAG,CACnB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,OAAO,EACP,aAAa,EACb,cAAc,EACd,eAAe,EACf,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,aAAa,CACd;;AAED;AACA;AACA;AACO,SAASC,gBAAgBA,CAACC,OAAoB,EAAE;EACrD,MAAMC,eAAe,GAAGC,MAAM,CAACC,gBAAgB,CAACH,OAAO,CAAC;EACxD,MAAMI,WAAW,GAAGN,YAAY,CAACO,GAAG,CACjCC,IAAI,IAAK,GAAGA,IAAI,IAAIL,eAAe,CAACM,gBAAgB,CAACD,IAAI,CAAC,EAC7D,CAAC,CAACE,IAAI,CAAC,GAAG,CAAC;EACX,OAAO,GAAGJ,WAAW,IAAIP,YAAY,EAAE;AACzC","ignoreList":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/extends";
|
|
2
|
-
import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from "react";
|
|
2
|
+
import React, { useCallback, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from "react";
|
|
3
3
|
import { flushSync } from "react-dom";
|
|
4
4
|
import ResizeObserver from "resize-observer-polyfill";
|
|
5
5
|
import calculateAutoSizeStyle from "./utils/calculateAutoSizeStyle.js";
|
|
@@ -18,6 +18,7 @@ function LegacyTextareaAutoResize(_ref, ref) {
|
|
|
18
18
|
value: propValue,
|
|
19
19
|
style,
|
|
20
20
|
submitWhen,
|
|
21
|
+
desiredSelectionRef,
|
|
21
22
|
onChange,
|
|
22
23
|
onSubmit,
|
|
23
24
|
onKeyDown,
|
|
@@ -39,7 +40,8 @@ function LegacyTextareaAutoResize(_ref, ref) {
|
|
|
39
40
|
textarea.focus();
|
|
40
41
|
valueLength && textarea.setSelectionRange(valueLength, valueLength);
|
|
41
42
|
}
|
|
42
|
-
}
|
|
43
|
+
},
|
|
44
|
+
element: textareaRef.current
|
|
43
45
|
}), []);
|
|
44
46
|
const doAutoResize = useCallback(() => {
|
|
45
47
|
const textarea = textareaRef.current;
|
|
@@ -84,13 +86,27 @@ function LegacyTextareaAutoResize(_ref, ref) {
|
|
|
84
86
|
// Ignore key events during composition
|
|
85
87
|
return;
|
|
86
88
|
}
|
|
89
|
+
if ((onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(e)) === false) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
87
92
|
if (e.key === "Enter" && (submitWhen === "enter-without-shift" ? !e.shiftKey : submitWhen === "enter-with-mod" && e[modKey])) {
|
|
88
93
|
e.preventDefault();
|
|
89
94
|
e.stopPropagation();
|
|
90
95
|
onSubmit === null || onSubmit === void 0 || onSubmit(e);
|
|
91
96
|
}
|
|
92
|
-
onKeyDown === null || onKeyDown === void 0 || onKeyDown(e);
|
|
93
97
|
}, [onKeyDown, onSubmit, submitWhen]);
|
|
98
|
+
useLayoutEffect(() => {
|
|
99
|
+
const desiredSelection = desiredSelectionRef === null || desiredSelectionRef === void 0 ? void 0 : desiredSelectionRef.current;
|
|
100
|
+
const textarea = textareaRef.current;
|
|
101
|
+
if (desiredSelection && textarea) {
|
|
102
|
+
const {
|
|
103
|
+
start,
|
|
104
|
+
end
|
|
105
|
+
} = desiredSelection;
|
|
106
|
+
desiredSelectionRef.current = null;
|
|
107
|
+
textarea.setSelectionRange(start, end);
|
|
108
|
+
}
|
|
109
|
+
}, [desiredSelectionRef, value]);
|
|
94
110
|
|
|
95
111
|
// istanbul ignore next
|
|
96
112
|
useEffect(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TextareaAutoResize.js","names":["React","useCallback","useEffect","useImperativeHandle","useRef","useState","flushSync","ResizeObserver","calculateAutoSizeStyle","modKey","test","navigator","platform","TextareaAutoResize","forwardRef","LegacyTextareaAutoResize","_ref","ref","autoResize","_autoResize","minRows","maxRows","borderSize","paddingSize","containerRef","value","propValue","style","submitWhen","onChange","onSubmit","onKeyDown","onCompositionStart","onCompositionEnd","props","setValue","textareaRef","autoStyle","setAutoStyle","focus","textarea","current","_textarea$value","valueLength","length","setSelectionRange","doAutoResize","process","env","NODE_ENV","handleInputChange","e","target","compositionRef","handleCompositionStart","handleCompositionEnd","handleKeyDown","key","shiftKey","preventDefault","stopPropagation","container","previousInlineSize","observer","entries","entry","currentInlineSize","contentBoxSize","inlineSize","contentRect","width","undefined","isInitial","requestAnimationFrame","observe","disconnect","createElement","_extends"],"sources":["../../src/TextareaAutoResize.tsx"],"sourcesContent":["import React, {\n useCallback,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\nimport { flushSync } from \"react-dom\";\nimport ResizeObserver from \"resize-observer-polyfill\";\nimport calculateAutoSizeStyle from \"./utils/calculateAutoSizeStyle.js\";\n\n// istanbul ignore next\nconst modKey = /Mac|iPod|iPhone|iPad/.test(navigator.platform)\n ? \"metaKey\"\n : \"ctrlKey\";\n\nexport interface TextareaAutoResizeProps\n extends React.DetailedHTMLProps<\n React.TextareaHTMLAttributes<HTMLTextAreaElement>,\n HTMLTextAreaElement\n > {\n /** @default true */\n autoResize?: boolean;\n minRows?: number | null;\n maxRows?: number | null;\n /** @default 2 */\n borderSize?: number;\n /** @default 8 */\n paddingSize?: number;\n containerRef?: React.RefObject<HTMLElement>;\n submitWhen?: \"enter-without-shift\" | \"enter-with-mod\";\n}\n\nexport interface TextareaAutoResizeRef {\n focus(): void;\n}\n\nexport const TextareaAutoResize = React.forwardRef<\n TextareaAutoResizeRef,\n TextareaAutoResizeProps\n>(LegacyTextareaAutoResize);\n\nfunction LegacyTextareaAutoResize(\n {\n autoResize: _autoResize,\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n containerRef,\n value: propValue,\n style,\n submitWhen,\n onChange,\n onSubmit,\n onKeyDown,\n onCompositionStart,\n onCompositionEnd,\n ...props\n }: TextareaAutoResizeProps,\n ref: React.ForwardedRef<TextareaAutoResizeRef>\n): React.JSX.Element {\n const autoResize = _autoResize ?? true;\n const [value, setValue] = useState(propValue ?? \"\");\n const textareaRef = useRef<HTMLTextAreaElement>(null);\n const [autoStyle, setAutoStyle] = useState<React.CSSProperties | null>(null);\n\n useImperativeHandle(\n ref,\n () => ({\n focus: () => {\n const textarea = textareaRef.current;\n // istanbul ignore else: defensive check\n if (textarea) {\n const valueLength = textarea.value?.length;\n textarea.focus();\n valueLength && textarea.setSelectionRange(valueLength, valueLength);\n }\n },\n }),\n []\n );\n\n const doAutoResize = useCallback(() => {\n const textarea = textareaRef.current;\n if (textarea && autoResize) {\n const style = calculateAutoSizeStyle(textarea, {\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n });\n // istanbul ignore next\n if (process.env.NODE_ENV === \"test\") {\n setAutoStyle(style);\n } else {\n flushSync(() => {\n setAutoStyle(style);\n });\n }\n }\n }, [autoResize, maxRows, minRows, borderSize, paddingSize]);\n\n const handleInputChange = (\n e: React.ChangeEvent<HTMLTextAreaElement>\n ): void => {\n setValue(e.target.value);\n onChange?.(e);\n };\n\n useEffect(() => {\n setValue(propValue ?? \"\");\n }, [propValue]);\n\n useEffect(() => {\n doAutoResize();\n }, [doAutoResize, value]);\n\n const compositionRef = useRef(false);\n\n const handleCompositionStart = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = true;\n onCompositionStart?.(e);\n },\n [onCompositionStart]\n );\n\n const handleCompositionEnd = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = false;\n onCompositionEnd?.(e);\n },\n [onCompositionEnd]\n );\n\n const handleKeyDown = useCallback(\n (e: React.KeyboardEvent<HTMLTextAreaElement>) => {\n if (compositionRef.current) {\n // Ignore key events during composition\n return;\n }\n\n if (\n e.key === \"Enter\" &&\n (submitWhen === \"enter-without-shift\"\n ? !e.shiftKey\n : submitWhen === \"enter-with-mod\" && e[modKey])\n ) {\n e.preventDefault();\n e.stopPropagation();\n onSubmit?.(e);\n }\n\n onKeyDown?.(e);\n },\n [onKeyDown, onSubmit, submitWhen]\n );\n\n // istanbul ignore next\n useEffect(() => {\n const container = containerRef?.current;\n if (!container || !autoResize) {\n return;\n }\n let previousInlineSize: number | undefined;\n const observer = new ResizeObserver((entries) => {\n for (const entry of entries) {\n if (entry.target === container) {\n // istanbul ignore next: compatibility\n const currentInlineSize = entry.contentBoxSize\n ? entry.contentBoxSize[0]\n ? entry.contentBoxSize[0].inlineSize\n : (entry.contentBoxSize as unknown as ResizeObserverSize)\n .inlineSize\n : entry.contentRect.width;\n if (\n currentInlineSize !== undefined &&\n currentInlineSize !== previousInlineSize\n ) {\n const isInitial = !previousInlineSize;\n previousInlineSize = currentInlineSize;\n if (!isInitial) {\n requestAnimationFrame(doAutoResize);\n }\n }\n }\n }\n });\n observer.observe(container);\n return () => {\n observer.disconnect();\n };\n }, [autoResize, containerRef, doAutoResize]);\n\n return (\n <textarea\n {...props}\n ref={textareaRef}\n value={value}\n style={{\n ...style,\n ...autoStyle,\n }}\n onChange={handleInputChange}\n onCompositionStart={handleCompositionStart}\n onCompositionEnd={handleCompositionEnd}\n onKeyDown={handleKeyDown}\n />\n );\n}\n"],"mappings":";AAAA,OAAOA,KAAK,IACVC,WAAW,EACXC,SAAS,EACTC,mBAAmB,EACnBC,MAAM,EACNC,QAAQ,QACH,OAAO;AACd,SAASC,SAAS,QAAQ,WAAW;AACrC,OAAOC,cAAc,MAAM,0BAA0B;AACrD,OAAOC,sBAAsB,MAAM,mCAAmC;;AAEtE;AACA,MAAMC,MAAM,GAAG,sBAAsB,CAACC,IAAI,CAACC,SAAS,CAACC,QAAQ,CAAC,GAC1D,SAAS,GACT,SAAS;AAuBb,OAAO,MAAMC,kBAAkB,gBAAGb,KAAK,CAACc,UAAU,CAGhDC,wBAAwB,CAAC;AAE3B,SAASA,wBAAwBA,CAAAC,IAAA,EAkB/BC,GAA8C,EAC3B;EAAA,IAlBnB;IACEC,UAAU,EAAEC,WAAW;IACvBC,OAAO;IACPC,OAAO;IACPC,UAAU;IACVC,WAAW;IACXC,YAAY;IACZC,KAAK,EAAEC,SAAS;IAChBC,KAAK;IACLC,UAAU;IACVC,QAAQ;IACRC,QAAQ;IACRC,SAAS;IACTC,kBAAkB;IAClBC,gBAAgB;IAChB,GAAGC;EACoB,CAAC,GA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ACvB,CAAC;EACH,CAAC,EAAE,CAACzD,UAAU,EAAEM,YAAY,EAAEsB,YAAY,CAAC,CAAC;EAE5C,oBACE9C,KAAA,CAAA4E,aAAA,aAAAC,QAAA,KACM3C,KAAK;IACTjB,GAAG,EAAEmB,WAAY;IACjBX,KAAK,EAAEA,KAAM;IACbE,KAAK,EAAE;MACL,GAAGA,KAAK;MACR,GAAGU;IACL,CAAE;IACFR,QAAQ,EAAEqB,iBAAkB;IAC5BlB,kBAAkB,EAAEsB,sBAAuB;IAC3CrB,gBAAgB,EAAEsB,oBAAqB;IACvCxB,SAAS,EAAEyB;EAAc,EAC1B,CAAC;AAEN","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"TextareaAutoResize.js","names":["React","useCallback","useEffect","useImperativeHandle","useLayoutEffect","useRef","useState","flushSync","ResizeObserver","calculateAutoSizeStyle","modKey","test","navigator","platform","TextareaAutoResize","forwardRef","LegacyTextareaAutoResize","_ref","ref","autoResize","_autoResize","minRows","maxRows","borderSize","paddingSize","containerRef","value","propValue","style","submitWhen","desiredSelectionRef","onChange","onSubmit","onKeyDown","onCompositionStart","onCompositionEnd","props","setValue","textareaRef","autoStyle","setAutoStyle","focus","textarea","current","_textarea$value","valueLength","length","setSelectionRange","element","doAutoResize","process","env","NODE_ENV","handleInputChange","e","target","compositionRef","handleCompositionStart","handleCompositionEnd","handleKeyDown","key","shiftKey","preventDefault","stopPropagation","desiredSelection","start","end","container","previousInlineSize","observer","entries","entry","currentInlineSize","contentBoxSize","inlineSize","contentRect","width","undefined","isInitial","requestAnimationFrame","observe","disconnect","createElement","_extends"],"sources":["../../src/TextareaAutoResize.tsx"],"sourcesContent":["import React, {\n useCallback,\n useEffect,\n useImperativeHandle,\n useLayoutEffect,\n useRef,\n useState,\n} from \"react\";\nimport { flushSync } from \"react-dom\";\nimport ResizeObserver from \"resize-observer-polyfill\";\nimport calculateAutoSizeStyle from \"./utils/calculateAutoSizeStyle.js\";\n\n// istanbul ignore next\nconst modKey = /Mac|iPod|iPhone|iPad/.test(navigator.platform)\n ? \"metaKey\"\n : \"ctrlKey\";\n\nexport interface TextareaAutoResizeProps\n extends React.DetailedHTMLProps<\n React.TextareaHTMLAttributes<HTMLTextAreaElement>,\n HTMLTextAreaElement\n > {\n /** @default true */\n autoResize?: boolean;\n minRows?: number | null;\n maxRows?: number | null;\n /** @default 2 */\n borderSize?: number;\n /** @default 8 */\n paddingSize?: number;\n containerRef?: React.RefObject<HTMLElement>;\n submitWhen?: \"enter-without-shift\" | \"enter-with-mod\";\n desiredSelectionRef?: React.MutableRefObject<{\n start: number;\n end: number;\n } | null>;\n onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;\n /**\n * The handler can return false to prevent default behavior (submit)\n */\n onKeyDown?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void | false;\n}\n\nexport interface TextareaAutoResizeRef {\n focus(): void;\n element: HTMLTextAreaElement | null;\n}\n\nexport const TextareaAutoResize = React.forwardRef<\n TextareaAutoResizeRef,\n TextareaAutoResizeProps\n>(LegacyTextareaAutoResize);\n\nfunction LegacyTextareaAutoResize(\n {\n autoResize: _autoResize,\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n containerRef,\n value: propValue,\n style,\n submitWhen,\n desiredSelectionRef,\n onChange,\n onSubmit,\n onKeyDown,\n onCompositionStart,\n onCompositionEnd,\n ...props\n }: TextareaAutoResizeProps,\n ref: React.ForwardedRef<TextareaAutoResizeRef>\n): React.JSX.Element {\n const autoResize = _autoResize ?? true;\n const [value, setValue] = useState(propValue ?? \"\");\n const textareaRef = useRef<HTMLTextAreaElement>(null);\n const [autoStyle, setAutoStyle] = useState<React.CSSProperties | null>(null);\n\n useImperativeHandle(\n ref,\n () => ({\n focus: () => {\n const textarea = textareaRef.current;\n // istanbul ignore else: defensive check\n if (textarea) {\n const valueLength = textarea.value?.length;\n textarea.focus();\n valueLength && textarea.setSelectionRange(valueLength, valueLength);\n }\n },\n element: textareaRef.current,\n }),\n []\n );\n\n const doAutoResize = useCallback(() => {\n const textarea = textareaRef.current;\n if (textarea && autoResize) {\n const style = calculateAutoSizeStyle(textarea, {\n minRows,\n maxRows,\n borderSize,\n paddingSize,\n });\n // istanbul ignore next\n if (process.env.NODE_ENV === \"test\") {\n setAutoStyle(style);\n } else {\n flushSync(() => {\n setAutoStyle(style);\n });\n }\n }\n }, [autoResize, maxRows, minRows, borderSize, paddingSize]);\n\n const handleInputChange = (\n e: React.ChangeEvent<HTMLTextAreaElement>\n ): void => {\n setValue(e.target.value);\n onChange?.(e);\n };\n\n useEffect(() => {\n setValue(propValue ?? \"\");\n }, [propValue]);\n\n useEffect(() => {\n doAutoResize();\n }, [doAutoResize, value]);\n\n const compositionRef = useRef(false);\n\n const handleCompositionStart = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = true;\n onCompositionStart?.(e);\n },\n [onCompositionStart]\n );\n\n const handleCompositionEnd = useCallback(\n (e: React.CompositionEvent<HTMLTextAreaElement>) => {\n compositionRef.current = false;\n onCompositionEnd?.(e);\n },\n [onCompositionEnd]\n );\n\n const handleKeyDown = useCallback(\n (e: React.KeyboardEvent<HTMLTextAreaElement>) => {\n if (compositionRef.current) {\n // Ignore key events during composition\n return;\n }\n\n if (onKeyDown?.(e) === false) {\n return;\n }\n\n if (\n e.key === \"Enter\" &&\n (submitWhen === \"enter-without-shift\"\n ? !e.shiftKey\n : submitWhen === \"enter-with-mod\" && e[modKey])\n ) {\n e.preventDefault();\n e.stopPropagation();\n onSubmit?.(e);\n }\n },\n [onKeyDown, onSubmit, submitWhen]\n );\n\n useLayoutEffect(() => {\n const desiredSelection = desiredSelectionRef?.current;\n const textarea = textareaRef.current;\n if (desiredSelection && textarea) {\n const { start, end } = desiredSelection;\n desiredSelectionRef.current = null;\n textarea.setSelectionRange(start, end);\n }\n }, [desiredSelectionRef, value]);\n\n // istanbul ignore next\n useEffect(() => {\n const container = containerRef?.current;\n if (!container || !autoResize) {\n return;\n }\n let previousInlineSize: number | undefined;\n const observer = new ResizeObserver((entries) => {\n for (const entry of entries) {\n if (entry.target === container) {\n // istanbul ignore next: compatibility\n const currentInlineSize = entry.contentBoxSize\n ? entry.contentBoxSize[0]\n ? entry.contentBoxSize[0].inlineSize\n : (entry.contentBoxSize as unknown as ResizeObserverSize)\n .inlineSize\n : entry.contentRect.width;\n if (\n currentInlineSize !== undefined &&\n currentInlineSize !== previousInlineSize\n ) {\n const isInitial = !previousInlineSize;\n previousInlineSize = currentInlineSize;\n if (!isInitial) {\n requestAnimationFrame(doAutoResize);\n }\n }\n }\n }\n });\n observer.observe(container);\n return () => {\n observer.disconnect();\n };\n }, [autoResize, containerRef, doAutoResize]);\n\n return (\n <textarea\n {...props}\n ref={textareaRef}\n value={value}\n style={{\n ...style,\n ...autoStyle,\n }}\n onChange={handleInputChange}\n onCompositionStart={handleCompositionStart}\n onCompositionEnd={handleCompositionEnd}\n onKeyDown={handleKeyDown}\n />\n );\n}\n"],"mappings":";AAAA,OAAOA,KAAK,IACVC,WAAW,EACXC,SAAS,EACTC,mBAAmB,EACnBC,eAAe,EACfC,MAAM,EACNC,QAAQ,QACH,OAAO;AACd,SAASC,SAAS,QAAQ,WAAW;AACrC,OAAOC,cAAc,MAAM,0BAA0B;AACrD,OAAOC,sBAAsB,MAAM,mCAAmC;;AAEtE;AACA,MAAMC,MAAM,GAAG,sBAAsB,CAACC,IAAI,CAACC,SAAS,CAACC,QAAQ,CAAC,GAC1D,SAAS,GACT,SAAS;AAiCb,OAAO,MAAMC,kBAAkB,gBAAGd,KAAK,CAACe,UAAU,CAGhDC,wBAAwB,CAAC;AAE3B,SAASA,wBAAwBA,CAAAC,IAAA,EAmB/BC,GAA8C,EAC3B;EAAA,IAnBnB;IACEC,UAAU,EAAEC,WAAW;IACvBC,OAAO;IACPC,OAAO;IACPC,UAAU;IACVC,WAAW;IACXC,YAAY;IACZC,KAAK,EAAEC,SAAS;IAChBC,KAAK;IACLC,UAAU;IACVC,mBAAmB;IACnBC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,CAAChB,MAAM,KAAKY,SAAS,EAAE;UAC9B;UACA,MAAMK,iBAAiB,GAAGD,KAAK,CAACE,cAAc,GAC1CF,KAAK,CAACE,cAAc,CAAC,CAAC,CAAC,GACrBF,KAAK,CAACE,cAAc,CAAC,CAAC,CAAC,CAACC,UAAU,GACjCH,KAAK,CAACE,cAAc,CAClBC,UAAU,GACfH,KAAK,CAACI,WAAW,CAACC,KAAK;UAC3B,IACEJ,iBAAiB,KAAKK,SAAS,IAC/BL,iBAAiB,KAAKJ,kBAAkB,EACxC;YACA,MAAMU,SAAS,GAAG,CAACV,kBAAkB;YACrCA,kBAAkB,GAAGI,iBAAiB;YACtC,IAAI,CAACM,SAAS,EAAE;cACdC,qBAAqB,CAAC9B,YAAY,CAAC;YACrC;UACF;QACF;MACF;IACF,CAAC,CAAC;IACFoB,QAAQ,CAACW,OAAO,CAACb,SAAS,CAAC;IAC3B,OAAO,MAAM;MACXE,QAAQ,CAACY,UAAU,CAAC,CAAC;IACvB,CAAC;EACH,CAAC,EAAE,CAAC9D,UAAU,EAAEM,YAAY,EAAEwB,YAAY,CAAC,CAAC;EAE5C,oBACEjD,KAAA,CAAAkF,aAAA,aAAAC,QAAA,KACM/C,KAAK;IACTlB,GAAG,EAAEoB,WAAY;IACjBZ,KAAK,EAAEA,KAAM;IACbE,KAAK,EAAE;MACL,GAAGA,KAAK;MACR,GAAGW;IACL,CAAE;IACFR,QAAQ,EAAEsB,iBAAkB;IAC5BnB,kBAAkB,EAAEuB,sBAAuB;IAC3CtB,gBAAgB,EAAEuB,oBAAqB;IACvCzB,SAAS,EAAE0B;EAAc,EAC1B,CAAC;AAEN","ignoreList":[]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getMirroredStyle } from "./utils/getMirroredStyle.js";
|
|
2
|
+
let hiddenElement;
|
|
3
|
+
export function getCaretPositionInTextarea(textarea, index) {
|
|
4
|
+
if (!hiddenElement) {
|
|
5
|
+
hiddenElement = document.createElement("div");
|
|
6
|
+
hiddenElement.setAttribute("tab-index", "-1");
|
|
7
|
+
hiddenElement.setAttribute("aria-hidden", "true");
|
|
8
|
+
document.body.appendChild(hiddenElement);
|
|
9
|
+
}
|
|
10
|
+
hiddenElement.setAttribute("style", getMirroredStyle(textarea));
|
|
11
|
+
hiddenElement.textContent = textarea.value.slice(0, index);
|
|
12
|
+
const span = document.createElement("span");
|
|
13
|
+
span.textContent = "\u200b"; // 占位符
|
|
14
|
+
hiddenElement.appendChild(span);
|
|
15
|
+
const rect = span.getBoundingClientRect();
|
|
16
|
+
span.remove();
|
|
17
|
+
return {
|
|
18
|
+
top: rect.top,
|
|
19
|
+
left: rect.left
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=getCaretPositionInTextarea.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCaretPositionInTextarea.js","names":["getMirroredStyle","hiddenElement","getCaretPositionInTextarea","textarea","index","document","createElement","setAttribute","body","appendChild","textContent","value","slice","span","rect","getBoundingClientRect","remove","top","left"],"sources":["../../src/getCaretPositionInTextarea.ts"],"sourcesContent":["import { getMirroredStyle } from \"./utils/getMirroredStyle.js\";\n\nlet hiddenElement: HTMLDivElement | undefined;\n\nexport function getCaretPositionInTextarea(\n textarea: HTMLTextAreaElement,\n index: number\n) {\n if (!hiddenElement) {\n hiddenElement = document.createElement(\"div\");\n hiddenElement.setAttribute(\"tab-index\", \"-1\");\n hiddenElement.setAttribute(\"aria-hidden\", \"true\");\n document.body.appendChild(hiddenElement);\n }\n\n hiddenElement.setAttribute(\"style\", getMirroredStyle(textarea));\n hiddenElement.textContent = textarea.value.slice(0, index);\n\n const span = document.createElement(\"span\");\n span.textContent = \"\\u200b\"; // 占位符\n hiddenElement.appendChild(span);\n\n const rect = span.getBoundingClientRect();\n span.remove();\n return {\n top: rect.top,\n left: rect.left,\n };\n}\n"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,6BAA6B;AAE9D,IAAIC,aAAyC;AAE7C,OAAO,SAASC,0BAA0BA,CACxCC,QAA6B,EAC7BC,KAAa,EACb;EACA,IAAI,CAACH,aAAa,EAAE;IAClBA,aAAa,GAAGI,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;IAC7CL,aAAa,CAACM,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC;IAC7CN,aAAa,CAACM,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC;IACjDF,QAAQ,CAACG,IAAI,CAACC,WAAW,CAACR,aAAa,CAAC;EAC1C;EAEAA,aAAa,CAACM,YAAY,CAAC,OAAO,EAAEP,gBAAgB,CAACG,QAAQ,CAAC,CAAC;EAC/DF,aAAa,CAACS,WAAW,GAAGP,QAAQ,CAACQ,KAAK,CAACC,KAAK,CAAC,CAAC,EAAER,KAAK,CAAC;EAE1D,MAAMS,IAAI,GAAGR,QAAQ,CAACC,aAAa,CAAC,MAAM,CAAC;EAC3CO,IAAI,CAACH,WAAW,GAAG,QAAQ,CAAC,CAAC;EAC7BT,aAAa,CAACQ,WAAW,CAACI,IAAI,CAAC;EAE/B,MAAMC,IAAI,GAAGD,IAAI,CAACE,qBAAqB,CAAC,CAAC;EACzCF,IAAI,CAACG,MAAM,CAAC,CAAC;EACb,OAAO;IACLC,GAAG,EAAEH,IAAI,CAACG,GAAG;IACbC,IAAI,EAAEJ,IAAI,CAACI;EACb,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getMirroredStyle } from "./utils/getMirroredStyle.js";
|
|
2
|
+
let hiddenElement;
|
|
3
|
+
export function getContentRectInTextarea(textarea, prefix, measureContent) {
|
|
4
|
+
if (!hiddenElement) {
|
|
5
|
+
hiddenElement = document.createElement("div");
|
|
6
|
+
hiddenElement.setAttribute("tab-index", "-1");
|
|
7
|
+
hiddenElement.setAttribute("aria-hidden", "true");
|
|
8
|
+
document.body.appendChild(hiddenElement);
|
|
9
|
+
}
|
|
10
|
+
hiddenElement.setAttribute("style", getMirroredStyle(textarea));
|
|
11
|
+
hiddenElement.textContent = prefix;
|
|
12
|
+
const span = document.createElement("span");
|
|
13
|
+
span.textContent = measureContent;
|
|
14
|
+
hiddenElement.appendChild(span);
|
|
15
|
+
const rect = span.getBoundingClientRect();
|
|
16
|
+
span.remove();
|
|
17
|
+
return rect;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=getContentRectInTextarea.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getContentRectInTextarea.js","names":["getMirroredStyle","hiddenElement","getContentRectInTextarea","textarea","prefix","measureContent","document","createElement","setAttribute","body","appendChild","textContent","span","rect","getBoundingClientRect","remove"],"sources":["../../src/getContentRectInTextarea.ts"],"sourcesContent":["import { getMirroredStyle } from \"./utils/getMirroredStyle.js\";\n\nlet hiddenElement: HTMLDivElement | undefined;\n\nexport function getContentRectInTextarea(\n textarea: HTMLTextAreaElement,\n prefix: string,\n measureContent: string\n) {\n if (!hiddenElement) {\n hiddenElement = document.createElement(\"div\");\n hiddenElement.setAttribute(\"tab-index\", \"-1\");\n hiddenElement.setAttribute(\"aria-hidden\", \"true\");\n document.body.appendChild(hiddenElement);\n }\n hiddenElement.setAttribute(\"style\", getMirroredStyle(textarea));\n\n hiddenElement.textContent = prefix;\n\n const span = document.createElement(\"span\");\n span.textContent = measureContent;\n hiddenElement.appendChild(span);\n\n const rect = span.getBoundingClientRect();\n span.remove();\n return rect;\n}\n"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,6BAA6B;AAE9D,IAAIC,aAAyC;AAE7C,OAAO,SAASC,wBAAwBA,CACtCC,QAA6B,EAC7BC,MAAc,EACdC,cAAsB,EACtB;EACA,IAAI,CAACJ,aAAa,EAAE;IAClBA,aAAa,GAAGK,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;IAC7CN,aAAa,CAACO,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC;IAC7CP,aAAa,CAACO,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC;IACjDF,QAAQ,CAACG,IAAI,CAACC,WAAW,CAACT,aAAa,CAAC;EAC1C;EACAA,aAAa,CAACO,YAAY,CAAC,OAAO,EAAER,gBAAgB,CAACG,QAAQ,CAAC,CAAC;EAE/DF,aAAa,CAACU,WAAW,GAAGP,MAAM;EAElC,MAAMQ,IAAI,GAAGN,QAAQ,CAACC,aAAa,CAAC,MAAM,CAAC;EAC3CK,IAAI,CAACD,WAAW,GAAGN,cAAc;EACjCJ,aAAa,CAACS,WAAW,CAACE,IAAI,CAAC;EAE/B,MAAMC,IAAI,GAAGD,IAAI,CAACE,qBAAqB,CAAC,CAAC;EACzCF,IAAI,CAACG,MAAM,CAAC,CAAC;EACb,OAAOF,IAAI;AACb","ignoreList":[]}
|
package/dist/esm/index.js
CHANGED
|
@@ -4,4 +4,6 @@ export * from "./FormStore.js";
|
|
|
4
4
|
export * from "./interfaces.js";
|
|
5
5
|
export * from "./pickFormItemProps.js";
|
|
6
6
|
export * from "./TextareaAutoResize.js";
|
|
7
|
+
export * from "./getCaretPositionInTextarea.js";
|
|
8
|
+
export * from "./getContentRectInTextarea.js";
|
|
7
9
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["export * from \"./Form.js\";\nexport * from \"./FormItemElement.js\";\nexport * from \"./FormStore.js\";\nexport * from \"./interfaces.js\";\nexport * from \"./pickFormItemProps.js\";\nexport * from \"./TextareaAutoResize.js\";\n"],"mappings":"AAAA,cAAc,WAAW;AACzB,cAAc,sBAAsB;AACpC,cAAc,gBAAgB;AAC9B,cAAc,iBAAiB;AAC/B,cAAc,wBAAwB;AACtC,cAAc,yBAAyB","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["export * from \"./Form.js\";\nexport * from \"./FormItemElement.js\";\nexport * from \"./FormStore.js\";\nexport * from \"./interfaces.js\";\nexport * from \"./pickFormItemProps.js\";\nexport * from \"./TextareaAutoResize.js\";\nexport * from \"./getCaretPositionInTextarea.js\";\nexport * from \"./getContentRectInTextarea.js\";\n"],"mappings":"AAAA,cAAc,WAAW;AACzB,cAAc,sBAAsB;AACpC,cAAc,gBAAgB;AAC9B,cAAc,iBAAiB;AAC/B,cAAc,wBAAwB;AACtC,cAAc,yBAAyB;AACvC,cAAc,iCAAiC;AAC/C,cAAc,+BAA+B","ignoreList":[]}
|
|
@@ -1,18 +1,6 @@
|
|
|
1
1
|
// istanbul ignore file
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
min-height: 0!important;
|
|
5
|
-
max-height: none!important;
|
|
6
|
-
height: 0!important;
|
|
7
|
-
visibility: hidden!important;
|
|
8
|
-
overflow: hidden!important;
|
|
9
|
-
position: absolute!important;
|
|
10
|
-
z-index: -1000!important;
|
|
11
|
-
top: 0!important;
|
|
12
|
-
right: 0!important;
|
|
13
|
-
pointer-events: none!important;
|
|
14
|
-
`;
|
|
15
|
-
const SIZING_STYLE = ["letter-spacing", "line-height", "padding-top", "padding-bottom", "font-family", "font-weight", "font-size", "font-variant", "text-rendering", "text-transform", "width", "text-indent", "padding-left", "padding-right", "border-width", "box-sizing", "word-break", "white-space"];
|
|
3
|
+
import { getMirroredStyle } from "./getMirroredStyle.js";
|
|
16
4
|
let hiddenTextarea;
|
|
17
5
|
/**
|
|
18
6
|
* 计算 textarea 高度
|
|
@@ -31,11 +19,9 @@ export default function calculateAutoSizeStyle(uiTextNode, options) {
|
|
|
31
19
|
hiddenTextarea.setAttribute("aria-hidden", "true");
|
|
32
20
|
document.body.appendChild(hiddenTextarea);
|
|
33
21
|
}
|
|
34
|
-
const uiTextNodeStyle = window.getComputedStyle(uiTextNode);
|
|
35
|
-
const sizingStyle = SIZING_STYLE.map(name => `${name}:${uiTextNodeStyle.getPropertyValue(name)}`).join(";");
|
|
36
22
|
|
|
37
23
|
// equal style
|
|
38
|
-
hiddenTextarea.setAttribute("style",
|
|
24
|
+
hiddenTextarea.setAttribute("style", getMirroredStyle(uiTextNode));
|
|
39
25
|
hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || "";
|
|
40
26
|
let minHeight;
|
|
41
27
|
let maxHeight;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calculateAutoSizeStyle.js","names":["
|
|
1
|
+
{"version":3,"file":"calculateAutoSizeStyle.js","names":["getMirroredStyle","hiddenTextarea","calculateAutoSizeStyle","uiTextNode","options","minRows","maxRows","borderSize","paddingSize","document","createElement","setAttribute","body","appendChild","value","placeholder","minHeight","maxHeight","overflowY","height","scrollHeight","singleRowHeight","process","env","NODE_ENV","parseFloat","window","getComputedStyle","getPropertyValue","Math","max","min","style","resize"],"sources":["../../../src/utils/calculateAutoSizeStyle.ts"],"sourcesContent":["// istanbul ignore file\nimport type React from \"react\";\nimport { getMirroredStyle } from \"./getMirroredStyle.js\";\n\nlet hiddenTextarea: HTMLTextAreaElement | undefined;\n\nexport interface AutoSizeOptions {\n minRows?: number | null;\n maxRows?: number | null;\n borderSize?: number;\n paddingSize?: number;\n}\n\n/**\n * 计算 textarea 高度\n * https://github.com/react-component/textarea/blob/1c0026fbe30e5f7dff1fca695b2cf262246381ca/src/calculateNodeHeight.tsx\n */\nexport default function calculateAutoSizeStyle(\n uiTextNode: HTMLTextAreaElement,\n options?: AutoSizeOptions\n): React.CSSProperties {\n const {\n minRows = null,\n maxRows = null,\n borderSize = 2,\n paddingSize = 8,\n } = options ?? {};\n\n if (!hiddenTextarea) {\n hiddenTextarea = document.createElement(\"textarea\");\n hiddenTextarea.setAttribute(\"tab-index\", \"-1\");\n hiddenTextarea.setAttribute(\"aria-hidden\", \"true\");\n document.body.appendChild(hiddenTextarea);\n }\n\n // equal style\n hiddenTextarea.setAttribute(\"style\", getMirroredStyle(uiTextNode));\n hiddenTextarea.value = uiTextNode.value || uiTextNode.placeholder || \"\";\n\n let minHeight: number | undefined;\n let maxHeight: number | undefined;\n let overflowY: React.CSSProperties[\"overflowY\"];\n\n let height = hiddenTextarea.scrollHeight + borderSize;\n\n if (minRows !== null || maxRows !== null) {\n const singleRowHeight =\n process.env.NODE_ENV === \"test\"\n ? 22\n : parseFloat(\n window\n .getComputedStyle(hiddenTextarea)\n .getPropertyValue(\"line-height\")\n );\n if (minRows !== null) {\n minHeight = singleRowHeight * minRows + paddingSize + borderSize;\n height = Math.max(minHeight, height);\n }\n if (maxRows !== null) {\n maxHeight = singleRowHeight * maxRows + paddingSize + borderSize;\n if (height <= maxHeight) {\n overflowY = \"hidden\";\n }\n height = Math.min(maxHeight, height);\n }\n }\n\n const style: React.CSSProperties = {\n height,\n overflowY,\n resize: \"none\",\n };\n\n if (minHeight) {\n style.minHeight = minHeight;\n }\n if (maxHeight) {\n style.maxHeight = maxHeight;\n }\n\n return style;\n}\n"],"mappings":"AAAA;;AAEA,SAASA,gBAAgB,QAAQ,uBAAuB;AAExD,IAAIC,cAA+C;AASnD;AACA;AACA;AACA;AACA,eAAe,SAASC,sBAAsBA,CAC5CC,UAA+B,EAC/BC,OAAyB,EACJ;EACrB,MAAM;IACJC,OAAO,GAAG,IAAI;IACdC,OAAO,GAAG,IAAI;IACdC,UAAU,GAAG,CAAC;IACdC,WAAW,GAAG;EAChB,CAAC,GAAGJ,OAAO,aAAPA,OAAO,cAAPA,OAAO,GAAI,CAAC,CAAC;EAEjB,IAAI,CAACH,cAAc,EAAE;IACnBA,cAAc,GAAGQ,QAAQ,CAACC,aAAa,CAAC,UAAU,CAAC;IACnDT,cAAc,CAACU,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC;IAC9CV,cAAc,CAACU,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC;IAClDF,QAAQ,CAACG,IAAI,CAACC,WAAW,CAACZ,cAAc,CAAC;EAC3C;;EAEA;EACAA,cAAc,CAACU,YAAY,CAAC,OAAO,EAAEX,gBAAgB,CAACG,UAAU,CAAC,CAAC;E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
|
+
max-height: none!important;
|
|
4
|
+
height: 0!important;
|
|
5
|
+
visibility: hidden!important;
|
|
6
|
+
overflow: hidden!important;
|
|
7
|
+
position: absolute!important;
|
|
8
|
+
z-index: -1000!important;
|
|
9
|
+
top: 0!important;
|
|
10
|
+
left: 0!important;
|
|
11
|
+
pointer-events: none!important;
|
|
12
|
+
`;
|
|
13
|
+
const SIZING_STYLE = ["letter-spacing", "line-height", "padding-top", "padding-bottom", "font-family", "font-weight", "font-size", "font-variant", "text-rendering", "text-transform", "width", "text-indent", "padding-left", "padding-right", "border-width", "box-sizing", "word-break", "white-space"];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 创建一个镜像元素,用于计算光标位置或文本尺寸。
|
|
17
|
+
*/
|
|
18
|
+
export function getMirroredStyle(element) {
|
|
19
|
+
const uiTextNodeStyle = window.getComputedStyle(element);
|
|
20
|
+
const sizingStyle = SIZING_STYLE.map(name => `${name}:${uiTextNodeStyle.getPropertyValue(name)}`).join(";");
|
|
21
|
+
return `${sizingStyle};${HIDDEN_STYLE}`;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=getMirroredStyle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMirroredStyle.js","names":["HIDDEN_STYLE","SIZING_STYLE","getMirroredStyle","element","uiTextNodeStyle","window","getComputedStyle","sizingStyle","map","name","getPropertyValue","join"],"sources":["../../../src/utils/getMirroredStyle.ts"],"sourcesContent":["const HIDDEN_STYLE = `\n min-height: 0!important;\n max-height: none!important;\n height: 0!important;\n visibility: hidden!important;\n overflow: hidden!important;\n position: absolute!important;\n z-index: -1000!important;\n top: 0!important;\n left: 0!important;\n pointer-events: none!important;\n`;\n\nconst SIZING_STYLE = [\n \"letter-spacing\",\n \"line-height\",\n \"padding-top\",\n \"padding-bottom\",\n \"font-family\",\n \"font-weight\",\n \"font-size\",\n \"font-variant\",\n \"text-rendering\",\n \"text-transform\",\n \"width\",\n \"text-indent\",\n \"padding-left\",\n \"padding-right\",\n \"border-width\",\n \"box-sizing\",\n \"word-break\",\n \"white-space\",\n];\n\n/**\n * 创建一个镜像元素,用于计算光标位置或文本尺寸。\n */\nexport function getMirroredStyle(element: HTMLElement) {\n const uiTextNodeStyle = window.getComputedStyle(element);\n const sizingStyle = SIZING_STYLE.map(\n (name) => `${name}:${uiTextNodeStyle.getPropertyValue(name)}`\n ).join(\";\");\n return `${sizingStyle};${HIDDEN_STYLE}`;\n}\n"],"mappings":"AAAA,MAAMA,YAAY,GAAG;AACrB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AAED,MAAMC,YAAY,GAAG,CACnB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,OAAO,EACP,aAAa,EACb,cAAc,EACd,eAAe,EACf,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,aAAa,CACd;;AAED;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAACC,OAAoB,EAAE;EACrD,MAAMC,eAAe,GAAGC,MAAM,CAACC,gBAAgB,CAACH,OAAO,CAAC;EACxD,MAAMI,WAAW,GAAGN,YAAY,CAACO,GAAG,CACjCC,IAAI,IAAK,GAAGA,IAAI,IAAIL,eAAe,CAACM,gBAAgB,CAACD,IAAI,CAAC,EAC7D,CAAC,CAACE,IAAI,CAAC,GAAG,CAAC;EACX,OAAO,GAAGJ,WAAW,IAAIP,YAAY,EAAE;AACzC","ignoreList":[]}
|
|
@@ -10,8 +10,18 @@ export interface TextareaAutoResizeProps extends React.DetailedHTMLProps<React.T
|
|
|
10
10
|
paddingSize?: number;
|
|
11
11
|
containerRef?: React.RefObject<HTMLElement>;
|
|
12
12
|
submitWhen?: "enter-without-shift" | "enter-with-mod";
|
|
13
|
+
desiredSelectionRef?: React.MutableRefObject<{
|
|
14
|
+
start: number;
|
|
15
|
+
end: number;
|
|
16
|
+
} | null>;
|
|
17
|
+
onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
|
18
|
+
/**
|
|
19
|
+
* The handler can return false to prevent default behavior (submit)
|
|
20
|
+
*/
|
|
21
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void | false;
|
|
13
22
|
}
|
|
14
23
|
export interface TextareaAutoResizeRef {
|
|
15
24
|
focus(): void;
|
|
25
|
+
element: HTMLTextAreaElement | null;
|
|
16
26
|
}
|
|
17
27
|
export declare const TextareaAutoResize: React.ForwardRefExoticComponent<Omit<TextareaAutoResizeProps, "ref"> & React.RefAttributes<TextareaAutoResizeRef>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getContentRectInTextarea(textarea: HTMLTextAreaElement, prefix: string, measureContent: string): DOMRect;
|
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next-shared/form",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"homepage": "https://github.com/easyops-cn/next-advanced-bricks/tree/master/shared/form",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"@next-core/test-next": "^2.0.0",
|
|
49
49
|
"concurrently": "^9.1.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "8697d099f9084bc1494cf85b3589094e53bcf5eb"
|
|
52
52
|
}
|