LyInput<T> constructor
LyInput<T> ({
- required T pureValue,
- LyValidationType? validationType,
- LyBaseValidator<
T> ? validator, - String? debugName,
- bool onPreDirty(
- LyInput<
T> self, - T value
- LyInput<
- bool onPostDirty(
- LyInput<
T> self, - T value
- LyInput<
- bool onPrePure(
- LyInput<
T> self, - T value
- LyInput<
- bool onPostPure(
- LyInput<
T> self, - T value
- LyInput<
- bool onPreValidate(
- LyInput<
T> self
- LyInput<
- bool onPostValidate(
- LyInput<
T> self
- LyInput<
Implementation
LyInput({
required T pureValue,
LyValidationType? validationType,
this.validator,
this.debugName,
this.onPreDirty,
this.onPostDirty,
this.onPrePure,
this.onPostPure,
this.onPreValidate,
this.onPostValidate,
}) : validationType = validationType ??
(validator != null
? LyValidationType.always
: LyValidationType.none),
super(
LyInputState(
value: pureValue,
lastNotNullValue: pureValue,
pureValue: pureValue,
debugName: debugName,
),
) {
on<LyInputDirtyEvent<T>>(
(event, emit) async {
await Future.delayed(Duration.zero);
final onPreDirtyResult = onPreDirty?.call(this, event.value) ?? true;
if (!onPreDirtyResult) {
return;
}
final lastNotNullValue = event.value ?? state.lastNotNullValue;
final pureValue = state.pureValue;
final error = state.error;
final debugName = state.debugName;
emit(
LyInputState<T>(
value: event.value,
lastNotNullValue: lastNotNullValue,
pureValue: pureValue,
error: error,
debugName: debugName,
),
);
if (validationType != LyValidationType.none) {
validate();
}
onPostDirty?.call(this, event.value);
},
transformer: sequential(),
);
on<LyInputPureEvent<T>>(
(event, emit) async {
await Future.delayed(Duration.zero);
final onPrePureResult = onPrePure?.call(this, event.value) ?? true;
if (!onPrePureResult) {
return;
}
final lastNotNullValue = event.value ?? state.lastNotNullValue;
final pureValue = event.value;
final debugName = state.debugName;
emit(
LyInputState<T>(
value: event.value,
lastNotNullValue: lastNotNullValue,
pureValue: pureValue,
debugName: debugName,
),
);
if (validationType == LyValidationType.always) {
validate();
}
onPostPure?.call(this, event.value);
},
transformer: sequential(),
);
on<LyInputValidateEvent<T>>(
(event, emit) async {
await Future.delayed(Duration.zero);
final onPreValidateResult = onPreValidate?.call(this) ?? true;
if (!onPreValidateResult) {
return;
}
if (validationType != LyValidationType.none) {
final value = state.value;
final lastNotNullValue = state.lastNotNullValue;
final pureValue = state.pureValue;
final debugName = state.debugName;
String? error;
if (value != pureValue && validator != null) {
error = validator!(state.value);
}
emit(
LyInputState<T>(
value: value,
lastNotNullValue: lastNotNullValue,
pureValue: pureValue,
error: error,
debugName: debugName,
),
);
}
onPostValidate?.call(this);
},
transformer: sequential(),
);
}