Add common component and helpers

This commit is contained in:
2023-10-26 16:49:16 +07:00
parent f9f5990a4a
commit bd407bb933
10 changed files with 140 additions and 5 deletions
+30
View File
@@ -0,0 +1,30 @@
import { TKeyValue, TPropValues, TStateSchema } from '../globals/types';
export const VALUE = 'value';
export const ERROR = 'error';
export const REQUIRED_FIELD_ERROR = 'This is required field';
function isBool(value: unknown) {
return typeof value === 'boolean';
}
const isObject = (value: unknown) => {
return typeof value === 'object' && value !== null;
};
const isRequired = (value: string | number, isRequired: unknown) => {
if (!value && isRequired) return REQUIRED_FIELD_ERROR;
return '';
};
const getPropValues = (stateSchema: TStateSchema, prop?: TPropValues) => {
return Object.keys(stateSchema).reduce((field, key) => {
field[key] = isBool(prop)
? prop
: stateSchema[key][prop as Exclude<TPropValues, boolean>];
return field;
}, {} as TKeyValue);
};
export { isObject, isRequired, getPropValues };