Skip to content

Commit

Permalink
Fix primefaces#4465: KeyFilter Android fix (primefaces#4469)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored May 31, 2023
1 parent b1f3f5c commit 57d92b4
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 17 deletions.
69 changes: 66 additions & 3 deletions components/doc/keyfilter/presetsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ export function PresetsDoc(props) {
const code = {
basic: `
<InputText keyfilter="int" />
<InputText keyfilter="pint" />
<InputText keyfilter="num" />
<InputText keyfilter="pnum" />
<InputText keyfilter="money" />
<InputText keyfilter="hex" />
<InputText keyfilter="alpha" />
<InputText keyfilter="alphanum" />
<InputText keyfilter="email" />
`,
javascript: `
import React from 'react';
Expand Down Expand Up @@ -39,7 +42,7 @@ export default function PresetsDemo() {
<InputText id="money" keyfilter="money" className="w-full" />
</div>
</div>
<div className="flex flex-wrap gap-3">
<div className="flex flex-wrap gap-3 mb-4">
<div className="flex-auto">
<label htmlFor="hex" className="font-bold block mb-2">
Hex
Expand All @@ -59,6 +62,26 @@ export default function PresetsDemo() {
<InputText id="alphanumeric" keyfilter="alphanum" className="w-full" />
</div>
</div>
<div className="flex flex-wrap gap-3">
<div className="flex-auto">
<label htmlFor="pint" className="font-bold block mb-2">
Positive Integer
</label>
<InputText id="pint" keyfilter="pint" className="w-full" />
</div>
<div className="flex-auto">
<label htmlFor="pnum" className="font-bold block mb-2">
Positive Number
</label>
<InputText id="pnum" keyfilter="pnum" className="w-full" />
</div>
<div className="flex-auto">
<label htmlFor="email" className="font-bold block mb-2">
Email
</label>
<InputText id="email" keyfilter="email" className="w-full" />
</div>
</div>
</div>
)
}
Expand Down Expand Up @@ -90,7 +113,7 @@ export default function PresetsDemo() {
<InputText id="money" keyfilter="money" className="w-full" />
</div>
</div>
<div className="flex flex-wrap gap-3">
<div className="flex flex-wrap gap-3 mb-4">
<div className="flex-auto">
<label htmlFor="hex" className="font-bold block mb-2">
Hex
Expand All @@ -110,6 +133,26 @@ export default function PresetsDemo() {
<InputText id="alphanumeric" keyfilter="alphanum" className="w-full" />
</div>
</div>
<div className="flex flex-wrap gap-3">
<div className="flex-auto">
<label htmlFor="pint" className="font-bold block mb-2">
Positive Integer
</label>
<InputText id="pint" keyfilter="pint" className="w-full" />
</div>
<div className="flex-auto">
<label htmlFor="pnum" className="font-bold block mb-2">
Positive Number
</label>
<InputText id="pnum" keyfilter="pnum" className="w-full" />
</div>
<div className="flex-auto">
<label htmlFor="email" className="font-bold block mb-2">
Email
</label>
<InputText id="email" keyfilter="email" className="w-full" />
</div>
</div>
</div>
)
}
Expand Down Expand Up @@ -144,7 +187,7 @@ export default function PresetsDemo() {
<InputText id="money" keyfilter="money" className="w-full" />
</div>
</div>
<div className="flex flex-wrap gap-3">
<div className="flex flex-wrap gap-3 mb-4">
<div className="flex-auto">
<label htmlFor="hex" className="font-bold block mb-2">
Hex
Expand All @@ -164,6 +207,26 @@ export default function PresetsDemo() {
<InputText id="alphanumeric" keyfilter="alphanum" className="w-full" />
</div>
</div>
<div className="flex flex-wrap gap-3">
<div className="flex-auto">
<label htmlFor="pint" className="font-bold block mb-2">
Positive Integer
</label>
<InputText id="pint" keyfilter="pint" className="w-full" />
</div>
<div className="flex-auto">
<label htmlFor="pnum" className="font-bold block mb-2">
Positive Number
</label>
<InputText id="pnum" keyfilter="pnum" className="w-full" />
</div>
<div className="flex-auto">
<label htmlFor="email" className="font-bold block mb-2">
Email
</label>
<InputText id="email" keyfilter="email" className="w-full" />
</div>
</div>
</div>
<DocSectionCode code={code} />
</>
Expand Down
15 changes: 12 additions & 3 deletions components/lib/inputtext/InputText.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export const InputText = React.memo(
}
};

const onBeforeInput = (event) => {
props.onBeforeInput && props.onBeforeInput(event);

if (props.keyfilter) {
KeyFilter.onBeforeInput(event, props.keyfilter, props.validateOnly);
}
};

const onInput = (event) => {
const target = event.target;
let validatePattern = true;
Expand Down Expand Up @@ -61,9 +69,10 @@ export const InputText = React.memo(
{
ref: elementRef,
className,
onInput: (e) => onInput(e),
onKeyDown: (e) => onKeyDown(e),
onPaste: (e) => onPaste(e)
onBeforeInput: onBeforeInput,
onInput: onInput,
onKeyDown: onKeyDown,
onPaste: onPaste
},
InputTextBase.getOtherProps(props),
ptm('root')
Expand Down
1 change: 1 addition & 0 deletions components/lib/inputtext/InputTextBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const InputTextBase = ComponentBase.extend({
validateOnly: false,
tooltip: null,
tooltipOptions: null,
onBeforeInput: null,
onInput: null,
onKeyDown: null,
onPaste: null,
Expand Down
9 changes: 9 additions & 0 deletions components/lib/inputtextarea/InputTextarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export const InputTextarea = React.memo(
}
};

const onBeforeInput = (event) => {
props.onBeforeInput && props.onBeforeInput(event);

if (props.keyfilter) {
KeyFilter.onBeforeInput(event, props.keyfilter, props.validateOnly);
}
};

const onPaste = (event) => {
props.onPaste && props.onPaste(event);

Expand Down Expand Up @@ -122,6 +130,7 @@ export const InputTextarea = React.memo(
onBlur: onBlur,
onKeyUp: onKeyUp,
onKeyDown: onKeyDown,
onBeforeInput: onBeforeInput,
onInput: onInput,
onPaste: onPaste
},
Expand Down
1 change: 1 addition & 0 deletions components/lib/inputtextarea/InputTextareaBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const InputTextareaBase = ComponentBase.extend({
keyfilter: null,
onBlur: null,
onFocus: null,
onBeforeInput: null,
onInput: null,
onKeyDown: null,
onKeyUp: null,
Expand Down
42 changes: 31 additions & 11 deletions components/lib/keyfilter/KeyFilter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DomHandler } from '../utils/Utils';

export const KeyFilter = {
/* eslint-disable */
DEFAULT_MASKS: {
Expand All @@ -17,26 +19,26 @@ export const KeyFilter = {
return KeyFilter.DEFAULT_MASKS[keyfilter] ? KeyFilter.DEFAULT_MASKS[keyfilter] : keyfilter;
},

onKeyPress(e, keyfilter, validateOnly) {
if (validateOnly) {
onBeforeInput(e, keyfilter, validateOnly) {
// android devices must use beforeinput https://stackoverflow.com/questions/36753548/keycode-on-android-is-always-229
if (validateOnly || !DomHandler.isAndroid()) {
return;
}

if (e.ctrlKey || e.altKey) {
this.validateKey(e, e.data, keyfilter);
},

onKeyPress(e, keyfilter, validateOnly) {
// non android devices use keydown
if (validateOnly || DomHandler.isAndroid()) {
return;
}

const isPrintableKey = e.key.length === 1;

if (!isPrintableKey) {
if (e.ctrlKey || e.altKey) {
return;
}

const regex = this.getRegex(keyfilter);

if (!regex.test(e.key)) {
e.preventDefault();
}
this.validateKey(e, e.key, keyfilter);
},

onPaste(e, keyfilter, validateOnly) {
Expand All @@ -57,6 +59,24 @@ export const KeyFilter = {
});
},

validateKey(e, key, keyfilter) {
if (key === null || key === undefined) {
return;
}

const isPrintableKey = key.length === 1;

if (!isPrintableKey) {
return;
}

const regex = this.getRegex(keyfilter);

if (!regex.test(key)) {
e.preventDefault();
}
},

validate(e, keyfilter) {
let value = e.target.value,
validatePattern = true;
Expand Down

0 comments on commit 57d92b4

Please sign in to comment.