Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion dist/hooks/use-form.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,9 +48,17 @@ var actionTypes = {

exports.actionTypes = actionTypes;

/**
* Is field registered.
*/
function isFieldRegistered(action, state) {
return action.type === actionTypes.REGISTER_FIELD && state.fields.meta[action.payload.field];
}
/**
* Values reducer.
*/


var valuesReducer = function valuesReducer(state, action) {
var payload = action.payload,
type = action.type;
Expand All@@ -66,6 +74,10 @@ var valuesReducer = function valuesReducer(state, action) {
newValue = payload.value;
}

if (newValue === state[payload.field]) {
return state;
}

return _objectSpread({}, state, _defineProperty({}, payload.field, newValue));
}

Expand All@@ -85,6 +97,8 @@ var valuesReducer = function valuesReducer(state, action) {


var metaReducer = function metaReducer(state, action) {
var _state$payload$field;

var payload = action.payload,
type = action.type;

Expand All@@ -96,6 +110,10 @@ var metaReducer = function metaReducer(state, action) {
})));

case actionTypes.SET_FIELD_VALUE:
if ((_state$payload$field = state[payload.field]) === null || _state$payload$field === void 0 ? void 0 : _state$payload$field.dirty) {
return state;
}

return _objectSpread({}, state, _defineProperty({}, payload.field, _objectSpread({}, state[payload.field], {
dirty: true
})));
Expand DownExpand Up@@ -209,11 +227,15 @@ function isSubmittingReducer(state, action) {

var formReducer = function formReducer(validate, stateReducer) {
return function (state, action) {
if (isFieldRegistered(action, state)) {
return state;
}

var fieldsValues = valuesReducer(state.fields.values, action);
var fieldsMeta = metaReducer(state.fields.meta, action);
var isSubmitting = isSubmittingReducer(state.isSubmitting, action);
var submitStatus = submitStatusReducer(state.submitStatus, action);
var fieldsErrors = errorsReducer({
var fieldsErrors = fieldsValues === state.fields.values ? state.fields.errors : errorsReducer({
action: action,
state: state.fields.errors,
validate: validate,
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/use-form.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -113,6 +113,10 @@ const valuesReducer = (state, action) => {
newValue = payload.value;
}

if (newValue === state[payload.field]) {
return state;
}

return {
...state,
[payload.field]: newValue
Expand DownExpand Up@@ -152,6 +156,10 @@ const metaReducer = (state, action) => {
};

case actionTypes.SET_FIELD_VALUE:
if (state[payload.field]?.dirty) {
return state;
}

return {
...state,
[payload.field]: {
Expand DownExpand Up@@ -294,7 +302,7 @@ const formReducer = (validate: Object => FieldErrors, stateReducer: (state: Form
const fieldsMeta = metaReducer(state.fields.meta, action);
const isSubmitting = isSubmittingReducer(state.isSubmitting, action);
const submitStatus = submitStatusReducer(state.submitStatus, action);
const fieldsErrors = errorsReducer({
const fieldsErrors = fieldsValues === state.fields.values ? state.fields.errors : errorsReducer({
action,
state: state.fields.errors,
validate,
Expand Down