@danderson00/react-forms 0.0.25 → 0.0.26

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.
package/dist/input.js CHANGED
@@ -111,7 +111,7 @@ var _default = function _default(render) {
111
111
  if (value !== previousValueProp) {
112
112
  setFieldValue(name, applyValueTransforms(value));
113
113
  }
114
- }, [props]);
114
+ }, [props]); // TODO: refactor bubbling to prop pattern
115
115
 
116
116
  var onChange = function onChange() {
117
117
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
@@ -196,6 +196,25 @@ var _default = function _default(render) {
196
196
  }
197
197
  };
198
198
 
199
+ var onKeyDown = function onKeyDown() {
200
+ var onKeyDown = props.onKeyDown;
201
+
202
+ for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
203
+ args[_key4] = arguments[_key4];
204
+ }
205
+
206
+ var event = args[0];
207
+ var element = event.target;
208
+
209
+ if (props.numeric && !/[0-9-]/.exec(event.key) && ![null, 0, 8, 9, 13, 27, 35, 36, 37, 39, 46].includes(event.keyCode)) {
210
+ event.preventDefault();
211
+ }
212
+
213
+ if (onKeyDown) {
214
+ onKeyDown.apply(element, args);
215
+ }
216
+ };
217
+
199
218
  var updateValidationState = function updateValidationState() {
200
219
  setFieldValidated(true); // this is a little hacky, but the simplest way I could think of to determine the
201
220
  // validity of the actual input element. :invalid is also not supported by enzyme
@@ -250,7 +269,8 @@ var _default = function _default(render) {
250
269
  }, passThroughProps), valueProps), numericProps), typeProps), errorProps), {}, {
251
270
  onChange: onChange,
252
271
  onFocus: onFocus,
253
- onBlur: onBlur
272
+ onBlur: onBlur,
273
+ onKeyDown: onKeyDown
254
274
  });
255
275
 
256
276
  return render(finalProps);
@@ -22,7 +22,7 @@ var _default = function _default(props, options) {
22
22
  defaultValue = props.defaultValue;
23
23
 
24
24
  var applyValueTransforms = function applyValueTransforms(value) {
25
- return (props.type === 'number' || props.numeric) && !isNaN(+value) ? +value : value;
25
+ return (props.type === 'number' || props.numeric) && !isNaN(+value) && value !== '' ? +value : value;
26
26
  };
27
27
 
28
28
  var defaultType = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danderson00/react-forms",
3
- "version": "0.0.25",
3
+ "version": "0.0.26",
4
4
  "description": "Ultra simple, stateless, validated forms for use in React function components",
5
5
  "main": "dist/index.js",
6
6
  "module": "src/index.js",
package/src/input.js CHANGED
@@ -51,6 +51,7 @@ export default (render, options = {}) => function Input(props) {
51
51
  }
52
52
  }, [props])
53
53
 
54
+ // TODO: refactor bubbling to prop pattern
54
55
  const onChange = (...args) => {
55
56
  const [event] = args
56
57
  const { name, setFieldValue, onChange, submitOnChange } = props
@@ -110,6 +111,21 @@ export default (render, options = {}) => function Input(props) {
110
111
  }
111
112
  }
112
113
 
114
+ const onKeyDown = (...args) => {
115
+ const { onKeyDown } = props
116
+ const [event] = args
117
+ const element = event.target
118
+
119
+ if(props.numeric && !/[0-9-]/.exec(event.key) && ![null, 0, 8, 9, 13, 27, 35, 36, 37, 39, 46].includes(event.keyCode)) {
120
+ event.preventDefault()
121
+ }
122
+
123
+ if(onKeyDown) {
124
+ onKeyDown.apply(element, args)
125
+ }
126
+ }
127
+
128
+
113
129
  const updateValidationState = () => {
114
130
  setFieldValidated(true)
115
131
  // this is a little hacky, but the simplest way I could think of to determine the
@@ -136,7 +152,7 @@ export default (render, options = {}) => function Input(props) {
136
152
  const finalProps = {
137
153
  id, name, className,
138
154
  ...passThroughProps, ...valueProps, ...numericProps, ...typeProps, ...errorProps,
139
- onChange, onFocus, onBlur
155
+ onChange, onFocus, onBlur, onKeyDown
140
156
  }
141
157
 
142
158
  return render(finalProps)
package/src/inputTypes.js CHANGED
@@ -3,7 +3,7 @@ const resolveValue = (value, args) => typeof value === 'function' ? value.apply(
3
3
  export default (props, options) => {
4
4
  const { type, checked, value, defaultValue } = props
5
5
 
6
- const applyValueTransforms = value => ((props.type === 'number' || props.numeric) && !isNaN(+value)) ? +value : value
6
+ const applyValueTransforms = value => ((props.type === 'number' || props.numeric) && !isNaN(+value) && value !== '') ? +value : value
7
7
 
8
8
  const defaultType = {
9
9
  setInitialValue: ({ set }) => {