Skip to content

Commit 300c484

Browse files
committed
fix: update method to calc numeric value
1 parent 4fc286f commit 300c484

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

examples/all-inputs/App.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<input-field name = "number" type = "number" label = "With Unit" :pretty="prettyUnit('meters')" />
7878
<input-field name = "number" type = "number" label = "With Suffix" suffix = "MHz" />
7979
<input-field name = "number" type = "number" disabled label = "With Suffix" suffix = "MHz" />
80-
80+
<input-field name = "number" type = "number" label = "Without step" suffix = "MHz" hide-step />
8181
<h2>Input Radio</h2>
8282

8383
<input-field name = "radio" type = "radio" :options = "optionsCheckbox"/>
@@ -140,12 +140,12 @@ function prettyUnit(unit: string) {
140140
if (typeof v !== "string" && typeof v !== "number") {
141141
return v;
142142
}
143-
v = String(v);
144-
v = v.replace(/[^0-9.]/g, '')
143+
let strValue = String(v);
144+
strValue = strValue.replace(/[^0-9.+-]/g, '')
145145
146-
v = k.format(Number(v));
146+
strValue = k.format(Number(v));
147147
148-
return `${v} ${unit}`
148+
return `${strValue} ${unit}`
149149
}
150150
}
151151

src/utils/parse-number.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import STORE from "../config/store";
2+
13
/**
24
* @description Попытка конвертации любой строки в число
35
* */
4-
export function parseNumber(data: unknown, defaultValue: number = 0) {
6+
7+
export function parseNumber(data: unknown, defaultValue: any = 0) {
8+
59
if (typeof data !== 'string') return defaultValue;
610

711
const parsedResult = new RegExp(/^([-+]?)([^.,+-]*)(.*)/g).exec(data);
812

9-
if (parsedResult === null) return 0;
13+
if (parsedResult === null) return defaultValue;
1014

1115
try {
1216
const parsedSting = [
@@ -20,14 +24,8 @@ export function parseNumber(data: unknown, defaultValue: number = 0) {
2024
if (parsedSting.length === 2) return defaultValue;
2125

2226
const result = Number.parseFloat(parsedSting)
23-
return Number.isNaN(result) ? 0 : result
27+
return Number.isNaN(result) ? defaultValue : result
2428
} catch (e) {
25-
return 0
29+
return defaultValue;
2630
}
27-
28-
2931
}
30-
31-
function test(str: string) {
32-
33-
}

src/widgets/inputs/input-number/widget-input-number.vue

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
>
99
<widget-number-step
1010
@step = "onStep"
11-
v-if = "!disabled"
11+
v-if = "!hideStep && !disabled "
1212
/>
1313
<input
1414
ref = "refInput"
1515
class = "input-number"
1616
type = "text"
1717
:value = "isFocused ? modelValue : useModify(() => props.pretty)(modelValue)"
18-
@input = "handleInput($event.target.value)"
18+
@input = "handleInput(($event.target as HTMLInputElement).value)"
1919
:disabled = "disabled"
2020
:autofocus="autofocus"
2121

@@ -36,6 +36,7 @@ import {StringModify, ValidationError} from "../../../types";
3636
import useModify from "../../../local-hooks/use-modify";
3737
import FieldWrap from "../field-wrap.vue";
3838
import {parseNumber} from "../../../utils/parse-number";
39+
import STORE from "../../../config/store";
3940
interface Props{
4041
step?: number | string,
4142
label?: string,
@@ -46,7 +47,8 @@ interface Props{
4647
name: string,
4748
pretty?: StringModify | StringModify[],
4849
suffix?: string,
49-
decimal?: boolean
50+
decimal?: boolean,
51+
hideStep?: boolean
5052
}
5153
const props = withDefaults(defineProps<Props>(), {
5254
step: 1
@@ -58,7 +60,7 @@ const emits = defineEmits<{
5860
}>()
5961
const refInput = ref<HTMLInputElement>()
6062
function handleInput(data: string | number) {
61-
const value = (typeof data !== "number") ? parseNumber(data) : data
63+
const value = (typeof data !== "number") ? parseNumber(data, STORE.cleanValue) : data
6264
6365
if (value !== props.modelValue) emits("update:modelValue", value);
6466
if (refInput.value) refInput.value.value = String(data).replace(/[^0-9.,+-]|/g, '')

src/widgets/inputs/input-text/input-text.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
:disabled="disabled"
1717
:autofocus="autofocus"
1818
:placeholder="placeholder"
19-
@input="onInput($event.target.value)"
19+
@input="onInput(($event.target as HTMLInputElement).value)"
2020
@focusin = "isFocused = true"
2121
@focusout = "isFocused = false"
2222
:name = "name"
@@ -31,6 +31,7 @@ import useModify from "../../../local-hooks/use-modify";
3131
import {StringModify, ValidationError} from "../../../types";
3232
import FieldWrap from "../field-wrap.vue";
3333
import {parseNumber} from "../../../utils/parse-number";
34+
import STORE from "../../../config/store";
3435
3536
const props = defineProps<{
3637
label?: string,
@@ -60,7 +61,7 @@ const emit = defineEmits<{
6061
const executePretty = useModify(() => props.pretty);
6162
const executeModify = useModify(
6263
() => [
63-
props.numeric ? parseNumber : undefined,
64+
props.numeric ? (s) => parseNumber(s, STORE.cleanValue) : undefined,
6465
...(Array.isArray(props.modify) ? props.modify : [props.modify])
6566
]
6667
)

0 commit comments

Comments
 (0)