@airplane/views
Version:
A React library for building Airplane views. Views components are optimized in style and functionality to produce internal apps that are easy to build and maintain.
79 lines (78 loc) • 1.47 kB
JavaScript
import { isEqual } from "lodash-es";
import { runValidate } from "./runValidation.js";
const inputReducer = (state, action) => {
switch (action.type) {
case "setValue": {
const {
value
} = action;
return {
...state,
value
};
}
case "setDisabled": {
let {
disabled
} = action;
if (disabled == null)
disabled = true;
return {
...state,
disabled
};
}
case "validate": {
const {
required,
validate
} = action;
const {
value,
errors
} = state;
const newErrors = runValidate(value, {
required,
validate
});
if (isEqual(errors, newErrors)) {
return state;
} else {
return {
...state,
errors: newErrors
};
}
}
case "setShowErrors": {
const {
showErrors
} = action;
if (showErrors === state.showErrors) {
return state;
}
return {
...state,
showErrors
};
}
case "reset": {
const {
initialValue,
initialDisabled
} = action;
return {
value: initialValue,
disabled: initialDisabled,
errors: [],
showErrors: false
};
}
default:
throw new Error("invalid action");
}
};
export {
inputReducer
};
//# sourceMappingURL=reducer.js.map