@dilicorp/ui 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,9 @@ export interface FormBuildItemInput extends Omit<FormBuilderBasicsItem, 'prefix'
14
14
  prefix?: string | React.ReactNode;
15
15
  fastField?: boolean;
16
16
  decimals?: number;
17
+ alpha?: boolean;
18
+ numbers?: boolean;
19
+ isEmail?: boolean;
17
20
  }
18
21
  declare type Props = Omit<FormBuildItemInput, 'component'> & {
19
22
  actions: FormikProps<any>;
@@ -6,7 +6,7 @@ import { InputMask } from './input-mask';
6
6
  import { InputPrefixSuffix } from './input-prefix-suffix';
7
7
  import { InputPercentage } from './input-percentage';
8
8
  export default function Input(props) {
9
- const { name, id = name, type = 'text', tabIndex = 0, actions, mask, multiple = false, currency, className, prefix, suffix, fastField = true, decimals = 2, ...elements } = props;
9
+ const { name, id = name, type = 'text', tabIndex = 0, actions, mask, multiple = false, currency, className, prefix, suffix, fastField = true, decimals = 2, alpha = true, numbers = true, isEmail = false, ...elements } = props;
10
10
  const validOptions = {
11
11
  alt: elements.alt,
12
12
  autoComplete: elements.autoComplete,
@@ -47,12 +47,55 @@ export default function Input(props) {
47
47
  actions.setFieldValue(name, newValue);
48
48
  }
49
49
  else {
50
- actions.setFieldValue(name, event.currentTarget.value);
50
+ if (type !== 'text' ||
51
+ currency ||
52
+ mask ||
53
+ prefix ||
54
+ suffix) {
55
+ actions.setFieldValue(name, event.currentTarget.value);
56
+ }
57
+ else {
58
+ actions.setFieldValue(name, sanitize(event.currentTarget.value));
59
+ }
51
60
  }
52
61
  }
53
62
  };
54
63
  const curValue = actions.values[name];
55
64
  const classes = classNames(className, 'form-control');
65
+ const replacePattern = isEmail
66
+ ? /[^a-zA-Z0-9._%+\-@]/g
67
+ : alpha
68
+ ? (numbers ? /[^A-Za-z0-9 \-_]/g : /[^A-Za-z \-_]/g)
69
+ : (!numbers ? /[0-9]/g : null);
70
+ const blockPattern = isEmail
71
+ ? /[^a-zA-Z0-9._%+\-@]/
72
+ : alpha
73
+ ? (numbers ? /[^A-Za-z0-9 \-_]/ : /[^A-Za-z \-_]/)
74
+ : (!numbers ? /[0-9]/ : null);
75
+ const sanitize = (value) => {
76
+ let result = replacePattern ? value.replace(replacePattern, '') : value;
77
+ if (alpha && !isEmail)
78
+ result = result.replace(/ {2,}/g, ' ');
79
+ return result;
80
+ };
81
+ const handleKeyDown = (blockPattern || (alpha && !isEmail))
82
+ ? (e) => {
83
+ var _a;
84
+ const { key, ctrlKey, metaKey } = e;
85
+ if (key.length === 1 && !ctrlKey && !metaKey) {
86
+ if (blockPattern && blockPattern.test(key)) {
87
+ e.preventDefault();
88
+ return;
89
+ }
90
+ if (alpha && !isEmail && key === ' ') {
91
+ const pos = (_a = e.currentTarget.selectionStart) !== null && _a !== void 0 ? _a : 0;
92
+ if (e.currentTarget.value[pos - 1] === ' ') {
93
+ e.preventDefault();
94
+ }
95
+ }
96
+ }
97
+ }
98
+ : undefined;
56
99
  const attrsInput = {
57
100
  ...validOptions,
58
101
  value: curValue,
@@ -82,7 +125,7 @@ export default function Input(props) {
82
125
  if (prefix || suffix)
83
126
  return React.createElement(InputPrefixSuffix, { ...{ attrs, suffix, prefix, handleChange } });
84
127
  const Tag = fastField ? FastField : Field;
85
- return (React.createElement(Tag, { ...attrsInput, as: multiple ? 'textarea' : 'input', readOnly: true, onFocus: (event) => {
128
+ return (React.createElement(Tag, { ...attrsInput, onKeyDown: handleKeyDown, as: multiple ? 'textarea' : 'input', readOnly: true, onFocus: (event) => {
86
129
  event.currentTarget.readOnly = false;
87
130
  } }));
88
131
  }