react-bfm
Version:
A basic field / form manager for React using hooks
71 lines • 3.77 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createContext } from 'react';
import { defaultDirtyCheck, mapFieldValueAndError, validateFieldName, validateNamespace } from './helpers';
import { getFieldState, getNamespaceState, initFieldState, removeField, createGetSnapshotFieldState, createGetSnapshotNamespaceState, createSubscribeToField, createSubscribeToNamespace, updateFieldStateWithCallback } from './state';
import { FIELD_KEY_INITIAL_VALUE, FIELD_KEY_INITIAL_VALUE_ERROR, FIELD_KEY_DIRTY, FIELD_KEY_ERROR, FIELD_KEY_FOCUS, FIELD_KEY_TOUCHED, FIELD_KEY_VALUE, FIELD_KEY_VALUE_ON_FOCUS } from './constants/field-keys';
var focusField = function focusField(namespace, fieldName) {
updateFieldStateWithCallback(namespace, fieldName, function (currentState) {
var _ref;
return _ref = {}, _ref[FIELD_KEY_FOCUS] = true, _ref[FIELD_KEY_VALUE_ON_FOCUS] = currentState[FIELD_KEY_VALUE], _ref;
});
};
var changeField = function changeField(namespace, fieldName, value, error, dirtyCheck) {
if (dirtyCheck === void 0) {
dirtyCheck = defaultDirtyCheck;
}
updateFieldStateWithCallback(namespace, fieldName, function (currentState) {
var _extends2;
return _extends((_extends2 = {}, _extends2[FIELD_KEY_DIRTY] = dirtyCheck(value, currentState[FIELD_KEY_VALUE_ON_FOCUS]), _extends2), mapFieldValueAndError(value, error));
});
};
var blurField = function blurField(namespace, fieldName) {
updateFieldStateWithCallback(namespace, fieldName, function () {
var _ref2;
return _ref2 = {}, _ref2[FIELD_KEY_FOCUS] = false, _ref2[FIELD_KEY_TOUCHED] = true, _ref2[FIELD_KEY_VALUE_ON_FOCUS] = null, _ref2;
});
};
var initField = function initField(namespace, fieldName, value, error) {
if (process.env.NODE_ENV !== 'production') {
if (!validateNamespace(namespace)) {
throw new Error('Expected string with a minimal length of 1 for `namespace`');
}
if (!validateFieldName(fieldName)) {
throw new Error('Expected string with a minimal length of 1 for `fieldName`');
}
}
initFieldState(namespace, fieldName, value, error);
};
/**
* sets initial value only when the field is not touched and not focused
* this way you can still change the input value after first rendering
*/
var initialValueField = function initialValueField(namespace, fieldName, initialValue, error) {
return updateFieldStateWithCallback(namespace, fieldName, function (currentState) {
var _extends3;
// only update value and error when field is not touched and not focused
var updateState = !currentState[FIELD_KEY_TOUCHED] && !currentState[FIELD_KEY_FOCUS] && currentState[FIELD_KEY_VALUE] !== initialValue ? mapFieldValueAndError(initialValue, error) : {};
// update error if value is still default
if (currentState[FIELD_KEY_VALUE] === initialValue && currentState[FIELD_KEY_ERROR] !== error) {
updateState.error = error;
updateState.valid = !error;
}
return _extends({}, updateState, (_extends3 = {}, _extends3[FIELD_KEY_INITIAL_VALUE] = initialValue, _extends3[FIELD_KEY_INITIAL_VALUE_ERROR] = error, _extends3));
});
};
/**
* @deprecated Will be removed in v3.0.0. This is an internal implementation detail and should not be used directly.
*/
export var BFMHooksContext = createContext({
blurField: blurField,
changeField: changeField,
initialValueField: initialValueField,
focusField: focusField,
getFieldState: getFieldState,
getNamespaceState: getNamespaceState,
initField: initField,
removeField: removeField,
createGetSnapshotFieldState: createGetSnapshotFieldState,
createGetSnapshotNamespaceState: createGetSnapshotNamespaceState,
createSubscribeToField: createSubscribeToField,
createSubscribeToNamespace: createSubscribeToNamespace
});