-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
457bc67
commit 90af632
Showing
5 changed files
with
139 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/vue/src/components/SpAmountInput/SpAmountInput.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<template> | ||
<input | ||
ref="inputRef" | ||
v-model="model" | ||
:placeholder="placeholder" | ||
type="text" | ||
inputmode="decimal" | ||
pattern="^[0-9]*[.,]?[0-9]*$" | ||
autocomplete="off" | ||
minlength="1" | ||
spellcheck="false" | ||
/> | ||
</template> | ||
<script lang="ts"> | ||
import { computed, defineComponent, PropType, ref } from 'vue' | ||
export default defineComponent({ | ||
name: 'SpAmountInput', | ||
props: { | ||
modelValue: { | ||
type: [String, Number], | ||
default: '' | ||
}, | ||
maxDecimals: { | ||
type: Number as PropType<number>, | ||
default: 0 | ||
}, | ||
placeholder: { | ||
type: String as PropType<string>, | ||
default: '0' | ||
} | ||
}, | ||
emits: ['update'], | ||
setup(props, { emit }) { | ||
let inputRef = ref(null) | ||
let format = (value: string): string => { | ||
let newValue: string = value | ||
// Replace commas | ||
newValue = newValue.replace(',', '.') | ||
// Disallow decimals if wished for | ||
newValue = props.maxDecimals > 0 ? newValue : newValue.replace('.', '') | ||
// Only numbers | ||
newValue = newValue.replace(/[^0-9.]/g, '') | ||
if (newValue.startsWith('.')) { | ||
newValue = '0' + newValue | ||
} | ||
if (newValue.split('').filter((char) => char === '.').length > 1) { | ||
// Remove subsequent separators | ||
newValue = newValue.replace(/(?<=\..*)\./g, '') | ||
} | ||
let [integerDigits, fractionDigits] = newValue.split('.') | ||
if (fractionDigits?.length > props.maxDecimals) { | ||
newValue = `${integerDigits}.${fractionDigits.slice( | ||
0, | ||
props.maxDecimals | ||
)}` | ||
} | ||
return newValue | ||
} | ||
let model = computed({ | ||
get: () => (props.modelValue || '').toString(), | ||
set: (value) => { | ||
if (!inputRef.value) { | ||
return | ||
} | ||
let currentValue = value | ||
while (parseFloat(currentValue) > Number.MAX_SAFE_INTEGER) { | ||
currentValue = currentValue.slice(0, -1) | ||
} | ||
let formatted = format(currentValue) | ||
emit('update', formatted) | ||
let inputHTMLEl = inputRef.value as HTMLInputElement | ||
inputHTMLEl.value = formatted | ||
} | ||
}) | ||
return { inputRef, format, model } | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { App as Application } from 'vue' | ||
|
||
import { registerComponent } from '../../utils/plugins/index' | ||
// @ts-ignore | ||
import C from './SpAmountInput.vue' | ||
|
||
export const Plugin = { | ||
install(vue: Application): void { | ||
registerComponent(vue, C) | ||
} | ||
} | ||
|
||
export default C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters