Updated textfield (temporarily breaking UWP/WPF)
* Added placeholder * Moved the event handlers to the constructor like in SwiftUI
This commit is contained in:
Родитель
041e8c29f1
Коммит
7d68cf28b4
|
@ -41,10 +41,7 @@ namespace HotUI.Samples
|
|||
new VStack
|
||||
{
|
||||
(state.CanEdit
|
||||
? (View) new TextField(() => state.Text)
|
||||
{
|
||||
Completed = (e) => state.Text = e
|
||||
}
|
||||
? (View) new TextField(() => state.Text, onCommit: value => state.Text = value)
|
||||
: new Text(() => $"{state.Text}: multiText")), // Fromated Text will warn you. This should be done by TextBinding
|
||||
new Text(state.Text),
|
||||
new HStack
|
||||
|
|
|
@ -35,10 +35,7 @@ namespace HotUI.Samples {
|
|||
new VStack
|
||||
{
|
||||
(state.CanEdit
|
||||
? (View) new TextField(() => state.Text)
|
||||
{
|
||||
Completed = (e) => state.Text = e
|
||||
}
|
||||
? (View) new TextField(() => state.Text, onCommit: (value) => state.Text = value )
|
||||
: new Text(() => $"{state.Text}: multiText")), // Fromated Text will warn you. This should be done by TextBinding
|
||||
new Text(state.Text),
|
||||
new HStack
|
||||
|
|
|
@ -28,6 +28,7 @@ namespace HotUI.Samples {
|
|||
new MenuItem("SecureFieldSample1", ()=> new SecureFieldSample1()),
|
||||
new MenuItem("SecureFieldSample2", ()=> new SecureFieldSample2()),
|
||||
new MenuItem("SecureFieldSample3", ()=> new SecureFieldSample3()),
|
||||
new MenuItem("TextFieldSample1", ()=> new TextFieldSample1()),
|
||||
new MenuItem("SwiftUI Tutorial Section 1", ()=> new Section1()),
|
||||
new MenuItem("SwiftUI Tutorial Section 2", ()=> new Section2()),
|
||||
new MenuItem("SwiftUI Tutorial Section 3", ()=> new Section3()),
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace HotUI.Samples
|
||||
{
|
||||
public class TextFieldSample1 : View
|
||||
{
|
||||
readonly State<string> name1 = "";
|
||||
readonly State<string> name2 = "";
|
||||
|
||||
public TextFieldSample1()
|
||||
{
|
||||
Body = () => new VStack
|
||||
{
|
||||
new TextField("Name", name1, name2),
|
||||
new HStack()
|
||||
{
|
||||
new Text("onEditingChanged:"),
|
||||
new Text(name1),
|
||||
new Spacer()
|
||||
},
|
||||
new HStack()
|
||||
{
|
||||
new Text("onCommit:"),
|
||||
new Text(name2),
|
||||
new Spacer()
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,7 +23,10 @@ namespace HotUI.Android
|
|||
return editText;
|
||||
}
|
||||
|
||||
private void HandleTextChanged(object sender, EventArgs e) => VirtualView?.Completed(TypedNativeView.Text);
|
||||
private void HandleTextChanged(object sender, EventArgs e)
|
||||
{
|
||||
VirtualView?.OnCommit?.Invoke(TypedNativeView.Text);
|
||||
}
|
||||
|
||||
public static void MapTextProperty(IViewHandler viewHandler, TextField virtualView)
|
||||
{
|
||||
|
|
|
@ -3,26 +3,27 @@ using HotUI;
|
|||
using Xamarin.Forms;
|
||||
using FEntry = Xamarin.Forms.Entry;
|
||||
using HView = HotUI.View;
|
||||
namespace HotUI.Forms {
|
||||
public class TextFieldHandler : FEntry, FormsViewHandler {
|
||||
namespace HotUI.Forms
|
||||
{
|
||||
public class TextFieldHandler : FEntry, FormsViewHandler
|
||||
{
|
||||
TextField _textField;
|
||||
|
||||
public TextFieldHandler ()
|
||||
{
|
||||
this.Focused += FormsControl_Focused;
|
||||
this.TextChanged += FormsControl_TextChanged;
|
||||
this.Unfocused += FormsControl_Unfocused;
|
||||
this.Completed += FormsControl_Completed;
|
||||
Focused += HandleFocused;
|
||||
TextChanged += HandleTextChanged;
|
||||
Unfocused += HandleUnfocused;
|
||||
Completed += HandleCompleted;
|
||||
}
|
||||
TextField _textField;
|
||||
|
||||
public Xamarin.Forms.View View => this;
|
||||
public object NativeView => View;
|
||||
public bool HasContainer { get; set; } = false;
|
||||
|
||||
public void Remove (HView view)
|
||||
{
|
||||
this.TextChanged -= FormsControl_TextChanged;
|
||||
this.Unfocused -= FormsControl_Unfocused;
|
||||
this.Completed -= FormsControl_Completed;
|
||||
this.Focused -= FormsControl_Focused;
|
||||
|
||||
}
|
||||
|
||||
public void SetView (HView view)
|
||||
|
@ -31,23 +32,31 @@ namespace HotUI.Forms {
|
|||
if (_textField == null)
|
||||
return;
|
||||
this.UpdateProperties (_textField);
|
||||
|
||||
}
|
||||
|
||||
public void UpdateValue (string property, object value)
|
||||
{
|
||||
this.UpdateProperty (property, value);
|
||||
}
|
||||
|
||||
private void HandleCompleted(object sender, EventArgs e)
|
||||
{
|
||||
_textField?.OnCommit?.Invoke(Text);
|
||||
}
|
||||
|
||||
private void HandleUnfocused(object sender, FocusEventArgs e)
|
||||
{
|
||||
_textField?.Unfocused?.Invoke(_textField);
|
||||
}
|
||||
|
||||
private void FormsControl_Completed (object sender, EventArgs e) => _textField?.Completed?.Invoke (Text);
|
||||
|
||||
private void FormsControl_Unfocused (object sender, Xamarin.Forms.FocusEventArgs e) => _textField?.Unfocused?.Invoke (_textField);
|
||||
|
||||
private void FormsControl_TextChanged (object sender, Xamarin.Forms.TextChangedEventArgs e) =>
|
||||
_textField?.TextChanged?.Invoke ((e.NewTextValue, e.OldTextValue));
|
||||
|
||||
private void FormsControl_Focused (object sender, Xamarin.Forms.FocusEventArgs e) => _textField?.Focused?.Invoke (_textField);
|
||||
private void HandleTextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
_textField?.OnEditingChanged?.Invoke(e.NewTextValue);
|
||||
}
|
||||
|
||||
private void HandleFocused(object sender, FocusEventArgs e)
|
||||
{
|
||||
_textField?.Focused?.Invoke(_textField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ using AppKit;
|
|||
|
||||
namespace HotUI.Mac.Handlers
|
||||
{
|
||||
public class TextFieldHandler : AbstractHandler<TextField,NSTextField>
|
||||
public class TextFieldHandler : AbstractHandler<TextField, NSTextField>
|
||||
{
|
||||
public static readonly PropertyMapper<TextField> Mapper = new PropertyMapper<TextField>(ViewHandler.Mapper)
|
||||
{
|
||||
|
@ -13,15 +13,24 @@ namespace HotUI.Mac.Handlers
|
|||
public TextFieldHandler() : base(Mapper)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
protected override NSTextField CreateView()
|
||||
{
|
||||
var textField = new NSTextField();
|
||||
textField.EditingEnded += EntryHandler_Ended;
|
||||
textField.EditingEnded += HandleEditingEnded;
|
||||
textField.Changed += HandleEditingChanged;
|
||||
return textField;
|
||||
}
|
||||
|
||||
void EntryHandler_Ended(object sender, EventArgs e) => VirtualView?.Completed(TypedNativeView.StringValue);
|
||||
private void HandleEditingChanged(object sender, EventArgs e)
|
||||
{
|
||||
VirtualView?.OnEditingChanged?.Invoke(TypedNativeView.StringValue);
|
||||
}
|
||||
|
||||
void HandleEditingEnded(object sender, EventArgs e)
|
||||
{
|
||||
VirtualView?.OnCommit?.Invoke(TypedNativeView.StringValue);
|
||||
}
|
||||
|
||||
public static void MapTextProperty(IViewHandler viewHandler, TextField virtualView)
|
||||
{
|
||||
|
|
|
@ -9,7 +9,8 @@ namespace HotUI.iOS
|
|||
{
|
||||
public static readonly PropertyMapper<TextField> Mapper = new PropertyMapper<TextField>(ViewHandler.Mapper)
|
||||
{
|
||||
[nameof(TextField.Text)] = MapTextProperty
|
||||
[nameof(TextField.Text)] = MapTextProperty,
|
||||
[nameof(SecureField.Placeholder)] = MapPlaceholderProperty
|
||||
};
|
||||
|
||||
public TextFieldHandler() : base(Mapper)
|
||||
|
@ -20,19 +21,26 @@ namespace HotUI.iOS
|
|||
protected override UITextField CreateView()
|
||||
{
|
||||
var textField = new UITextField();
|
||||
textField.EditingDidEnd += EntryHandler_EditingDidEnd;
|
||||
|
||||
textField.EditingDidEnd += HandleEditingDidEnd;
|
||||
textField.EditingChanged += HandleEditingChanged;
|
||||
|
||||
textField.ShouldReturn = s =>
|
||||
{
|
||||
textField.ResignFirstResponder();
|
||||
return true;
|
||||
};
|
||||
|
||||
return textField;
|
||||
}
|
||||
|
||||
private void EntryHandler_EditingDidEnd(object sender, EventArgs e)
|
||||
|
||||
private void HandleEditingChanged(object sender, EventArgs e)
|
||||
{
|
||||
VirtualView?.Completed(TypedNativeView.Text);
|
||||
VirtualView?.OnEditingChanged?.Invoke(TypedNativeView.Text);
|
||||
}
|
||||
|
||||
private void HandleEditingDidEnd(object sender, EventArgs e)
|
||||
{
|
||||
VirtualView?.OnCommit?.Invoke(TypedNativeView.Text);
|
||||
}
|
||||
|
||||
public static void MapTextProperty(IViewHandler viewHandler, TextField virtualView)
|
||||
|
@ -41,5 +49,12 @@ namespace HotUI.iOS
|
|||
nativeView.Text = virtualView.Text;
|
||||
nativeView.SizeToFit();
|
||||
}
|
||||
|
||||
public static void MapPlaceholderProperty(IViewHandler viewHandler, TextField virtualView)
|
||||
{
|
||||
var nativeView = (UITextField) viewHandler.NativeView;
|
||||
nativeView.Placeholder = virtualView.Placeholder;
|
||||
nativeView.SizeToFit();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +1,43 @@
|
|||
using System;
|
||||
namespace HotUI {
|
||||
public class TextField : View {
|
||||
public TextField ()
|
||||
{
|
||||
|
||||
namespace HotUI
|
||||
{
|
||||
public class TextField : View
|
||||
{
|
||||
public TextField (string placeholder, Action<string> onEditingChanged = null, Action<string> onCommit = null) : base (true)
|
||||
{
|
||||
Placeholder = placeholder;
|
||||
OnEditingChanged = onEditingChanged;
|
||||
OnCommit = onCommit;
|
||||
}
|
||||
public TextField (string text) : base (true)
|
||||
|
||||
public TextField (string text = null, string placeholder = null, Action<string> onEditingChanged = null, Action<string> onCommit = null) : base (true)
|
||||
{
|
||||
Text = text;
|
||||
Placeholder = placeholder;
|
||||
OnEditingChanged = onEditingChanged;
|
||||
OnCommit = onCommit;
|
||||
}
|
||||
public TextField (Func<string> builder)
|
||||
|
||||
public TextField (Func<string> builder, string placeholder = null, Action<string> onEditingChanged = null, Action<string> onCommit = null )
|
||||
{
|
||||
TextBinding = builder;
|
||||
Placeholder = placeholder;
|
||||
OnEditingChanged = onEditingChanged;
|
||||
OnCommit = onCommit;
|
||||
}
|
||||
|
||||
|
||||
string text;
|
||||
public string Text {
|
||||
get => text;
|
||||
private set => this.SetValue (State, ref text, value, ViewPropertyChanged);
|
||||
}
|
||||
|
||||
string placeholder;
|
||||
public string Placeholder {
|
||||
get => placeholder;
|
||||
set => this.SetValue (State, ref placeholder, value, ViewPropertyChanged);
|
||||
}
|
||||
|
||||
public Func<string> TextBinding { get; private set; }
|
||||
protected override void WillUpdateView ()
|
||||
|
@ -36,16 +54,10 @@ namespace HotUI {
|
|||
Text = text;
|
||||
}
|
||||
}
|
||||
|
||||
string placeholder;
|
||||
public string Placeholder {
|
||||
get => placeholder;
|
||||
set => this.SetValue (State, ref placeholder, value, ViewPropertyChanged);
|
||||
}
|
||||
|
||||
public Action<string> Completed { get; set; }
|
||||
public Action<TextField> Focused { get; set; }
|
||||
public Action<TextField> Unfocused { get; set; }
|
||||
public Action<(string NewText, string OldText)> TextChanged { get; set; }
|
||||
|
||||
public Action<TextField> Focused { get; private set; }
|
||||
public Action<TextField> Unfocused { get; private set; }
|
||||
public Action<string> OnEditingChanged { get; private set; }
|
||||
public Action<string> OnCommit { get; private set; }
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче