@danderson00/react-forms 0.0.15 → 0.0.19
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/ErrorMessage.js +1 -1
- package/dist/Form.js +9 -2
- package/dist/fields.js +1 -1
- package/dist/index.js +2 -1
- package/dist/input.js +30 -1
- package/dist/submit.js +4 -2
- package/package.json +3 -3
- package/src/Form.js +6 -1
- package/src/input.js +21 -2
- package/src/submit.js +5 -2
package/dist/ErrorMessage.js
CHANGED
package/dist/Form.js
CHANGED
|
@@ -67,6 +67,11 @@ var _default = function _default(ContextProvider) {
|
|
|
67
67
|
error = _useState4[0],
|
|
68
68
|
setError = _useState4[1];
|
|
69
69
|
|
|
70
|
+
var _useState5 = (0, _react.useState)(),
|
|
71
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
72
|
+
valid = _useState6[0],
|
|
73
|
+
setValid = _useState6[1];
|
|
74
|
+
|
|
70
75
|
var form = /*#__PURE__*/(0, _react.createRef)();
|
|
71
76
|
|
|
72
77
|
var getFieldValue = function getFieldValue(path) {
|
|
@@ -84,9 +89,10 @@ var _default = function _default(ContextProvider) {
|
|
|
84
89
|
};
|
|
85
90
|
|
|
86
91
|
var setFieldValue = function setFieldValue(path, value) {
|
|
87
|
-
|
|
92
|
+
setFieldValues(function (previousValues) {
|
|
88
93
|
return (0, _objectPathImmutable.set)(previousValues, path, value);
|
|
89
94
|
});
|
|
95
|
+
setValid(form.current.checkValidity());
|
|
90
96
|
};
|
|
91
97
|
|
|
92
98
|
var onSubmit = function onSubmit(e, additionalValues) {
|
|
@@ -165,7 +171,8 @@ var _default = function _default(ContextProvider) {
|
|
|
165
171
|
setFieldValue: setFieldValue,
|
|
166
172
|
getFieldValue: getFieldValue,
|
|
167
173
|
getFieldValues: getFieldValues,
|
|
168
|
-
onSubmit: onSubmit
|
|
174
|
+
onSubmit: onSubmit,
|
|
175
|
+
valid: valid
|
|
169
176
|
}, mergedAdditionalContext)
|
|
170
177
|
})), error && /*#__PURE__*/(0, _react.createElement)(ErrorMessage, {
|
|
171
178
|
key: 'error',
|
package/dist/fields.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.Text = exports.RadioButton = exports.Radio = exports.Checkbox = void 0;
|
|
7
7
|
|
|
8
8
|
var _react = _interopRequireDefault(require("react"));
|
|
9
9
|
|
package/dist/index.js
CHANGED
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.Form = void 0;
|
|
6
7
|
Object.defineProperty(exports, "FormProvider", {
|
|
7
8
|
enumerable: true,
|
|
8
9
|
get: function get() {
|
|
9
10
|
return _Form["default"];
|
|
10
11
|
}
|
|
11
12
|
});
|
|
12
|
-
exports.
|
|
13
|
+
exports.wrapSubmit = exports.wrapInput = exports.useFormValues = exports.createRawConsumer = exports.context = exports.Textarea = exports.Submit = exports.Select = exports.Input = void 0;
|
|
13
14
|
|
|
14
15
|
var _react = require("react");
|
|
15
16
|
|
package/dist/input.js
CHANGED
|
@@ -15,7 +15,7 @@ var _inputTypes = _interopRequireDefault(require("./inputTypes"));
|
|
|
15
15
|
|
|
16
16
|
var _throttle2 = _interopRequireDefault(require("./throttle"));
|
|
17
17
|
|
|
18
|
-
var _excluded = ["type", "name", "value", "checked", "defaultValue", "numeric", "onSubmit", "registerFieldValidator", "setFieldValue", "getFieldValue", "getFieldValues", "submitOnChange", "submitOnBlur", "throttle"];
|
|
18
|
+
var _excluded = ["type", "name", "value", "checked", "defaultValue", "numeric", "onSubmit", "registerFieldValidator", "setFieldValue", "getFieldValue", "getFieldValues", "submitOnChange", "submitOnBlur", "selectOnFocus", "throttle", "valid"];
|
|
19
19
|
|
|
20
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
21
21
|
|
|
@@ -170,6 +170,32 @@ var _default = function _default(render) {
|
|
|
170
170
|
}
|
|
171
171
|
};
|
|
172
172
|
|
|
173
|
+
var onFocus = function onFocus() {
|
|
174
|
+
var selectOnFocus = props.selectOnFocus,
|
|
175
|
+
onFocus = props.onFocus;
|
|
176
|
+
|
|
177
|
+
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
178
|
+
args[_key3] = arguments[_key3];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
var event = args[0];
|
|
182
|
+
var element = event.target;
|
|
183
|
+
|
|
184
|
+
var selectText = function selectText() {
|
|
185
|
+
element.selectionStart = 0;
|
|
186
|
+
element.selectionEnd = element.value.length;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
if (selectOnFocus) {
|
|
190
|
+
selectText();
|
|
191
|
+
setTimeout(selectText);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (onFocus) {
|
|
195
|
+
onFocus.apply(element, args);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
173
199
|
var updateValidationState = function updateValidationState() {
|
|
174
200
|
setFieldValidated(true); // this is a little hacky, but the simplest way I could think of to determine the
|
|
175
201
|
// validity of the actual input element. :invalid is also not supported by enzyme
|
|
@@ -198,7 +224,9 @@ var _default = function _default(render) {
|
|
|
198
224
|
getFieldValues = props.getFieldValues,
|
|
199
225
|
submitOnChange = props.submitOnChange,
|
|
200
226
|
submitOnBlur = props.submitOnBlur,
|
|
227
|
+
selectOnFocus = props.selectOnFocus,
|
|
201
228
|
_throttle = props.throttle,
|
|
229
|
+
valid = props.valid,
|
|
202
230
|
passThroughProps = _objectWithoutProperties(props, _excluded);
|
|
203
231
|
|
|
204
232
|
var valueProps = getValueProps({
|
|
@@ -218,6 +246,7 @@ var _default = function _default(render) {
|
|
|
218
246
|
className: className
|
|
219
247
|
}, passThroughProps), valueProps), typeProps), errorProps), {}, {
|
|
220
248
|
onChange: onChange,
|
|
249
|
+
onFocus: onFocus,
|
|
221
250
|
onBlur: onBlur
|
|
222
251
|
});
|
|
223
252
|
|
package/dist/submit.js
CHANGED
|
@@ -9,7 +9,7 @@ exports["default"] = void 0;
|
|
|
9
9
|
|
|
10
10
|
var _react = _interopRequireWildcard(require("react"));
|
|
11
11
|
|
|
12
|
-
var _excluded = ["onSubmit", "registerFieldValidator", "setFieldValue", "getFieldValue", "getFieldValues"];
|
|
12
|
+
var _excluded = ["onSubmit", "registerFieldValidator", "setFieldValue", "getFieldValue", "getFieldValues", "valid", "disableInvalid"];
|
|
13
13
|
|
|
14
14
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
15
15
|
|
|
@@ -62,11 +62,13 @@ var _default = function _default(render) {
|
|
|
62
62
|
setFieldValue = props.setFieldValue,
|
|
63
63
|
getFieldValue = props.getFieldValue,
|
|
64
64
|
getFieldValues = props.getFieldValues,
|
|
65
|
+
valid = props.valid,
|
|
66
|
+
disableInvalid = props.disableInvalid,
|
|
65
67
|
propsToPass = _objectWithoutProperties(props, _excluded);
|
|
66
68
|
|
|
67
69
|
var finalProps = _objectSpread(_objectSpread({}, propsToPass), {}, {
|
|
68
70
|
onClick: onClick,
|
|
69
|
-
disabled: executing
|
|
71
|
+
disabled: executing || props.disableInvalid && !props.valid
|
|
70
72
|
});
|
|
71
73
|
|
|
72
74
|
return render(finalProps);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@danderson00/react-forms",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
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",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"\\.css$": "<rootDir>/test/setup/styleMock.js"
|
|
33
33
|
},
|
|
34
34
|
"setupFilesAfterEnv": [
|
|
35
|
-
"<rootDir>test/setup/
|
|
35
|
+
"<rootDir>test/setup/enzyme.js"
|
|
36
36
|
]
|
|
37
37
|
},
|
|
38
38
|
"babel": {
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"component"
|
|
50
50
|
],
|
|
51
51
|
"author": "danderson00",
|
|
52
|
-
"license": "
|
|
52
|
+
"license": "MIT",
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
package/src/Form.js
CHANGED
|
@@ -6,6 +6,7 @@ export default (ContextProvider, additionalContext = {}, ErrorMessage) => (
|
|
|
6
6
|
const [fieldValues, setFieldValues] = useState(props.values || {})
|
|
7
7
|
const [fieldValidators, registerFieldValidator] = useReducer((validators, validator) => [...validators, validator], [])
|
|
8
8
|
const [error, setError] = useState()
|
|
9
|
+
const [valid, setValid] = useState()
|
|
9
10
|
const form = createRef()
|
|
10
11
|
|
|
11
12
|
const getFieldValue = path => get(fieldValues, path)
|
|
@@ -15,7 +16,10 @@ export default (ContextProvider, additionalContext = {}, ErrorMessage) => (
|
|
|
15
16
|
{}
|
|
16
17
|
)
|
|
17
18
|
|
|
18
|
-
const setFieldValue = (path, value) =>
|
|
19
|
+
const setFieldValue = (path, value) => {
|
|
20
|
+
setFieldValues(previousValues => set(previousValues, path, value))
|
|
21
|
+
setValid(form.current.checkValidity())
|
|
22
|
+
}
|
|
19
23
|
|
|
20
24
|
const onSubmit = (e, additionalValues) => {
|
|
21
25
|
fieldValidators.forEach(setFieldValidated => setFieldValidated())
|
|
@@ -88,6 +92,7 @@ export default (ContextProvider, additionalContext = {}, ErrorMessage) => (
|
|
|
88
92
|
getFieldValue,
|
|
89
93
|
getFieldValues,
|
|
90
94
|
onSubmit,
|
|
95
|
+
valid,
|
|
91
96
|
...mergedAdditionalContext
|
|
92
97
|
}
|
|
93
98
|
}),
|
package/src/input.js
CHANGED
|
@@ -91,6 +91,25 @@ export default (render, options = {}) => function Input(props) {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
const onFocus = (...args) => {
|
|
95
|
+
const { selectOnFocus, onFocus } = props
|
|
96
|
+
const [event] = args
|
|
97
|
+
const element = event.target
|
|
98
|
+
|
|
99
|
+
const selectText = () => {
|
|
100
|
+
element.selectionStart = 0
|
|
101
|
+
element.selectionEnd = element.value.length
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if(selectOnFocus) {
|
|
105
|
+
selectText()
|
|
106
|
+
setTimeout(selectText)
|
|
107
|
+
}
|
|
108
|
+
if(onFocus) {
|
|
109
|
+
onFocus.apply(element, args)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
94
113
|
const updateValidationState = () => {
|
|
95
114
|
setFieldValidated(true)
|
|
96
115
|
// this is a little hacky, but the simplest way I could think of to determine the
|
|
@@ -105,7 +124,7 @@ export default (render, options = {}) => function Input(props) {
|
|
|
105
124
|
const {
|
|
106
125
|
type, name, value, checked, defaultValue, numeric,
|
|
107
126
|
onSubmit, registerFieldValidator, setFieldValue, getFieldValue, getFieldValues,
|
|
108
|
-
submitOnChange, submitOnBlur, throttle,
|
|
127
|
+
submitOnChange, submitOnBlur, selectOnFocus, throttle, valid,
|
|
109
128
|
...passThroughProps
|
|
110
129
|
} = props
|
|
111
130
|
const valueProps = getValueProps({ currentValue: getFieldValue(name) })
|
|
@@ -116,7 +135,7 @@ export default (render, options = {}) => function Input(props) {
|
|
|
116
135
|
const finalProps = {
|
|
117
136
|
id, name, className,
|
|
118
137
|
...passThroughProps, ...valueProps, ...typeProps, ...errorProps,
|
|
119
|
-
onChange, onBlur
|
|
138
|
+
onChange, onFocus, onBlur
|
|
120
139
|
}
|
|
121
140
|
|
|
122
141
|
return render(finalProps)
|
package/src/submit.js
CHANGED
|
@@ -14,12 +14,15 @@ export default render => function Submit(props) {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const {
|
|
17
|
+
const {
|
|
18
|
+
onSubmit, registerFieldValidator, setFieldValue, getFieldValue, getFieldValues,
|
|
19
|
+
valid, disableInvalid, ...propsToPass
|
|
20
|
+
} = props
|
|
18
21
|
|
|
19
22
|
const finalProps = {
|
|
20
23
|
...propsToPass,
|
|
21
24
|
onClick,
|
|
22
|
-
disabled: executing
|
|
25
|
+
disabled: executing || (props.disableInvalid && !props.valid)
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
return render(finalProps)
|