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 5 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v4.0.1

- [+] Добавлена ValueModel
- [*] FieldModel переименована в FormFieldModel и наследует ValueModel с возможностью добавления валидаторов

# 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
39 changes: 0 additions & 39 deletions src/models/FieldModel/FieldModel.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/models/FieldModel/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/models/FieldModel/types.ts

This file was deleted.

75 changes: 75 additions & 0 deletions src/models/FormFieldModel/FieldModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { action, computed, makeObservable, observable } from 'mobx';
BobbyLilD marked this conversation as resolved.
Show resolved Hide resolved

import { ValueModel } from '../ValueModel';
BobbyLilD marked this conversation as resolved.
Show resolved Hide resolved

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

type ProtectedFields = '_error' | '_setError' | '_resetError';

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

protected _error: string | null = null;

constructor(initData: FormFieldInitDataType<T>) {
super(initData.value);

this._validators = initData.validators;

makeObservable<FormFieldModel<T>, ProtectedFields>(this, {
_error: observable,

error: computed,
hasError: computed,

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

get error(): string | null {
return this._error;
}

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

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

this._value = value;
this._resetError();
this._touched = true;
BobbyLilD marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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);
BobbyLilD marked this conversation as resolved.
Show resolved Hide resolved
this._resetTouched();
this._resetError();
};
}
3 changes: 3 additions & 0 deletions src/models/FormFieldModel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as FormFieldModel } from './FieldModel';

export * from './types';
6 changes: 6 additions & 0 deletions src/models/FormFieldModel/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type ValidatorType<T> = (value: T) => string | null;

export type FormFieldInitDataType<T> = {
value: T;
validators: ValidatorType<T>[];
};
57 changes: 57 additions & 0 deletions src/models/ValueModel/ValueModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { action, computed, makeObservable, observable } from 'mobx';

type ProtectedFields = '_value' | '_touched' | '_resetTouched';

export default class ValueModel<T = string> {
protected _value: T;
protected _touched = false;
protected readonly _initialValue: T;

constructor(value: T) {
this._value = value;
this._initialValue = value;

makeObservable<ValueModel<T>, ProtectedFields>(this, {
_value: observable,
_touched: observable,

value: computed,
isEmpty: computed,
touched: computed,

setValue: action.bound,
reset: action.bound,
_resetTouched: action.bound,
});
}

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

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._touched = true;
}

protected _resetTouched = (): void => {
this._touched = false;
};

reset = (): void => {
this.setValue(this._initialValue);
this._resetTouched();
};
}
1 change: 1 addition & 0 deletions src/models/ValueModel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as ValueModel } from './ValueModel';
4 changes: 3 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export * from './BlurAndFocusHandlerModel';

export * from './PollingModel';

export * from './FieldModel';
export * from './FormFieldModel';

export * from './ValueModel';