Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the directive of positive number #9451

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions shell/initialize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import '../plugins/extend-router';

import consolePlugin from '../plugins/console';
import intNumber from '../plugins/int-number';
import positiveIntNumber from '../plugins/positive-int-number.js';
import nuxtClientInit from '../plugins/nuxt-client-init';
import replaceAll from '../plugins/replaceall';
import backButton from '../plugins/back-button';
Expand Down Expand Up @@ -280,6 +281,10 @@ async function createApp(ssrContext, config = {}) {
await intNumber(app.context, inject);
}

if (process.client && typeof positiveIntNumber === 'function') {
await positiveIntNumber(app.context, inject);
}

if (process.client && typeof nuxtClientInit === 'function') {
await nuxtClientInit(app.context, inject);
}
Expand Down
7 changes: 5 additions & 2 deletions shell/plugins/int-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ export default Vue.directive('intNumber', {
el.addEventListener('keypress', (e) => {
e = e || window.event;
const charcode = typeof e.charCode === 'number' ? e.charCode : e.keyCode;
const re = /\d/;
const inputChar = String.fromCharCode(charcode);

if (!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey) {
// Allow digits, minus sign at the beginning, and Ctrl key combinations
const re = /^-?\d*$/;

if (!re.test(inputChar) && charcode > 9 && !e.ctrlKey) {
if (e.preventDefault) {
e.preventDefault();
} else {
Expand Down
19 changes: 19 additions & 0 deletions shell/plugins/positive-int-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Vue from 'vue';

export default Vue.directive('positiveIntNumber', {
inserted(el) {
el.addEventListener('keypress', (e) => {
e = e || window.event;
const charcode = typeof e.charCode === 'number' ? e.charCode : e.keyCode;
const re = /^\d+$/; // Use regex to match positive numbers

if (!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
});
}
});
Loading