|
@@ -14,6 +14,7 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
|
|
|
var isDismissible: Bool
|
|
var isDismissible: Bool
|
|
|
var textFieldDidBeginEditing: (() -> Void)?
|
|
var textFieldDidBeginEditing: (() -> Void)?
|
|
|
var numberFormatter: NumberFormatter
|
|
var numberFormatter: NumberFormatter
|
|
|
|
|
+ var allowDecimalSeparator: Bool
|
|
|
|
|
|
|
|
public init(
|
|
public init(
|
|
|
text: Binding<Decimal>,
|
|
text: Binding<Decimal>,
|
|
@@ -27,7 +28,8 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
|
|
|
maxLength: Int? = nil,
|
|
maxLength: Int? = nil,
|
|
|
isDismissible: Bool = true,
|
|
isDismissible: Bool = true,
|
|
|
textFieldDidBeginEditing: (() -> Void)? = nil,
|
|
textFieldDidBeginEditing: (() -> Void)? = nil,
|
|
|
- numberFormatter: NumberFormatter
|
|
|
|
|
|
|
+ numberFormatter: NumberFormatter,
|
|
|
|
|
+ allowDecimalSeparator: Bool = true
|
|
|
) {
|
|
) {
|
|
|
_text = text
|
|
_text = text
|
|
|
self.placeholder = placeholder
|
|
self.placeholder = placeholder
|
|
@@ -42,6 +44,7 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
|
|
|
self.textFieldDidBeginEditing = textFieldDidBeginEditing
|
|
self.textFieldDidBeginEditing = textFieldDidBeginEditing
|
|
|
self.numberFormatter = numberFormatter
|
|
self.numberFormatter = numberFormatter
|
|
|
self.numberFormatter.numberStyle = .decimal
|
|
self.numberFormatter.numberStyle = .decimal
|
|
|
|
|
+ self.allowDecimalSeparator = allowDecimalSeparator
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public func makeUIView(context: Context) -> UITextField {
|
|
public func makeUIView(context: Context) -> UITextField {
|
|
@@ -146,7 +149,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
|
|
|
let isDecimalSeparator = (string == decimalFormatter.decimalSeparator && textField.text?.contains(string) == false)
|
|
let isDecimalSeparator = (string == decimalFormatter.decimalSeparator && textField.text?.contains(string) == false)
|
|
|
|
|
|
|
|
// Only proceed if the input is a valid number or decimal separator
|
|
// Only proceed if the input is a valid number or decimal separator
|
|
|
- if isNumber || isDecimalSeparator,
|
|
|
|
|
|
|
+ if isNumber || isDecimalSeparator && parent.allowDecimalSeparator,
|
|
|
let currentText = textField.text as NSString?
|
|
let currentText = textField.text as NSString?
|
|
|
{
|
|
{
|
|
|
// Get the proposed new text
|
|
// Get the proposed new text
|
|
@@ -164,7 +167,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Allow the change if it's a valid number or decimal separator
|
|
// Allow the change if it's a valid number or decimal separator
|
|
|
- return isNumber || isDecimalSeparator
|
|
|
|
|
|
|
+ return isNumber || isDecimalSeparator && parent.allowDecimalSeparator
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public func textFieldDidBeginEditing(_: UITextField) {
|
|
public func textFieldDidBeginEditing(_: UITextField) {
|