import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:fis_ui/widgets/gesture/shortcuts.dart';
import 'package:fis_ui/widgets/gesture/actions.dart';
import 'package:flutter/services.dart';

/// 禁用空格、回车的输入框包裹组件
class DisableSpaceInputWrapper extends FStatelessWidget {
  const DisableSpaceInputWrapper(
      {super.key, required this.controller, required this.child})
      : assert(child is FTextField, "child must be FTextField");
  final TextEditingController controller;
  final FWidget child;

  @override
  FWidget build(BuildContext context) {
    return FShortcuts(
      shortcuts: {
        LogicalKeySet(LogicalKeyboardKey.space): DoNothingIntent(),
        LogicalKeySet(LogicalKeyboardKey.enter): DoNothingIntent(),
      },
      child: FActions(
        actions: <Type, Action<Intent>>{
          DoNothingIntent: CallbackAction<DoNothingIntent>(
            onInvoke: (DoNothingIntent intent) {
              intent.doNothing(controller);
              return null;
            },
          ),
        },
        child: child,
      ),
    );
  }
}

class DoNothingIntent extends Intent {
  /// Creates an intent that does nothing.
  void doNothing(TextEditingController controller) {}
}
