Skip to content
This repository has been archived by the owner on Aug 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from Nevoss/dev
Browse files Browse the repository at this point in the history
Version PR - v0.4.0
  • Loading branch information
Nevoss authored Nov 26, 2018
2 parents 450de44 + 654feb6 commit d07cc2b
Show file tree
Hide file tree
Showing 41 changed files with 4,814 additions and 5,769 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
npm-debug.log
.idea
lib
4 changes: 0 additions & 4 deletions babel.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
preset: 'ts-jest',
}
63 changes: 63 additions & 0 deletions lib/core/Errors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ErrorsStack } from "../types";
export declare class Errors {
/**
* Errors stack, holds all the form errors
*/
$errors: ErrorsStack;
/**
* Construct the Errors class with errors
*
* @param errors
*/
constructor(errors?: ErrorsStack);
/**
* Record errors to the ErrorsStack
*
* @param errors
*/
record(errors: ErrorsStack): Errors;
/**
* Append errors to the ErrorsStack
*
* @param errors
*/
append(errors: ErrorsStack): Errors;
/**
* checks if fieldKey exists in the ErrorsStack
*
* @param fieldKey
*/
has(fieldKey: string): boolean;
/**
* Returns array of errors for specific field
*
* @param fieldKey
* @param defaultValue
*/
get(fieldKey: string, defaultValue?: any): string[];
/**
* returns first error of specific field key
*
* @param fieldKey
* @param defaultValue
*/
getFirst<T>(fieldKey: string, defaultValue?: T): T | string;
/**
* Returns all the ErrorsStack
*/
all(): ErrorsStack;
/**
* delete a key from ErrorsStack
*
* @param fieldKey
*/
clearField(fieldKey: string): Errors;
/**
* check if there is any error in the ErrorsStack
*/
any(): boolean;
/**
* Clear the ErrorsStack object
*/
clear(): Errors;
}
92 changes: 92 additions & 0 deletions lib/core/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Errors {
/**
* Construct the Errors class with errors
*
* @param errors
*/
constructor(errors = {}) {
this.record(errors);
}
/**
* Record errors to the ErrorsStack
*
* @param errors
*/
record(errors) {
this.$errors = Object.assign({}, errors);
return this;
}
/**
* Append errors to the ErrorsStack
*
* @param errors
*/
append(errors) {
this.$errors = Object.assign({}, this.$errors, errors);
return this;
}
/**
* checks if fieldKey exists in the ErrorsStack
*
* @param fieldKey
*/
has(fieldKey) {
return this.$errors.hasOwnProperty(fieldKey);
}
/**
* Returns array of errors for specific field
*
* @param fieldKey
* @param defaultValue
*/
get(fieldKey, defaultValue = []) {
if (!this.has(fieldKey)) {
return defaultValue;
}
return this.$errors[fieldKey];
}
/**
* returns first error of specific field key
*
* @param fieldKey
* @param defaultValue
*/
getFirst(fieldKey, defaultValue = null) {
const errors = this.get(fieldKey);
return errors.length <= 0 ? defaultValue : errors[0];
}
/**
* Returns all the ErrorsStack
*/
all() {
return this.$errors;
}
/**
* delete a key from ErrorsStack
*
* @param fieldKey
*/
clearField(fieldKey) {
if (this.has(fieldKey)) {
delete this.$errors[fieldKey];
this.$errors = Object.assign({}, this.$errors);
}
return this;
}
/**
* check if there is any error in the ErrorsStack
*/
any() {
return Object.keys(this.$errors).length > 0;
}
/**
* Clear the ErrorsStack object
*/
clear() {
this.$errors = {};
return this;
}
}
exports.Errors = Errors;
132 changes: 132 additions & 0 deletions lib/core/Form.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Errors } from "./Errors";
import { Validator } from "./Validator";
import { Options, SubmitCallback } from "../types";
export declare class Form {
/**
* Defaults options for the Form instance
*/
static defaults: Options;
/**
* determine if the form is on submitting mode
*/
$submitting: boolean;
/**
* Errors class - handling all the errors of the fields
*/
$errors: Errors;
/**
* Validator class - handling all the validations stuff
*/
$validator: Validator;
/**
* Holds all the labels of the fields
*/
$labels: Object;
/**
* The initiate data that was provide to the form
*/
$originalData: Object;
/**
* all the extra data that provide in the construction of this class
* will be hold here.
*/
$extra: Object;
/**
* Options of the Form
*/
$options: Options;
/**
* constructor of the class
*
* @param data
* @param options
*/
constructor(data: Object, options?: Options);
/**
* Init the form
* fill all the data that should be filled (Validator, OriginalData etc..(
*
* @param data
*/
private init;
/**
* Set all the fields value same as $originalData fields value
*/
reset(): Form;
/**
* get all the data of the form
*/
data(): Object;
/**
* fill the Form data with new data.
* without remove another fields data.
*
* @param newData
*/
fill(newData: Object): Form;
/**
* validate specific key or the whole form.
*
* @param fieldKey
*/
validate(fieldKey?: string | null): boolean;
/**
* validate specific field
*
* @param fieldKey
*/
validateField(fieldKey: string): boolean;
/**
* validate all the fields of the form
*/
validateAll(): boolean;
/**
* build Field object
*
* @param fieldKey
*/
private buildFieldObject;
/**
* assign options to Options object
*
* @param options
*/
assignOptions(options: Options): this;
/**
* submit the form, this method received a callback that
* will submit the form and must return a Promise.
*
* @param callback
*/
submit(callback: SubmitCallback): Promise<any>;
/**
* Successful submission method
*
* @param response
*/
private successfulSubmission;
/**
* UnSuccessful submission method
*
* @param error
*/
private unSuccessfulSubmission;
/**
* Hook for successful submission
* use Form.successfulSubmissionHook = () => {};
* for extending the successful submission handling
*
* @param response
* @param form
*/
static successfulSubmissionHook(response: any, form: Form): Promise<any>;
/**
* Hook for un successful submission
* use Form.unSuccessfulSubmissionHook = () => {};
* for extending the un successful submission handling
*
* @param error
* @param form
*/
static unSuccessfulSubmissionHook(error: any, form: Form): Promise<any>;
}
Loading

0 comments on commit d07cc2b

Please sign in to comment.