react-hook-core 0.5.14 → 0.5.15
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/lib/com.js +1 -1
- package/lib/core.js +22 -12
- package/lib/state.js +58 -26
- package/package.json +1 -1
- package/src/com.ts +2 -2
- package/src/core.ts +20 -10
- package/src/state.ts +74 -28
package/lib/com.js
CHANGED
|
@@ -8,7 +8,7 @@ function Error(p) {
|
|
|
8
8
|
{},
|
|
9
9
|
React.createElement(
|
|
10
10
|
"header",
|
|
11
|
-
{},
|
|
11
|
+
{ className: "error-header" },
|
|
12
12
|
p.back && React.createElement("button", { type: "button", id: "backBtn", name: "backBtn", className: "btn-back", onClick: p.back }),
|
|
13
13
|
React.createElement("h2", {}, p.title),
|
|
14
14
|
),
|
package/lib/core.js
CHANGED
|
@@ -7,11 +7,23 @@ var resources = (function () {
|
|
|
7
7
|
resources.getSortId = function (field) {
|
|
8
8
|
return field + "Sort"
|
|
9
9
|
}
|
|
10
|
-
resources.
|
|
11
|
-
|
|
10
|
+
resources.normalizePhone = function (s) {
|
|
11
|
+
if (!s) {
|
|
12
|
+
return ""
|
|
13
|
+
}
|
|
14
|
+
var len = s.length
|
|
15
|
+
var buf = new Array(len)
|
|
16
|
+
var j = 0
|
|
17
|
+
for (var i = 0; i < len; i++) {
|
|
18
|
+
var c = s.charCodeAt(i)
|
|
19
|
+
if (c === 43 || (c >= 48 && c <= 57)) {
|
|
20
|
+
buf[j++] = s[i]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
12
24
|
}
|
|
13
|
-
resources.
|
|
14
|
-
return
|
|
25
|
+
resources.normalizeFax = function (fax) {
|
|
26
|
+
return resources.normalizePhone(fax)
|
|
15
27
|
}
|
|
16
28
|
resources._cache = {}
|
|
17
29
|
resources.cache = true
|
|
@@ -21,16 +33,14 @@ var resources = (function () {
|
|
|
21
33
|
resources.defaultLimit = 24
|
|
22
34
|
resources.limits = exports.pageSizes
|
|
23
35
|
resources.pageMaxSize = 7
|
|
24
|
-
resources.phone = / |\-|\.|\(|\)/g
|
|
25
|
-
resources.fax = / |\-|\.|\(|\)/g
|
|
26
36
|
return resources
|
|
27
37
|
})()
|
|
28
38
|
exports.resources = resources
|
|
29
|
-
function
|
|
30
|
-
return resources.
|
|
39
|
+
function normalizePhone(phone) {
|
|
40
|
+
return resources.normalizePhone(phone)
|
|
31
41
|
}
|
|
32
|
-
exports.
|
|
33
|
-
function
|
|
34
|
-
return resources.
|
|
42
|
+
exports.normalizePhone = normalizePhone
|
|
43
|
+
function normalizeFax(fax) {
|
|
44
|
+
return resources.normalizeFax(fax)
|
|
35
45
|
}
|
|
36
|
-
exports.
|
|
46
|
+
exports.normalizeFax = normalizeFax
|
package/lib/state.js
CHANGED
|
@@ -24,33 +24,65 @@ function getDecimalSeparator(ele) {
|
|
|
24
24
|
separator = form.getAttribute("data-decimal-separator")
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
return separator === ","
|
|
27
|
+
return separator === "," || separator === "٫" ? separator : "."
|
|
28
28
|
}
|
|
29
29
|
exports.getDecimalSeparator = getDecimalSeparator
|
|
30
|
+
function normalizeInteger(s) {
|
|
31
|
+
if (!s) {
|
|
32
|
+
return ""
|
|
33
|
+
}
|
|
34
|
+
var len = s.length
|
|
35
|
+
var buf = new Array(len)
|
|
36
|
+
var j = 0
|
|
37
|
+
for (var i = 0; i < len; i++) {
|
|
38
|
+
var c = s.charCodeAt(i)
|
|
39
|
+
if (c >= 48 && c <= 57) {
|
|
40
|
+
buf[j++] = s[i]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
44
|
+
}
|
|
45
|
+
exports.normalizeInteger = normalizeInteger
|
|
30
46
|
function removeSeparators(s) {
|
|
31
|
-
if (!s)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
if (!s) {
|
|
48
|
+
return ""
|
|
49
|
+
}
|
|
50
|
+
var len = s.length
|
|
51
|
+
var buffer = new Uint16Array(len)
|
|
52
|
+
var write = 0
|
|
53
|
+
for (var i = 0; i < len; i++) {
|
|
35
54
|
var c = s.charCodeAt(i)
|
|
36
|
-
if (
|
|
37
|
-
|
|
55
|
+
if ((c >= 48 && c <= 57) || c === 46) {
|
|
56
|
+
buffer[write++] = c
|
|
38
57
|
}
|
|
39
58
|
}
|
|
40
|
-
return
|
|
59
|
+
return String.fromCharCode.apply(null, buffer.subarray(0, write))
|
|
41
60
|
}
|
|
42
61
|
exports.removeSeparators = removeSeparators
|
|
43
|
-
|
|
44
|
-
|
|
62
|
+
function normalizeNumber(s) {
|
|
63
|
+
if (!s) {
|
|
64
|
+
return ""
|
|
65
|
+
}
|
|
66
|
+
var len = s.length
|
|
67
|
+
var buf = new Array(len)
|
|
68
|
+
var j = 0
|
|
69
|
+
for (var i = 0; i < len; i++) {
|
|
70
|
+
var c = s.charCodeAt(i)
|
|
71
|
+
if (c >= 48 && c <= 57) {
|
|
72
|
+
buf[j++] = s[i]
|
|
73
|
+
} else if (c === 44 || c === 1643) {
|
|
74
|
+
buf[j++] = "."
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
78
|
+
}
|
|
79
|
+
exports.normalizeNumber = normalizeNumber
|
|
45
80
|
function updateNumber(e, o, setObj, decimalSeparator, callback, formatStr) {
|
|
46
81
|
var ctrl = e.target
|
|
47
82
|
var v0 = formatStr ? formatStr(ctrl.value) : ctrl.value
|
|
48
83
|
var dataField = ctrl.getAttribute("data-field")
|
|
49
84
|
var field = dataField ? dataField : ctrl.name
|
|
50
|
-
var v = decimalSeparator === ","
|
|
51
|
-
if (v.indexOf(",") >= 0) {
|
|
52
|
-
v = v.replace(",", ".")
|
|
53
|
-
}
|
|
85
|
+
var v = decimalSeparator === "," || decimalSeparator === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
|
|
54
86
|
if (v === "" || v == null) {
|
|
55
87
|
reflect_1.setValue(o, field, undefined)
|
|
56
88
|
} else {
|
|
@@ -63,11 +95,15 @@ function updateNumber(e, o, setObj, decimalSeparator, callback, formatStr) {
|
|
|
63
95
|
}
|
|
64
96
|
}
|
|
65
97
|
exports.updateNumber = updateNumber
|
|
66
|
-
function formatAndUpdateState(e, o, setObj, formatStr, callback) {
|
|
67
|
-
updateState(e, o, setObj,
|
|
98
|
+
function formatAndUpdateState(e, o, setObj, formatStr, decimalSeparator, callback) {
|
|
99
|
+
updateState(e, o, setObj, decimalSeparator, formatStr, callback)
|
|
68
100
|
}
|
|
69
101
|
exports.formatAndUpdateState = formatAndUpdateState
|
|
70
|
-
function
|
|
102
|
+
function updateStateAndCallback(e, o, setObj, callback, decimalSeparator, formatStr) {
|
|
103
|
+
updateState(e, o, setObj, decimalSeparator, formatStr, callback)
|
|
104
|
+
}
|
|
105
|
+
exports.updateStateAndCallback = updateStateAndCallback
|
|
106
|
+
function updateState(e, o, setObj, decimalSeparator, formatStr, callback) {
|
|
71
107
|
var ctrl = e.target
|
|
72
108
|
var v0 = formatStr ? formatStr(ctrl.value) : ctrl.value
|
|
73
109
|
var dataField = ctrl.getAttribute("data-field")
|
|
@@ -121,14 +157,13 @@ function updateState(e, o, setObj, callback, formatStr) {
|
|
|
121
157
|
} else {
|
|
122
158
|
var datatype = ctrl.getAttribute("data-type")
|
|
123
159
|
if (datatype === "phone") {
|
|
124
|
-
var val = core_1.
|
|
160
|
+
var val = core_1.normalizePhone(v0)
|
|
125
161
|
reflect_1.setValue(o, field, val)
|
|
126
162
|
} else if (datatype === "fax") {
|
|
127
|
-
var val = core_1.
|
|
163
|
+
var val = core_1.normalizeFax(v0)
|
|
128
164
|
reflect_1.setValue(o, field, val)
|
|
129
165
|
} else if (datatype === "integer") {
|
|
130
|
-
|
|
131
|
-
var v = removeSeparators(v0)
|
|
166
|
+
var v = normalizeInteger(v0)
|
|
132
167
|
if (v === "" || v == null) {
|
|
133
168
|
reflect_1.setValue(o, field, undefined)
|
|
134
169
|
} else {
|
|
@@ -136,11 +171,8 @@ function updateState(e, o, setObj, callback, formatStr) {
|
|
|
136
171
|
reflect_1.setValue(o, field, val)
|
|
137
172
|
}
|
|
138
173
|
} else if (datatype === "number") {
|
|
139
|
-
var
|
|
140
|
-
var v =
|
|
141
|
-
if (v.indexOf(",") >= 0) {
|
|
142
|
-
v = v.replace(",", ".")
|
|
143
|
-
}
|
|
174
|
+
var sep = decimalSeparator ? decimalSeparator : getDecimalSeparator(ctrl)
|
|
175
|
+
var v = sep === "," || sep === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
|
|
144
176
|
if (v === "" || v == null) {
|
|
145
177
|
reflect_1.setValue(o, field, undefined)
|
|
146
178
|
} else {
|
package/package.json
CHANGED
package/src/com.ts
CHANGED
|
@@ -12,7 +12,7 @@ export function Error(p: ErrorProps) {
|
|
|
12
12
|
{},
|
|
13
13
|
React.createElement(
|
|
14
14
|
"header",
|
|
15
|
-
{},
|
|
15
|
+
{ className: "error-header" },
|
|
16
16
|
p.back && React.createElement("button", { type: "button", id: "backBtn", name: "backBtn", className: "btn-back", onClick: p.back }),
|
|
17
17
|
React.createElement("h2", {}, p.title),
|
|
18
18
|
),
|
|
@@ -20,7 +20,7 @@ export function Error(p: ErrorProps) {
|
|
|
20
20
|
)
|
|
21
21
|
/*
|
|
22
22
|
<div>
|
|
23
|
-
<header>
|
|
23
|
+
<header className="error-header">
|
|
24
24
|
<button type="button" id="backBtn" name="backBtn" className="btn-back" onClick={() => navigate(-1)}></button>
|
|
25
25
|
<h2>{resource.error_404_title}</h2>
|
|
26
26
|
</header>
|
package/src/core.ts
CHANGED
|
@@ -14,23 +14,33 @@ export class resources {
|
|
|
14
14
|
static defaultLimit = 24
|
|
15
15
|
static limits = pageSizes
|
|
16
16
|
static pageMaxSize = 7
|
|
17
|
-
static phone = / |\-|\.|\(|\)/g
|
|
18
|
-
static fax = / |\-|\.|\(|\)/g
|
|
19
17
|
static getSortId(field: string): string {
|
|
20
18
|
return field + "Sort"
|
|
21
19
|
}
|
|
22
|
-
static
|
|
23
|
-
|
|
20
|
+
static normalizePhone(s?: string | null): string {
|
|
21
|
+
if (!s) {
|
|
22
|
+
return ""
|
|
23
|
+
}
|
|
24
|
+
const len = s.length
|
|
25
|
+
const buf = new Array<string>(len)
|
|
26
|
+
let j = 0
|
|
27
|
+
for (let i = 0; i < len; i++) {
|
|
28
|
+
const c = s.charCodeAt(i)
|
|
29
|
+
if (c === 43 || (c >= 48 && c <= 57)) {
|
|
30
|
+
buf[j++] = s[i]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
24
34
|
}
|
|
25
|
-
static
|
|
26
|
-
return
|
|
35
|
+
static normalizeFax(fax?: string | null): string {
|
|
36
|
+
return resources.normalizePhone(fax)
|
|
27
37
|
}
|
|
28
38
|
}
|
|
29
|
-
export function
|
|
30
|
-
return resources.
|
|
39
|
+
export function normalizePhone(phone?: string | null): string {
|
|
40
|
+
return resources.normalizePhone(phone)
|
|
31
41
|
}
|
|
32
|
-
export function
|
|
33
|
-
return resources.
|
|
42
|
+
export function normalizeFax(fax?: string | null): string {
|
|
43
|
+
return resources.normalizeFax(fax)
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
export interface StringMap {
|
package/src/state.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ChangeEvent } from "react"
|
|
2
|
-
import {
|
|
2
|
+
import { normalizeFax, normalizePhone } from "./core"
|
|
3
3
|
import { setValue } from "./reflect"
|
|
4
4
|
|
|
5
5
|
export function getDecimalSeparator(ele: HTMLInputElement): string {
|
|
@@ -10,25 +10,62 @@ export function getDecimalSeparator(ele: HTMLInputElement): string {
|
|
|
10
10
|
separator = form.getAttribute("data-decimal-separator")
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
return separator === ","
|
|
13
|
+
return separator === "," || separator === "٫" ? separator : "."
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
export function normalizeInteger(s?: string | null): string {
|
|
16
|
+
if (!s) {
|
|
17
|
+
return ""
|
|
18
|
+
}
|
|
19
|
+
const len = s.length
|
|
20
|
+
const buf = new Array<string>(len)
|
|
21
|
+
let j = 0
|
|
22
|
+
for (let i = 0; i < len; i++) {
|
|
21
23
|
const c = s.charCodeAt(i)
|
|
24
|
+
if (c >= 48 && c <= 57) {
|
|
25
|
+
buf[j++] = s[i]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
29
|
+
}
|
|
30
|
+
export function removeSeparators(s?: string | null): string {
|
|
31
|
+
if (!s) {
|
|
32
|
+
return ""
|
|
33
|
+
}
|
|
34
|
+
const len = s.length
|
|
35
|
+
const buffer = new Uint16Array(len) // preallocate max possible
|
|
36
|
+
let write = 0
|
|
22
37
|
|
|
23
|
-
|
|
24
|
-
|
|
38
|
+
for (let i = 0; i < len; i++) {
|
|
39
|
+
const c = s.charCodeAt(i)
|
|
40
|
+
// '0'–'9' (48–57), '.' (46)
|
|
41
|
+
if ((c >= 48 && c <= 57) || c === 46) {
|
|
42
|
+
buffer[write++] = c
|
|
25
43
|
}
|
|
26
44
|
}
|
|
27
|
-
|
|
45
|
+
// Convert only the used portion to string
|
|
46
|
+
return String.fromCharCode.apply(null, buffer.subarray(0, write) as any)
|
|
28
47
|
}
|
|
48
|
+
// Keep digits 0–9 ; Replace , and ٫ (Arabic decimal separator) → . ; Remove everything else
|
|
49
|
+
export function normalizeNumber(s?: string | null): string {
|
|
50
|
+
if (!s) {
|
|
51
|
+
return ""
|
|
52
|
+
}
|
|
53
|
+
const len = s.length
|
|
54
|
+
const buf = new Array<string>(len)
|
|
55
|
+
let j = 0
|
|
56
|
+
for (let i = 0; i < len; i++) {
|
|
57
|
+
const c = s.charCodeAt(i)
|
|
29
58
|
|
|
30
|
-
|
|
31
|
-
|
|
59
|
+
if (c >= 48 && c <= 57) {
|
|
60
|
+
buf[j++] = s[i]
|
|
61
|
+
} else if (c === 44 || c === 1643) {
|
|
62
|
+
buf[j++] = "."
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
66
|
+
}
|
|
67
|
+
// const r1 = / |,|\$|€|£|¥|'|٬|،| /g
|
|
68
|
+
// const r2 = / |\.|\$|€|£|¥|'|٬|،| /g
|
|
32
69
|
export function updateNumber<T>(
|
|
33
70
|
e: ChangeEvent<HTMLInputElement>,
|
|
34
71
|
o: T,
|
|
@@ -41,10 +78,7 @@ export function updateNumber<T>(
|
|
|
41
78
|
const v0: string = formatStr ? formatStr(ctrl.value) : ctrl.value
|
|
42
79
|
const dataField = ctrl.getAttribute("data-field")
|
|
43
80
|
const field = dataField ? dataField : ctrl.name
|
|
44
|
-
let v = decimalSeparator === ","
|
|
45
|
-
if (v.indexOf(",") >= 0) {
|
|
46
|
-
v = v.replace(",", ".")
|
|
47
|
-
}
|
|
81
|
+
let v = decimalSeparator === "," || decimalSeparator === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
|
|
48
82
|
if (v === "" || v == null) {
|
|
49
83
|
setValue(o, field, undefined)
|
|
50
84
|
} else {
|
|
@@ -65,11 +99,12 @@ export function formatAndUpdateState<T>(
|
|
|
65
99
|
o: T,
|
|
66
100
|
setObj: (v: React.SetStateAction<T>) => void,
|
|
67
101
|
formatStr?: (s?: string) => string,
|
|
102
|
+
decimalSeparator?: string,
|
|
68
103
|
callback?: () => void,
|
|
69
104
|
) {
|
|
70
|
-
updateState(e, o, setObj,
|
|
105
|
+
updateState(e, o, setObj, decimalSeparator, formatStr, callback)
|
|
71
106
|
}
|
|
72
|
-
export function
|
|
107
|
+
export function updateStateAndCallback<T>(
|
|
73
108
|
e:
|
|
74
109
|
| ChangeEvent<HTMLInputElement>
|
|
75
110
|
| ChangeEvent<HTMLSelectElement>
|
|
@@ -78,7 +113,22 @@ export function updateState<T>(
|
|
|
78
113
|
o: T,
|
|
79
114
|
setObj: (v: React.SetStateAction<T>) => void,
|
|
80
115
|
callback?: () => void,
|
|
116
|
+
decimalSeparator?: string,
|
|
117
|
+
formatStr?: (s?: string) => string,
|
|
118
|
+
) {
|
|
119
|
+
updateState(e, o, setObj, decimalSeparator, formatStr, callback)
|
|
120
|
+
}
|
|
121
|
+
export function updateState<T>(
|
|
122
|
+
e:
|
|
123
|
+
| ChangeEvent<HTMLInputElement>
|
|
124
|
+
| ChangeEvent<HTMLSelectElement>
|
|
125
|
+
| ChangeEvent<HTMLTextAreaElement>
|
|
126
|
+
| ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>,
|
|
127
|
+
o: T,
|
|
128
|
+
setObj: (v: React.SetStateAction<T>) => void,
|
|
129
|
+
decimalSeparator?: string,
|
|
81
130
|
formatStr?: (s?: string) => string,
|
|
131
|
+
callback?: () => void,
|
|
82
132
|
) {
|
|
83
133
|
const ctrl = e.target
|
|
84
134
|
const v0: string = formatStr ? formatStr(ctrl.value) : ctrl.value
|
|
@@ -129,14 +179,13 @@ export function updateState<T>(
|
|
|
129
179
|
} else {
|
|
130
180
|
const datatype = ctrl.getAttribute("data-type")
|
|
131
181
|
if (datatype === "phone") {
|
|
132
|
-
const val =
|
|
182
|
+
const val = normalizePhone(v0)
|
|
133
183
|
setValue(o, field, val)
|
|
134
184
|
} else if (datatype === "fax") {
|
|
135
|
-
const val =
|
|
185
|
+
const val = normalizeFax(v0)
|
|
136
186
|
setValue(o, field, val)
|
|
137
187
|
} else if (datatype === "integer") {
|
|
138
|
-
|
|
139
|
-
let v = removeSeparators(v0)
|
|
188
|
+
let v = normalizeInteger(v0)
|
|
140
189
|
if (v === "" || v == null) {
|
|
141
190
|
setValue(o, field, undefined)
|
|
142
191
|
} else {
|
|
@@ -144,11 +193,8 @@ export function updateState<T>(
|
|
|
144
193
|
setValue(o, field, val)
|
|
145
194
|
}
|
|
146
195
|
} else if (datatype === "number") {
|
|
147
|
-
const
|
|
148
|
-
let v =
|
|
149
|
-
if (v.indexOf(",") >= 0) {
|
|
150
|
-
v = v.replace(",", ".")
|
|
151
|
-
}
|
|
196
|
+
const sep = decimalSeparator ? decimalSeparator : getDecimalSeparator(ctrl as HTMLInputElement)
|
|
197
|
+
let v = sep === "," || sep === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
|
|
152
198
|
if (v === "" || v == null) {
|
|
153
199
|
setValue(o, field, undefined)
|
|
154
200
|
} else {
|