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

Preserve input's selection #35

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions glade_forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.3.2
- **[Fix]**: `GladeInput` now preserves selection. (Before, a cursor jumped at the end.)

## 1.3.1
- **[Fix]**: Fixed `GladeInput.create` assert to allow null for `value` and `initialValue` when input's type is nullable.

Expand Down
7 changes: 5 additions & 2 deletions glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class GladeInput<T> extends ChangeNotifier {
// synchronize text controller with value
_textEditingController?.value = TextEditingValue(
text: strValue,
selection: TextSelection.collapsed(offset: strValue.length),
selection: _textEditingController?.selection ?? const TextSelection.collapsed(offset: -1),
);

_isPure = false;
Expand Down Expand Up @@ -201,7 +201,10 @@ class GladeInput<T> extends ChangeNotifier {
ValueTransform<T>? valueTransform,
DefaultTranslations? defaultTranslations,
}) {
assert(value != null || initialValue != null || TypeHelper.typeIsNullable<T>(), 'If type is not nullable, at least one of value or initialValue must be set');
assert(
value != null || initialValue != null || TypeHelper.typeIsNullable<T>(),
'If type is not nullable, at least one of value or initialValue must be set',
);

final validatorInstance = validator?.call(GladeValidator<T>()) ?? GladeValidator<T>().build();

Expand Down
2 changes: 1 addition & 1 deletion glade_forms/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: glade_forms
description: A universal way to define form validators with support of translations.
version: 1.3.1
version: 1.3.2
repository: https://github.com/netglade/glade_forms
issue_tracker: https://github.com/netglade/glade_forms/issues
screenshots:
Expand Down
19 changes: 19 additions & 0 deletions glade_forms/test/glade_input_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/widgets.dart';
import 'package:glade_forms/glade_forms.dart';
import 'package:test/test.dart';

Expand All @@ -7,4 +8,22 @@ void main() {

expect(input.isValid, isTrue);
});

test('GladeInput preserves selection on value change', () {
final input = GladeInput<String>.create(validator: (v) => v.build(), value: 'abc', inputKey: 'a');

// Set selection
input.controller?.selection = const TextSelection.collapsed(offset: 1);
final before = input.controller?.selection;

// Update text value.
input.updateValue('abXXXc');

// ignore: avoid-duplicate-initializers, different value
final after = input.controller?.selection;

// Compare their offsets.
expect(before?.baseOffset, equals(after?.baseOffset));
expect(before?.extentOffset, equals(after?.extentOffset));
});
}
Loading