dotnet/maui

DisplayPromptAsync preselect initialValue in prompt for easy overwriting

Open

#4,981 opened on Mar 1, 2022

View on GitHub
 (6 comments) (3 reactions) (0 assignees)C# (1,951 forks)batch import
area-controls-dialogalertgood first issueproposal/opent/enhancement ☀️

Repository metrics

Stars
 (23,245 stars)
PR merge metrics
 (Avg merge 47d 9h) (248 merged PRs in 30d)

Description

Description

With DisplayPromptAsync you can include an initialValue to be showed in the dialog shown.

I'd like a feature for this initialValue to be preselected so it is ready to be overwritten with a simple input, instead of the user needs to clear the field first.

Also requested on StackOverflow by another user.

Public API Changes

Not sure if I fill out this field correctly, but just add another optional parameter next to string initialValue named e.g. bool preselectInitialValue = false. In body, if new parameter is true, select all of the text in tne Entry. Also test scenariou where user enters input very quickly after rendering, as the .Text apparently can be null.

public Task<string> DisplayPromptAsync(
  string title, 
  string message, 
  string accept = "OK", 
  string cancel = "Cancel", 
  string placeholder = null, 
  int maxLength = -1, 
  Keyboard keyboard = null, 
  string initialValue = "", 
  bool preselectInitialValue = false) 
{
  (...)
  if (preselectInitialValue && !string.isNullOrEmpty(initialValue)) {
    x.SelectionLength = x.Text != null ? x.Text.Length : 0;
  }
  (...)
}

Intended Use-Case

Example: User needs to enter number of copies to print. Most often this is 1, so that is set as initialValue. If user presses "2" to print two copies, it now says "12". If as requested, we could indicate the initialValue should be preselected (quite a normal UI functionality), then when user presses "2", it would overwrite "1" and instead the value entered would be "2".

Contributor guide