use-redux-form
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -5,3 +5,3 @@ import _defineProperty from '@babel/runtime/helpers/defineProperty'; | ||
import { useSelector, shallowEqual } from 'react-redux'; | ||
import { complement, isNil, isEmpty, compose, map, cond, test, T, identity, filter, split, curry, hasPath, path, reduce, keys, is, F, find, values, defaultTo } from 'ramda'; | ||
import { complement, isNil, isEmpty, compose, map, cond, test, T, identity, filter, split, curry, reduce, keys, is, path, F, find, values, defaultTo } from 'ramda'; | ||
@@ -42,6 +42,2 @@ function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
} | ||
var extractByPath = curry(function (parsedPath, v) { | ||
// to get rid of negitive array index | ||
return hasPath(parsedPath, v) ? path(parsedPath, v) : ''; | ||
}); | ||
var excludeProps = curry(function (exclude, props) { | ||
@@ -88,3 +84,3 @@ return compose(reduce(function (acc, key) { | ||
var getFormState = compose(extractByPath, parsePath)(storePath); | ||
var getFormState = compose(path, parsePath)(storePath); | ||
var formState = useSelector(getFormState, shallowEqual); | ||
@@ -115,2 +111,7 @@ useEffect(function () { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!fieldPath || typeof fieldPath !== 'string') { | ||
throw genericError('invalid [fieldPath] given'); | ||
} | ||
var _options$isRequired = options.isRequired, | ||
@@ -123,29 +124,28 @@ isRequired = _options$isRequired === void 0 ? false : _options$isRequired, | ||
key = options.key; | ||
var computedKey = fieldPath; | ||
var computedValue = extractByPath(parsePath(fieldPath), formState); | ||
var parentPath = fieldPath; | ||
var parentRoot = path(parsePath(fieldPath), formState); | ||
var finalPath = parentPath; | ||
var finalValue = parentRoot; | ||
if (!fieldPath || typeof fieldPath !== 'string') { | ||
throw genericError('invalid [fieldPath] given'); | ||
} | ||
if (typeof key === 'function') { | ||
var computedPath = key(computedValue, formState); | ||
var hasNagativeIndex = /\[(-\d+?)\]/.test(computedPath); // to get rid of negitive array index | ||
var childPath = key(parentRoot, formState); | ||
var hasNagativeIndex = /\[(-\d+?)\]/.test(childPath); | ||
finalPath = "".concat(parentPath).concat(childPath); | ||
if (hasNagativeIndex) { | ||
computedValue = ''; | ||
finalValue = null; | ||
} else { | ||
computedKey = "".concat(fieldPath).concat(computedPath); | ||
computedValue = extractByPath(parsePath(computedKey), formState); | ||
finalPath = "".concat(parentPath).concat(childPath); | ||
finalValue = path(parsePath(finalPath), formState); | ||
} | ||
} | ||
computedValue = String(defaultTo('', computedValue)); | ||
finalValue = String(defaultTo('', finalValue)); | ||
var transformedValue = transform({ | ||
value: computedValue, | ||
name: computedKey | ||
value: finalValue, | ||
name: finalPath | ||
}); | ||
var isFalsy = isRequired && isNone(transformedValue); | ||
var errors = validate(formState) || {}; | ||
var isInvalid = isFalsy || !!errors[computedKey]; | ||
var isInvalid = isFalsy || !!errors[finalPath] || !!errors[name]; | ||
return excludeProps(exclude, { | ||
@@ -155,3 +155,3 @@ value: transformedValue, | ||
disabled: isDisabled, | ||
name: name || computedKey, | ||
name: name || finalPath, | ||
isInvalid: isInvalid, | ||
@@ -166,6 +166,6 @@ onChange: function onChange(evt) { | ||
value: transform({ | ||
name: computedKey, | ||
name: finalPath, | ||
value: value | ||
}, evt), | ||
name: computedKey | ||
name: finalPath | ||
}, evt); | ||
@@ -172,0 +172,0 @@ } |
@@ -50,6 +50,2 @@ 'use strict'; | ||
} | ||
var extractByPath = ramda.curry(function (parsedPath, v) { | ||
// to get rid of negitive array index | ||
return ramda.hasPath(parsedPath, v) ? ramda.path(parsedPath, v) : ''; | ||
}); | ||
var excludeProps = ramda.curry(function (exclude, props) { | ||
@@ -96,3 +92,3 @@ return ramda.compose(ramda.reduce(function (acc, key) { | ||
var getFormState = ramda.compose(extractByPath, parsePath)(storePath); | ||
var getFormState = ramda.compose(ramda.path, parsePath)(storePath); | ||
var formState = reactRedux.useSelector(getFormState, reactRedux.shallowEqual); | ||
@@ -123,2 +119,7 @@ react.useEffect(function () { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!fieldPath || typeof fieldPath !== 'string') { | ||
throw genericError('invalid [fieldPath] given'); | ||
} | ||
var _options$isRequired = options.isRequired, | ||
@@ -131,29 +132,28 @@ isRequired = _options$isRequired === void 0 ? false : _options$isRequired, | ||
key = options.key; | ||
var computedKey = fieldPath; | ||
var computedValue = extractByPath(parsePath(fieldPath), formState); | ||
var parentPath = fieldPath; | ||
var parentRoot = ramda.path(parsePath(fieldPath), formState); | ||
var finalPath = parentPath; | ||
var finalValue = parentRoot; | ||
if (!fieldPath || typeof fieldPath !== 'string') { | ||
throw genericError('invalid [fieldPath] given'); | ||
} | ||
if (typeof key === 'function') { | ||
var computedPath = key(computedValue, formState); | ||
var hasNagativeIndex = /\[(-\d+?)\]/.test(computedPath); // to get rid of negitive array index | ||
var childPath = key(parentRoot, formState); | ||
var hasNagativeIndex = /\[(-\d+?)\]/.test(childPath); | ||
finalPath = "".concat(parentPath).concat(childPath); | ||
if (hasNagativeIndex) { | ||
computedValue = ''; | ||
finalValue = null; | ||
} else { | ||
computedKey = "".concat(fieldPath).concat(computedPath); | ||
computedValue = extractByPath(parsePath(computedKey), formState); | ||
finalPath = "".concat(parentPath).concat(childPath); | ||
finalValue = ramda.path(parsePath(finalPath), formState); | ||
} | ||
} | ||
computedValue = String(ramda.defaultTo('', computedValue)); | ||
finalValue = String(ramda.defaultTo('', finalValue)); | ||
var transformedValue = transform({ | ||
value: computedValue, | ||
name: computedKey | ||
value: finalValue, | ||
name: finalPath | ||
}); | ||
var isFalsy = isRequired && isNone(transformedValue); | ||
var errors = validate(formState) || {}; | ||
var isInvalid = isFalsy || !!errors[computedKey]; | ||
var isInvalid = isFalsy || !!errors[finalPath] || !!errors[name]; | ||
return excludeProps(exclude, { | ||
@@ -163,3 +163,3 @@ value: transformedValue, | ||
disabled: isDisabled, | ||
name: name || computedKey, | ||
name: name || finalPath, | ||
isInvalid: isInvalid, | ||
@@ -174,6 +174,6 @@ onChange: function onChange(evt) { | ||
value: transform({ | ||
name: computedKey, | ||
name: finalPath, | ||
value: value | ||
}, evt), | ||
name: computedKey | ||
name: finalPath | ||
}, evt); | ||
@@ -180,0 +180,0 @@ } |
{ | ||
"name": "use-redux-form", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "React hook for using Redux store", | ||
@@ -5,0 +5,0 @@ "author": "Tony Jin <jsveron23@gmail.com>", |
@@ -215,3 +215,3 @@ # use-redux-form | ||
/** | ||
* Field name (if not given, it would use `form.username` as field name) | ||
* Field name (if not given, it would use `someParentState` as field name) | ||
* @type {String} | ||
@@ -218,0 +218,0 @@ * @default '' |
23375
321