diff --git a/doc/helpers/Input-extensions.md b/doc/helpers/Input-extensions.md index fcf585485..8874f6f67 100644 --- a/doc/helpers/Input-extensions.md +++ b/doc/helpers/Input-extensions.md @@ -8,12 +8,12 @@ Provides various attached properties for _input controls_, such as `TextBox` and ## Attached Properties -| Property | Type | Description | -|------------------------|--------------|-----------------------------------------------------------------------------------------------------------------------------------| -| `AutoDismiss` | `bool` | Whether the soft keyboard will be dismissed when the enter key is pressed. | -| `AutoFocusNext` | `bool` | Whether the focus will move to the next focusable element when the enter key is pressed.\* | -| `AutoFocusNextElement` | `Control` | Sets the next control to focus when the enter key is pressed.\* | -| `ReturnType` | `ReturnType` | The type of return button on a soft keyboard. It can be one of the following options: __Default, Done, Go, Next, Search, Send__. | +| Property | Type | Description | +|------------------------|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------| +| `AutoDismiss` | `bool` | Whether the soft keyboard will be dismissed when the enter key is pressed. | +| `AutoFocusNext` | `bool` | Whether the focus will move to the next focusable element when the enter key is pressed.\* | +| `AutoFocusNextElement` | `Control` | Sets the next control to focus when the enter key is pressed.\* | +| `ReturnType` | `ReturnType` | The type of return button on a soft keyboard for Android/iOS. It can be one of the following options: __Default, Done, Go, Next, Search, Send__. | `AutoFocusNext` and `AutoFocusNextElement`\*: Having either or both of the two properties set will enable the focus next behavior. `AutoFocusNextElement` will take precedence over `AutoFocusNext` when both are set. diff --git a/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs b/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs index 8164fd9a8..c56d38203 100644 --- a/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs +++ b/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs @@ -143,27 +143,31 @@ internal static bool IsEnterCommandSupportedFor(DependencyObject host) private static void OnReturnTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { - if (sender is Control control && (sender is TextBox || sender is PasswordBox) && e.NewValue is ReturnType returnType) + if (sender is TextBox || sender is PasswordBox) { + if (e.NewValue is not ReturnType returnType) + { + returnType = ReturnType.Default; + } #if __ANDROID__ ImeAction imeAction = GetImeActionFromReturnType(returnType); - if (control is TextBox textBox) + if (sender is TextBox textBox) { textBox.ImeOptions = imeAction; } - else if (control is PasswordBox passwordBox) + else if (sender is PasswordBox passwordBox) { passwordBox.ImeOptions = imeAction; } #elif __IOS__ UIReturnKeyType returnKeyType = GetReturnKeyTypeFromReturnType(returnType); - if (control is TextBox textBox) + if (sender is TextBox textBox) { textBox.ReturnKeyType = returnKeyType; } - else if (control is PasswordBox passwordBox) + else if (sender is PasswordBox passwordBox) { passwordBox.ReturnKeyType = returnKeyType; } @@ -241,35 +245,27 @@ private static void OnUIElementKeyUp(object sender, KeyRoutedEventArgs e) } #if __ANDROID__ - private static ImeAction GetImeActionFromReturnType(ReturnType returnType) + private static ImeAction GetImeActionFromReturnType(ReturnType returnType) => returnType switch { - switch (returnType) - { - case ReturnType.Next: return ImeAction.Next; - case ReturnType.Go: return ImeAction.Go; - case ReturnType.Search: return ImeAction.Search; - case ReturnType.Send: return ImeAction.Send; - case ReturnType.Done: return ImeAction.Done; - case ReturnType.Default: - default: return ImeAction.Unspecified; - } - } + ReturnType.Next => ImeAction.Next, + ReturnType.Go => ImeAction.Go, + ReturnType.Search => ImeAction.Search, + ReturnType.Send => ImeAction.Send, + ReturnType.Done => ImeAction.Done, + ReturnType.Default or _ => ImeAction.Unspecified + }; #endif #if __IOS__ - private static UIReturnKeyType GetReturnKeyTypeFromReturnType(ReturnType returnType) + private static UIReturnKeyType GetReturnKeyTypeFromReturnType(ReturnType returnType) => returnType switch { - switch (returnType) - { - case ReturnType.Next: return UIReturnKeyType.Next; - case ReturnType.Go: return UIReturnKeyType.Go; - case ReturnType.Search: return UIReturnKeyType.Search; - case ReturnType.Send: return UIReturnKeyType.Send; - case ReturnType.Done: return UIReturnKeyType.Done; - case ReturnType.Default: - default: return UIReturnKeyType.Default; - } - } + ReturnType.Next => UIReturnKeyType.Next, + ReturnType.Go => UIReturnKeyType.Go, + ReturnType.Search => UIReturnKeyType.Search, + ReturnType.Send => UIReturnKeyType.Send, + ReturnType.Done => UIReturnKeyType.Done, + ReturnType.Default or _ => UIReturnKeyType.Default + }; #endif } }