@danderson00/react-forms 0.0.24 → 0.0.27

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/Form.js CHANGED
@@ -73,6 +73,11 @@ var _default = function _default(ContextProvider) {
73
73
  setValid = _useState6[1];
74
74
 
75
75
  var form = /*#__PURE__*/(0, _react.createRef)();
76
+ (0, _react.useEffect)(function () {
77
+ if (form.current) {
78
+ setValid(form.current.checkValidity());
79
+ }
80
+ }, [form.current]);
76
81
 
77
82
  var getFieldValue = function getFieldValue(path) {
78
83
  return (0, _objectPathImmutable.get)(fieldValues, path);
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
@@ -232,6 +251,9 @@ var _default = function _default(render) {
232
251
  var valueProps = getValueProps({
233
252
  currentValue: getFieldValue(name)
234
253
  });
254
+ var numericProps = numeric && {
255
+ type: 'number'
256
+ };
235
257
  var typeProps = type && {
236
258
  type: type
237
259
  };
@@ -240,14 +262,15 @@ var _default = function _default(render) {
240
262
  };
241
263
  var className = (fieldValidated ? 'validated ' : '') + (props.className || '') || undefined;
242
264
 
243
- var finalProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({
265
+ var finalProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({
244
266
  id: id,
245
267
  name: name,
246
268
  className: className
247
- }, passThroughProps), valueProps), typeProps), errorProps), {}, {
269
+ }, passThroughProps), valueProps), numericProps), typeProps), errorProps), {}, {
248
270
  onChange: onChange,
249
271
  onFocus: onFocus,
250
- onBlur: onBlur
272
+ onBlur: onBlur,
273
+ onKeyDown: onKeyDown
251
274
  });
252
275
 
253
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.24",
3
+ "version": "0.0.27",
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/Form.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createElement, createRef, useReducer, useState } from 'react'
1
+ import { createElement, createRef, useEffect, useReducer, useState } from 'react'
2
2
  import { get, set } from 'object-path-immutable'
3
3
 
4
4
  export default (ContextProvider, additionalContext = {}, ErrorMessage) => (
@@ -9,6 +9,12 @@ export default (ContextProvider, additionalContext = {}, ErrorMessage) => (
9
9
  const [valid, setValid] = useState()
10
10
  const form = createRef()
11
11
 
12
+ useEffect(() => {
13
+ if(form.current) {
14
+ setValid(form.current.checkValidity())
15
+ }
16
+ }, [form.current])
17
+
12
18
  const getFieldValue = path => get(fieldValues, path)
13
19
  const getFieldValues = () => fieldValues
14
20
  const mapFieldValues = target => Object.keys(target).reduce(
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
@@ -128,14 +144,15 @@ export default (render, options = {}) => function Input(props) {
128
144
  ...passThroughProps
129
145
  } = props
130
146
  const valueProps = getValueProps({ currentValue: getFieldValue(name) })
147
+ const numericProps = (numeric && { type: 'number' })
131
148
  const typeProps = (type && { type })
132
149
  const errorProps = (passErrorProp && { error })
133
150
  const className = ((fieldValidated ? 'validated ' : '') + (props.className || '') || undefined)
134
151
 
135
152
  const finalProps = {
136
153
  id, name, className,
137
- ...passThroughProps, ...valueProps, ...typeProps, ...errorProps,
138
- onChange, onFocus, onBlur
154
+ ...passThroughProps, ...valueProps, ...numericProps, ...typeProps, ...errorProps,
155
+ onChange, onFocus, onBlur, onKeyDown
139
156
  }
140
157
 
141
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 }) => {