@havue/ip-input 1.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 HappyPedestrian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # DragAndScale
2
+
3
+ [documents](https://happypedestrian.github.io/havue/components/ip-input.html)
@@ -0,0 +1,164 @@
1
+ import './style.css';
2
+ import { defineComponent, ref, watch, createElementBlock, openBlock, normalizeClass, Fragment, renderList, createElementVNode, createCommentVNode } from "vue";
3
+ const withInstall = (main, extra) => {
4
+ main.install = (app) => {
5
+ for (const comp of [main, ...Object.values({})]) {
6
+ app.component(comp.name, comp);
7
+ }
8
+ };
9
+ return main;
10
+ };
11
+ function getValidIPItemValue(val) {
12
+ const num = parseInt(val + "");
13
+ return isNaN(num) ? "" : num < 0 ? "0" : num > 255 ? "255" : num + "";
14
+ }
15
+ function getRange(el) {
16
+ const ret = {
17
+ begin: null,
18
+ end: null,
19
+ result: null
20
+ };
21
+ ret.begin = el.selectionStart || 0;
22
+ ret.end = el.selectionEnd || 0;
23
+ ret.result = el.value.substring(ret.begin, ret.end);
24
+ el.focus();
25
+ return ret;
26
+ }
27
+ const _hoisted_1 = ["value", "disabled", "onInput", "onKeydown", "onPaste"];
28
+ const _hoisted_2 = { key: 0 };
29
+ const _sfc_main = /* @__PURE__ */ defineComponent({
30
+ ...{
31
+ name: "HvIpInput"
32
+ },
33
+ __name: "IpInput",
34
+ props: {
35
+ modelValue: { default: "..." },
36
+ disabled: { type: Boolean, default: false }
37
+ },
38
+ emits: ["update:model-value"],
39
+ setup(__props, { emit: __emit }) {
40
+ const emits = __emit;
41
+ const props = __props;
42
+ const inputRefs = ref([]);
43
+ const values = ref([]);
44
+ watch(
45
+ () => props.modelValue,
46
+ (value) => {
47
+ value = value + "";
48
+ const strs = value.split(".");
49
+ values.value = Array(4).fill("").map((_, i) => {
50
+ return getValidIPItemValue(strs[i] + "");
51
+ });
52
+ },
53
+ {
54
+ immediate: true
55
+ }
56
+ );
57
+ function onIPChange() {
58
+ const ip = values.value.map((val) => getValidIPItemValue(val)).join(".");
59
+ return emits("update:model-value", ip);
60
+ }
61
+ function handleChange(e, i) {
62
+ var _a;
63
+ if (props.disabled) {
64
+ return;
65
+ }
66
+ const curValue = e.target.value;
67
+ const num = getValidIPItemValue(curValue);
68
+ values.value[i] = num;
69
+ e.target.value = num;
70
+ onIPChange();
71
+ if (num === "") {
72
+ return e.preventDefault();
73
+ }
74
+ if (String(num).length === 3 && i < 3) {
75
+ (_a = inputRefs.value[i + 1]) == null ? void 0 : _a.focus();
76
+ }
77
+ }
78
+ function handleKeyDown(e, i) {
79
+ let domId = i;
80
+ const { end } = getRange(e.target);
81
+ switch (e.keyCode) {
82
+ case 37:
83
+ case 8: {
84
+ if (end === 0 && i > 0) {
85
+ domId = i - 1;
86
+ }
87
+ break;
88
+ }
89
+ case 39: {
90
+ if (end === e.target.value.length && i < 3) {
91
+ domId = i + 1;
92
+ }
93
+ break;
94
+ }
95
+ case 110:
96
+ case 190: {
97
+ e.preventDefault();
98
+ if (i < 3) {
99
+ domId = i + 1;
100
+ }
101
+ break;
102
+ }
103
+ }
104
+ const inputEl = inputRefs.value[domId];
105
+ inputEl.focus();
106
+ if (domId < i) {
107
+ const len = inputEl.value.length;
108
+ inputEl.selectionStart = inputEl.selectionEnd = len;
109
+ } else if (domId > i) {
110
+ inputEl.selectionStart = inputEl.selectionEnd = 0;
111
+ }
112
+ }
113
+ function handlePaste(e, i) {
114
+ if (!e.clipboardData || !e.clipboardData.getData) {
115
+ return;
116
+ }
117
+ const pasteData = e.clipboardData.getData("text/plain");
118
+ if (!pasteData) {
119
+ return;
120
+ }
121
+ const value = pasteData.split(".").map((v) => getValidIPItemValue(v));
122
+ if (value.length !== 4 - i) {
123
+ return;
124
+ }
125
+ if (!value.every((item) => item !== "")) {
126
+ return;
127
+ }
128
+ value.forEach((val, j) => {
129
+ values.value[i + j] = val + "";
130
+ });
131
+ onIPChange();
132
+ return e.preventDefault();
133
+ }
134
+ return (_ctx, _cache) => {
135
+ return openBlock(), createElementBlock("div", {
136
+ class: normalizeClass(["hv-ip-input", props.disabled ? "is-disabled" : ""])
137
+ }, [
138
+ (openBlock(true), createElementBlock(Fragment, null, renderList(values.value, (value, i) => {
139
+ return openBlock(), createElementBlock("div", {
140
+ class: "hv-ip-input-item",
141
+ key: i
142
+ }, [
143
+ createElementVNode("input", {
144
+ ref_for: true,
145
+ ref_key: "inputRefs",
146
+ ref: inputRefs,
147
+ type: "text",
148
+ value,
149
+ disabled: props.disabled,
150
+ onInput: (e) => handleChange(e, i),
151
+ onKeydown: (e) => handleKeyDown(e, i),
152
+ onPaste: (e) => handlePaste(e, i)
153
+ }, null, 40, _hoisted_1),
154
+ i !== 3 ? (openBlock(), createElementBlock("i", _hoisted_2, ".")) : createCommentVNode("", true)
155
+ ]);
156
+ }), 128))
157
+ ], 2);
158
+ };
159
+ }
160
+ });
161
+ const HvIpInput = withInstall(_sfc_main);
162
+ export {
163
+ HvIpInput
164
+ };
@@ -0,0 +1,166 @@
1
+ (function(global, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("vue")) : typeof define === "function" && define.amd ? define(["exports", "vue"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["ip-input"] = {}, global.Vue));
3
+ })(this, function(exports2, vue) {
4
+ "use strict";
5
+ const withInstall = (main, extra) => {
6
+ main.install = (app) => {
7
+ for (const comp of [main, ...Object.values({})]) {
8
+ app.component(comp.name, comp);
9
+ }
10
+ };
11
+ return main;
12
+ };
13
+ function getValidIPItemValue(val) {
14
+ const num = parseInt(val + "");
15
+ return isNaN(num) ? "" : num < 0 ? "0" : num > 255 ? "255" : num + "";
16
+ }
17
+ function getRange(el) {
18
+ const ret = {
19
+ begin: null,
20
+ end: null,
21
+ result: null
22
+ };
23
+ ret.begin = el.selectionStart || 0;
24
+ ret.end = el.selectionEnd || 0;
25
+ ret.result = el.value.substring(ret.begin, ret.end);
26
+ el.focus();
27
+ return ret;
28
+ }
29
+ const _hoisted_1 = ["value", "disabled", "onInput", "onKeydown", "onPaste"];
30
+ const _hoisted_2 = { key: 0 };
31
+ const _sfc_main = /* @__PURE__ */ vue.defineComponent({
32
+ ...{
33
+ name: "HvIpInput"
34
+ },
35
+ __name: "IpInput",
36
+ props: {
37
+ modelValue: { default: "..." },
38
+ disabled: { type: Boolean, default: false }
39
+ },
40
+ emits: ["update:model-value"],
41
+ setup(__props, { emit: __emit }) {
42
+ const emits = __emit;
43
+ const props = __props;
44
+ const inputRefs = vue.ref([]);
45
+ const values = vue.ref([]);
46
+ vue.watch(
47
+ () => props.modelValue,
48
+ (value) => {
49
+ value = value + "";
50
+ const strs = value.split(".");
51
+ values.value = Array(4).fill("").map((_, i) => {
52
+ return getValidIPItemValue(strs[i] + "");
53
+ });
54
+ },
55
+ {
56
+ immediate: true
57
+ }
58
+ );
59
+ function onIPChange() {
60
+ const ip = values.value.map((val) => getValidIPItemValue(val)).join(".");
61
+ return emits("update:model-value", ip);
62
+ }
63
+ function handleChange(e, i) {
64
+ var _a;
65
+ if (props.disabled) {
66
+ return;
67
+ }
68
+ const curValue = e.target.value;
69
+ const num = getValidIPItemValue(curValue);
70
+ values.value[i] = num;
71
+ e.target.value = num;
72
+ onIPChange();
73
+ if (num === "") {
74
+ return e.preventDefault();
75
+ }
76
+ if (String(num).length === 3 && i < 3) {
77
+ (_a = inputRefs.value[i + 1]) == null ? void 0 : _a.focus();
78
+ }
79
+ }
80
+ function handleKeyDown(e, i) {
81
+ let domId = i;
82
+ const { end } = getRange(e.target);
83
+ switch (e.keyCode) {
84
+ case 37:
85
+ case 8: {
86
+ if (end === 0 && i > 0) {
87
+ domId = i - 1;
88
+ }
89
+ break;
90
+ }
91
+ case 39: {
92
+ if (end === e.target.value.length && i < 3) {
93
+ domId = i + 1;
94
+ }
95
+ break;
96
+ }
97
+ case 110:
98
+ case 190: {
99
+ e.preventDefault();
100
+ if (i < 3) {
101
+ domId = i + 1;
102
+ }
103
+ break;
104
+ }
105
+ }
106
+ const inputEl = inputRefs.value[domId];
107
+ inputEl.focus();
108
+ if (domId < i) {
109
+ const len = inputEl.value.length;
110
+ inputEl.selectionStart = inputEl.selectionEnd = len;
111
+ } else if (domId > i) {
112
+ inputEl.selectionStart = inputEl.selectionEnd = 0;
113
+ }
114
+ }
115
+ function handlePaste(e, i) {
116
+ if (!e.clipboardData || !e.clipboardData.getData) {
117
+ return;
118
+ }
119
+ const pasteData = e.clipboardData.getData("text/plain");
120
+ if (!pasteData) {
121
+ return;
122
+ }
123
+ const value = pasteData.split(".").map((v) => getValidIPItemValue(v));
124
+ if (value.length !== 4 - i) {
125
+ return;
126
+ }
127
+ if (!value.every((item) => item !== "")) {
128
+ return;
129
+ }
130
+ value.forEach((val, j) => {
131
+ values.value[i + j] = val + "";
132
+ });
133
+ onIPChange();
134
+ return e.preventDefault();
135
+ }
136
+ return (_ctx, _cache) => {
137
+ return vue.openBlock(), vue.createElementBlock("div", {
138
+ class: vue.normalizeClass(["hv-ip-input", props.disabled ? "is-disabled" : ""])
139
+ }, [
140
+ (vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(values.value, (value, i) => {
141
+ return vue.openBlock(), vue.createElementBlock("div", {
142
+ class: "hv-ip-input-item",
143
+ key: i
144
+ }, [
145
+ vue.createElementVNode("input", {
146
+ ref_for: true,
147
+ ref_key: "inputRefs",
148
+ ref: inputRefs,
149
+ type: "text",
150
+ value,
151
+ disabled: props.disabled,
152
+ onInput: (e) => handleChange(e, i),
153
+ onKeydown: (e) => handleKeyDown(e, i),
154
+ onPaste: (e) => handlePaste(e, i)
155
+ }, null, 40, _hoisted_1),
156
+ i !== 3 ? (vue.openBlock(), vue.createElementBlock("i", _hoisted_2, ".")) : vue.createCommentVNode("", true)
157
+ ]);
158
+ }), 128))
159
+ ], 2);
160
+ };
161
+ }
162
+ });
163
+ const HvIpInput = withInstall(_sfc_main);
164
+ exports2.HvIpInput = HvIpInput;
165
+ Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
166
+ });
package/dist/style.css ADDED
@@ -0,0 +1,26 @@
1
+ .hv-ip-input {
2
+ --hv-ip-input-width: 30px;
3
+ display: inline-block;
4
+ padding: 6px 8px;
5
+ font-size: 14px;
6
+ line-height: 19px;
7
+ outline: none;
8
+ border: 1px solid #373d48;
9
+ border-radius: 4px;
10
+ }
11
+ .hv-ip-input input {
12
+ width: var(--hv-ip-input-width);
13
+ text-align: center;
14
+ outline: none;
15
+ background-color: transparent;
16
+ border: none;
17
+ }
18
+ .hv-ip-input.is-disabled {
19
+ cursor: not-allowed;
20
+ }
21
+ .hv-ip-input.is-disabled input {
22
+ cursor: not-allowed;
23
+ }
24
+ .hv-ip-input .hv-ip-input-item {
25
+ display: inline-block;
26
+ }
@@ -0,0 +1,13 @@
1
+ type __VLS_Props = {
2
+ modelValue: string | undefined;
3
+ disabled?: boolean;
4
+ };
5
+ declare const _default: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {} & {
6
+ "update:model-value": (value: string) => any;
7
+ }, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
8
+ "onUpdate:model-value"?: ((value: string) => any) | undefined;
9
+ }>, {
10
+ modelValue: string | undefined;
11
+ disabled: boolean;
12
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
13
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import type { SFCWithInstall } from '@havue/utils';
2
+ import IpInput from './IpInput.vue';
3
+ export declare const HvIpInput: SFCWithInstall<typeof IpInput>;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * IP每一项值是否有效 | IP Whether each entry value is valid
3
+ * @param val 单个IP值 | Single IP value
4
+ */
5
+ export declare function getValidIPItemValue(val: string | number): string;
6
+ /**
7
+ * 光标在输入框文本中的位置 | The position of the cursor within the text of the input field
8
+ * @param { HTMLInputElement } el
9
+ */
10
+ export declare function getRange(el: HTMLInputElement): {
11
+ begin: number | null;
12
+ end: number | null;
13
+ result: string | null;
14
+ };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@havue/ip-input",
3
+ "version": "1.2.0",
4
+ "description": "Ip address input components for Vue3",
5
+ "keywords": [
6
+ "havue",
7
+ "components",
8
+ "ip",
9
+ "address",
10
+ "input",
11
+ "vue3"
12
+ ],
13
+ "license": "MIT",
14
+ "homepage": "https://happypedestrian.github.io/havue/guide/",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/HappyPedestrian/havue.git"
18
+ },
19
+ "dependencies": {
20
+ "@havue/shared": "^1.2.0"
21
+ },
22
+ "devDependencies": {
23
+ "vue": "^3.3.0"
24
+ },
25
+ "peerDependencies": {
26
+ "vue": "^3.3.0"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "registry": "https://registry.npmjs.org/"
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "main": "./dist/ip-input.umd.js",
36
+ "module": "./dist/ip-input.mjs",
37
+ "types": "./dist/types/src/index.d.ts",
38
+ "exports": {
39
+ ".": {
40
+ "require": "./dist/ip-input.umd.js",
41
+ "import": "./dist/ip-input.mjs",
42
+ "types": "./dist/types/src/index.d.ts"
43
+ }
44
+ },
45
+ "scripts": {
46
+ "build": "vite build --mode package"
47
+ }
48
+ }