2016-05-02 15:08:45 +05:30
|
|
|
import FormInputComponent from './FormInputComponent';
|
2016-05-02 14:50:50 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
type LoadingListener = (isLoading: boolean) => void;
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2019-12-08 03:23:22 +05:30
|
|
|
export type ValidationError =
|
2019-12-07 16:58:52 +05:30
|
|
|
| string
|
|
|
|
| {
|
|
|
|
type: string;
|
|
|
|
payload?: { [key: string]: any };
|
|
|
|
};
|
|
|
|
|
2016-05-02 15:08:45 +05:30
|
|
|
export default class FormModel {
|
2019-11-27 14:33:32 +05:30
|
|
|
fields = {};
|
2019-12-07 16:58:52 +05:30
|
|
|
errors: {
|
|
|
|
[fieldId: string]: ValidationError;
|
|
|
|
} = {};
|
2019-11-27 14:33:32 +05:30
|
|
|
handlers: LoadingListener[] = [];
|
|
|
|
renderErrors: boolean;
|
|
|
|
_isLoading: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} options
|
|
|
|
* @param {bool} [options.renderErrors=true] - whether the bound filed should
|
|
|
|
* render their errors
|
|
|
|
*/
|
|
|
|
constructor(options: { renderErrors?: boolean } = {}) {
|
|
|
|
this.renderErrors = options.renderErrors !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects form with React's component
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* <input {...this.form.bindField('foo')} type="text" />
|
|
|
|
*
|
|
|
|
* @param {string} name - the name of field
|
|
|
|
*
|
|
|
|
* @returns {object} - ref and name props for component
|
|
|
|
*/
|
2019-12-08 03:23:22 +05:30
|
|
|
bindField(
|
|
|
|
name: string,
|
|
|
|
): {
|
|
|
|
name: string;
|
|
|
|
ref: (el: any) => void;
|
|
|
|
error?: ValidationError;
|
|
|
|
} {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.fields[name] = {};
|
|
|
|
|
2019-12-08 03:23:22 +05:30
|
|
|
const props: {
|
|
|
|
name: string;
|
|
|
|
ref: (el: any) => void;
|
|
|
|
error?: ValidationError;
|
|
|
|
} = {
|
2019-11-27 14:33:32 +05:30
|
|
|
name,
|
2019-12-07 16:58:52 +05:30
|
|
|
ref: (el: FormInputComponent<any> | null) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (el) {
|
|
|
|
if (!(el instanceof FormInputComponent)) {
|
|
|
|
throw new Error('Expected FormInputComponent component');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fields[name] = el;
|
|
|
|
} else {
|
|
|
|
delete this.fields[name];
|
2016-05-02 14:50:50 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
};
|
2016-05-02 14:50:50 +05:30
|
|
|
|
2019-12-08 03:23:22 +05:30
|
|
|
const error = this.getError(name);
|
|
|
|
|
|
|
|
if (this.renderErrors && error) {
|
|
|
|
props.error = error;
|
2016-05-01 12:48:34 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focuses field
|
|
|
|
*
|
|
|
|
* @param {string} fieldId - an id of field to focus
|
|
|
|
*/
|
|
|
|
focus(fieldId: string) {
|
|
|
|
if (!this.fields[fieldId]) {
|
|
|
|
throw new Error(
|
|
|
|
`Can not focus. The field with an id ${fieldId} does not exists`,
|
|
|
|
);
|
2016-05-01 12:48:34 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.fields[fieldId].focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a value of field
|
|
|
|
*
|
|
|
|
* @param {string} fieldId - an id of field to get value of
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
value(fieldId: string) {
|
|
|
|
const field = this.fields[fieldId];
|
|
|
|
|
|
|
|
if (!field) {
|
|
|
|
throw new Error(
|
|
|
|
`Can not get value. The field with an id ${fieldId} does not exists`,
|
|
|
|
);
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!field.getValue) {
|
|
|
|
return ''; // the field was not initialized through ref yet
|
2016-05-02 14:50:50 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return field.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add errors to form fields
|
|
|
|
*
|
|
|
|
* errorType may be string or object {type: string, payload: object}, where
|
|
|
|
* payload is additional data for errorType
|
|
|
|
*
|
|
|
|
* @param {object} errors - object maping {fieldId: errorType}
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
setErrors(errors: { [key: string]: ValidationError }) {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (typeof errors !== 'object' || errors === null) {
|
|
|
|
throw new Error('Errors must be an object');
|
2016-08-23 00:48:11 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const oldErrors = this.errors;
|
|
|
|
this.errors = errors;
|
2016-05-02 14:50:50 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
Object.keys(this.fields).forEach(fieldId => {
|
|
|
|
if (this.renderErrors) {
|
|
|
|
if (oldErrors[fieldId] || errors[fieldId]) {
|
|
|
|
this.fields[fieldId].setError(errors[fieldId] || null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasErrors()) {
|
|
|
|
this.fields[fieldId].onFormInvalid();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
getFirstError(): ValidationError | null {
|
|
|
|
const [error] = Object.values(this.errors);
|
|
|
|
|
|
|
|
return error || null;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get error by id
|
|
|
|
*
|
|
|
|
* @param {string} fieldId - an id of field to get error for
|
|
|
|
*
|
|
|
|
* @returns {string|object|null}
|
|
|
|
*/
|
|
|
|
getError(fieldId: string) {
|
|
|
|
return this.errors[fieldId] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {bool}
|
|
|
|
*/
|
|
|
|
hasErrors() {
|
|
|
|
return Object.keys(this.errors).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert form into key-value object representation
|
|
|
|
*
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
|
|
|
serialize(): { [key: string]: any } {
|
|
|
|
return Object.keys(this.fields).reduce((acc, fieldId) => {
|
|
|
|
const field = this.fields[fieldId];
|
|
|
|
|
|
|
|
if (field) {
|
|
|
|
acc[fieldId] = field.getValue();
|
|
|
|
} else {
|
|
|
|
console.warn('Can not serialize %s field. Because it is null', fieldId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind handler to listen for form loading state change
|
|
|
|
*
|
|
|
|
* @param {Function} fn
|
|
|
|
*/
|
|
|
|
addLoadingListener(fn: LoadingListener) {
|
|
|
|
this.removeLoadingListener(fn);
|
|
|
|
this.handlers.push(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove form loading state handler
|
|
|
|
*
|
|
|
|
* @param {Function} fn
|
|
|
|
*/
|
|
|
|
removeLoadingListener(fn: LoadingListener) {
|
|
|
|
this.handlers = this.handlers.filter(handler => handler !== fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch form in loading state
|
|
|
|
*/
|
|
|
|
beginLoading() {
|
|
|
|
this._isLoading = true;
|
|
|
|
this.notifyHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable loading state
|
|
|
|
*/
|
|
|
|
endLoading() {
|
|
|
|
this._isLoading = false;
|
|
|
|
this.notifyHandlers();
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
private notifyHandlers() {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.handlers.forEach(fn => fn(this._isLoading));
|
|
|
|
}
|
2016-05-01 12:48:34 +05:30
|
|
|
}
|