Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/components/reusableComponents/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class Form<P, S extends IFormState> extends Component<P, S> {
protected renderField = (props: IGeneratedFieldProps<any>, error: string): JSX.Element | null => {
const { active, value, options = [], name, noValidate, avatar, onFileUploadChange, touched, validationErrorStyle, dateFormat = DateFormatTypes.DateOnly } = props;

const fieldConfig: IFieldConfig = objectUtils.pick(props, 'name', 'value', 'type', 'placeholder', 'min', 'max', 'autoComplete', 'accept');
const fieldConfig: IFieldConfig = objectUtils.pick(props, 'name', 'value', 'type', 'placeholder', 'min', 'max', 'minLength', 'maxLength', 'autoComplete', 'accept');

const errorElement = this.displayError(touched, error, validationErrorStyle);

Expand Down
8 changes: 4 additions & 4 deletions src/services/DataPersister.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDataPersister } from 'types';
import { DataPersistKeys, IDataPersister } from 'types';

class DataPersister implements IDataPersister {
private static instance: DataPersister;
Expand All @@ -11,15 +11,15 @@ class DataPersister implements IDataPersister {
return DataPersister.instance;
}

public persistData<T>(key: string, data: T): void {
public persistData<T>(key: DataPersistKeys, data: T): void {
window.localStorage.setItem(key, JSON.stringify(data));
}

public deleteData(key: string): void {
public deleteData(key: DataPersistKeys): void {
window.localStorage.removeItem(key);
}

public readData<T>(key: string): T | null {
public readData<T>(key: DataPersistKeys): T | null {
return JSON.parse(window.localStorage.getItem(key) ?? 'null');
}

Expand Down
3 changes: 2 additions & 1 deletion src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ export enum DateFormatTypes {
}

export enum AcceptedFileUploadTypes {
All = '.',
Image = 'image/*',
Audio = 'audio/*',
Video = 'video/*',
ImageList = '.jpeg, .jpg, .png, .webp'
ImageList = '.jpeg, .jpg, .png, .webp',
}

export enum TypeOfDataTypes {
Expand Down
10 changes: 6 additions & 4 deletions src/types/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ChangeEventHandler } from 'react';
import { AcceptedFileUploadTypes, DateFormatTypes, FieldAutoCompleteValues, FieldTypes, InputTypes, ValidationErrorStyle } from './enums';
import { AcceptedFileUploadTypes, DataPersistKeys, DateFormatTypes, FieldAutoCompleteValues, FieldTypes, InputTypes, ValidationErrorStyle } from './enums';

export interface IDataPersister {
persistData<T>(key: string, data: T): void;
deleteData(key: string): void;
readData<T>(key: string): T | null;
persistData<T>(key: DataPersistKeys, data: T): void;
deleteData(key: DataPersistKeys): void;
readData<T>(key: DataPersistKeys): T | null;
clearData(): void;
}

Expand Down Expand Up @@ -40,6 +40,8 @@ interface IGeneratedFieldOptionalProps<T> {
label: string;
min: number;
max: number;
minLength: number;
maxLength: number;
validationlabel: string;
noValidate: boolean;
validationErrorStyle: ValidationErrorStyle;
Expand Down
8 changes: 5 additions & 3 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ import { FieldsFactory, FieldTypes, IGeneratedFieldProps, InputTypes, IObjectHas

export const validate = (fields: IObjectHasComputedProps): IObjectHasComputedProps =>
Object.values(fields).reduce(
(acc: IObjectHasComputedProps, { name, type, label, min, max, value, validationlabel, noValidate }): IObjectHasComputedProps => {
(acc: IObjectHasComputedProps, { name, type, label, min, max, minLength, maxLength, value, validationlabel, noValidate }): IObjectHasComputedProps => {
acc[name] = '';

if (noValidate) return acc;

const fieldName = validationlabel ?? label ?? name;
const minValue = min ?? minLength;
const maxValue = max ?? maxLength;

if (typeof value === 'string') value = value.trim();

if (type === 'number' && +value <= 0) acc[name] = `${ fieldName } must be greater than or equal to 1`;

if (type === 'email' && !/\S+@\S+\.\S+/.test(value)) acc[name] = `must be a valid email`;

if (value && (min || max) && (+value.length < min || +value.length > max)) {
if (value && (minValue || maxValue) && (+value.length < minValue || +value.length > maxValue)) {
acc[name] = `${ fieldName } field must be ${
min && max ? `between ${ min } and ${ max }` : min && !max ? `more than ${ min }` : max && !min ? `less than ${ max }` : ''
minValue && maxValue ? `between ${ minValue } and ${ maxValue }` : minValue && !maxValue ? `more than ${ minValue }` : maxValue && !minValue ? `less than ${ maxValue }` : ''
}`;
}

Expand Down