LyInput<T> constructor

LyInput<T>({
  1. required T pureValue,
  2. LyValidationType? validationType,
  3. LyBaseValidator<T>? validator,
  4. String? debugName,
  5. bool onPreDirty(
    1. LyInput<T> self,
    2. T value
    )?,
  6. bool onPostDirty(
    1. LyInput<T> self,
    2. T value
    )?,
  7. bool onPrePure(
    1. LyInput<T> self,
    2. T value
    )?,
  8. bool onPostPure(
    1. LyInput<T> self,
    2. T value
    )?,
  9. bool onPreValidate(
    1. LyInput<T> self
    )?,
  10. bool onPostValidate(
    1. LyInput<T> self
    )?,
})

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(),
  );
}