Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

special-16444: обновление FieldModel #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v4.0.1

- [*] В FieldModel добавлены: возможность пропустить значение через валидаторы, хранение текущей ошибки, отслеживание состояния поля (touched, hasError, isEmpty), функция сброса

# v4.0.0

- [*] AppParamsStore: в конструктор добавлен аргумент `apiUrl`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ktsstudio/mediaproject-stores",
"version": "4.0.0",
"version": "4.0.1",
"author": "KTS Studio",
"license": "MIT",
"description": "Package with basic MobX stores for mediaprojects",
Expand Down
98 changes: 82 additions & 16 deletions src/models/FieldModel/FieldModel.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,105 @@
import { action, computed, makeObservable, observable } from 'mobx';

import { IField } from './types';
import { FormFieldInitDataType, ValidatorType } from './types';

type PrivateFields =
BobbyLilD marked this conversation as resolved.
Show resolved Hide resolved
| '_value'
| '_error'
| '_setError'
| '_resetError'
| '_touched'
| '_resetTouched';

export default class FormFieldModel<T = string> {
private readonly _validators: ValidatorType<T>[];

class FieldModel<T = string> implements IField<T> {
private _value: T;
private readonly _initialValue: T;
BobbyLilD marked this conversation as resolved.
Show resolved Hide resolved
private _touched = false;
protected readonly _initialValue: T;

constructor(value: T, config?: { initialValue: T }) {
this._value = value;
this._initialValue =
config?.initialValue === undefined ? value : config.initialValue;
private _error: string | null = null;

makeObservable<FieldModel<T>, '_value'>(this, {
_value: observable.ref,
constructor(initData: FormFieldInitDataType<T>) {
this._value = initData.value;
this._initialValue = initData.value;
this._validators = initData.validators;

makeObservable<FormFieldModel<T>, PrivateFields>(this, {
_value: observable,
_error: observable,
_touched: observable,

value: computed,
error: computed,
hasError: computed,
isEmpty: computed,
touched: computed,

changeValue: action.bound,
setValue: action.bound,
reset: action.bound,
_setError: action.bound,
_resetError: action.bound,
_resetTouched: action.bound,
});
}

get value(): T {
return this._value;
}

changeValue<I extends T>(value: I): I {
get error(): string | null {
return this._error;
}

get hasError(): boolean {
return this._error !== null;
}

get touched(): boolean {
return this._touched;
}

get isEmpty(): boolean {
return !this._value;
}

setValue(value: T): void {
if (value === this._value) {
return;
}

this._value = value;
this._resetError();
this._touched = true;
}

return value;
private _setError(value: string): void {
this._error = value;
}

reset(): void {
this._value = this._initialValue;
private _resetTouched = (): void => {
this._touched = false;
};

private _resetError(): void {
this._error = null;
}
}

export default FieldModel;
validate(): void {
this._validators.some((validator) => {
const error = validator(this.value);

if (error) {
this._setError(error);
}

return Boolean(error);
});
}

reset = (): void => {
this.setValue(this._initialValue);
this._resetTouched();
this._resetError();
};
}
10 changes: 5 additions & 5 deletions src/models/FieldModel/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface IField<T = string> {
get value(): T;
export type ValidatorType<T> = (value: T) => string | null;

changeValue(value: T): void;
reset(): void;
}
export type FormFieldInitDataType<T> = {
value: T;
validators: ValidatorType<T>[];
};