Skip to content

Commit

Permalink
Fixed #12412
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Dec 12, 2022
1 parent 3521bb7 commit 50b5dcf
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Craft CMS 3.x

## Unreleased

- Fixed a bug where it wasn’t possible to enter `0` in Number fields and numeric column cells within editable tables, using certain keyboard layouts. ([#12412](https://github.com/craftcms/cms/issues/12412))
- Added `Craft.filterInputVal()`.
- Added `Craft.filterNumberInputVal()`.

## 3.7.61 - 2022-11-17

- Fixed an error that occurred if an arrow function was passed to the `|sort` Twig filter. ([#12334](https://github.com/craftcms/cms/issues/12334))
Expand Down
21 changes: 3 additions & 18 deletions src/fields/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,9 @@ protected function inputHtml($value, ElementInterface $element = null): string

$js = <<<JS
(function() {
\$('#$namespacedId').on('keydown', ev => {
if (
!Garnish.isCtrlKeyPressed(ev) &&
![
9, // tab,
13, // return / enter
27, // esc
8, 46, // backspace, delete
37, 38, 39, 40, // arrows
173, 189, 109, // minus, subtract
190, 110, // period, decimal
188, // comma
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, // 0-9
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, // numpad 0-9
].includes(ev.which)
) {
ev.preventDefault();
}
const input = \$('#$namespacedId');
input.on('input', () => {
Craft.filterNumberInputVal(input);
});
})();
JS;
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js.map

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/web/assets/cp/src/js/Craft.js
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,38 @@ $.extend(Craft, {
setFocusWithin: function (container) {
Garnish.setFocusWithin(container);
},

/**
* Reduces an input’s value to characters that match the given regex pattern.
* @param {jQuery|HTMLElement} input
* @param {RegExp} regex
*/
filterInputVal: function (input, regex) {
const $input = $(input);
const val = $input.val();
let selectionStart = $input[0].selectionStart;
let newVal = '';
for (let i = 0; i < val.length; i++) {
if (val[i].match(regex)) {
newVal += val[i];
} else if (i < selectionStart) {
selectionStart--;
}
}
if (newVal !== val) {
$input.val(newVal);
$input[0].setSelectionRange(selectionStart, selectionStart);
}
},

/**
* Reduces an input’s value to numeric characters.
* @param {jQuery|HTMLElement} input
* @param {RegExp} regex
*/
filterNumberInputVal: function (input) {
this.filterInputVal(input, /[0-9.,\-]/);
},
});

// -------------------------------------------
Expand Down
27 changes: 5 additions & 22 deletions src/web/assets/cp/src/js/EditableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,15 +792,6 @@ Craft.EditableTable.Row = Garnish.Base.extend(
}
return;
}

// Was this an invalid number character?
if (
ev.data.type === 'number' &&
!ctrl &&
!Craft.inArray(keyCode, Craft.EditableTable.Row.numericKeyCodes)
) {
ev.preventDefault();
}
},

handlePaste: function (ev) {
Expand All @@ -820,22 +811,13 @@ Craft.EditableTable.Row = Garnish.Base.extend(
return;
}

var safeValue;

if (ev.data.type === 'number') {
// Only grab the number at the beginning of the value (if any)
var match = ev.currentTarget.value.match(/^\s*(-?[\d\\.]*)/);

if (match !== null) {
safeValue = match[1];
} else {
safeValue = '';
}
} else {
// Just strip any newlines
safeValue = ev.currentTarget.value.replace(/[\r\n]/g, '');
Craft.filterNumberInputVal(ev.currentTarget);
return;
}

// Strip any newlines
const safeValue = ev.currentTarget.value.replace(/[\r\n]/g, '');
if (safeValue !== ev.currentTarget.value) {
ev.currentTarget.value = safeValue;
}
Expand Down Expand Up @@ -870,6 +852,7 @@ Craft.EditableTable.Row = Garnish.Base.extend(
},
},
{
/** @deprecated */
numericKeyCodes: [
9 /* (tab) */, 8 /* (delete) */, 37, 38, 39, 40 /* (arrows) */, 45,
91 /* (minus) */, 46, 190 /* period */, 48, 49, 50, 51, 52, 53, 54, 55,
Expand Down

0 comments on commit 50b5dcf

Please sign in to comment.