@momo-kits/calculator-keyboard 0.150.1-rn80.9 → 0.150.2-beta.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,7 +40,20 @@ class RCTInputCalculator : ReactTextInputManager() {
40
40
 
41
41
  @ReactProp(name = "value")
42
42
  fun setValue(view: ReactEditText, value: String?) {
43
- // view.setText(value)
43
+ UiThreadUtil.runOnUiThread {
44
+ val e = view.editableText ?: run { view.setText(value ?: ""); return@runOnUiThread }
45
+ val newText = value ?: ""
46
+ if (e.toString() == newText) return@runOnUiThread
47
+
48
+ val wasFocused = view.hasFocus()
49
+ val atEnd = wasFocused && view.selectionStart == e.length && view.selectionEnd == e.length
50
+
51
+ e.replace(0, e.length, newText)
52
+
53
+ if (!wasFocused || atEnd) {
54
+ view.setSelection(newText.length)
55
+ }
56
+ }
44
57
  }
45
58
 
46
59
  @ReactProp(name = "keyboardColor")
@@ -53,6 +53,9 @@ RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)
53
53
  RCT_EXPORT_SHADOW_PROPERTY(text, NSString)
54
54
  RCT_EXPORT_SHADOW_PROPERTY(placeholder, NSString)
55
55
  RCT_EXPORT_SHADOW_PROPERTY(onContentSizeChange, RCTDirectEventBlock)
56
+ RCT_EXPORT_VIEW_PROPERTY(value, NSString)
57
+ RCT_EXPORT_VIEW_PROPERTY(onFocus, RCTBubblingEventBlock)
58
+ RCT_EXPORT_VIEW_PROPERTY(onBlur, RCTBubblingEventBlock)
56
59
 
57
60
  RCT_EXPORT_METHOD(focus : (nonnull NSNumber *)viewTag)
58
61
  {
@@ -6,34 +6,69 @@ class RCTInputCalculator: RCTBaseTextInputViewManager {
6
6
  override func view() -> UIView! {
7
7
  return InputCalculator(bridge: bridge)
8
8
  }
9
-
9
+
10
10
  override static func requiresMainQueueSetup() -> Bool {
11
11
  return true
12
12
  }
13
-
13
+
14
14
  }
15
15
 
16
16
  class InputCalculator: RCTSinglelineTextInputView {
17
17
  var bridge: RCTBridge?
18
18
  var keyboardView: CalculatorKeyboardView?
19
-
20
- @objc var value: String = ""
21
-
19
+
20
+ @objc var onFocus: RCTBubblingEventBlock?
21
+ @objc var onBlur: RCTBubblingEventBlock?
22
+
23
+ @objc func beginEditingInput(_ note: Notification) { onFocus?([:]) }
24
+ @objc func endEditingInput(_ note: Notification) { onBlur?([:]) }
25
+
26
+ @objc var value: String = "" {
27
+ didSet {
28
+ guard let tf = backedTextInputView as? UITextField else { return }
29
+ let old = tf.text ?? ""
30
+ if old == value { return }
31
+
32
+ let isFirstResponder = tf.isFirstResponder
33
+ let atEnd = isFirstResponder && tf.offset(from: tf.beginningOfDocument, to: tf.selectedTextRange?.start ?? tf.endOfDocument) == old.count
34
+ tf.text = value
35
+ if !isFirstResponder || atEnd,
36
+ let end = tf.position(from: tf.beginningOfDocument, offset: value.count) {
37
+ tf.selectedTextRange = tf.textRange(from: end, to: end)
38
+ }
39
+ }
40
+ }
41
+
22
42
  @objc var keyboardColor: UIColor = UIColor(hex: "#d9d9d9") {
23
43
  didSet {
24
44
  self.keyboardView?.setKeyboardColor(keyboardColor)
25
45
  }
26
46
  }
27
-
47
+
48
+
28
49
  override init(bridge: RCTBridge) {
29
50
  super.init(bridge: bridge)
30
51
  self.bridge = bridge
31
52
  self.keyboardView = CalculatorKeyboardView()
32
53
  self.keyboardView!.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 290 + getbottomInset())
33
54
  self.keyboardView!.input = self
34
-
55
+
35
56
  backedTextInputView.inputView = self.keyboardView
36
57
  backedTextInputView.inputView?.reloadInputViews()
58
+
59
+ NotificationCenter.default.addObserver(
60
+ self,
61
+ selector: #selector(beginEditingInput(_:)),
62
+ name: UITextField.textDidBeginEditingNotification,
63
+ object: backedTextInputView
64
+ )
65
+ NotificationCenter.default.addObserver(
66
+ self,
67
+ selector: #selector(endEditingInput(_:)),
68
+ name: UITextField.textDidEndEditingNotification,
69
+ object: backedTextInputView
70
+ )
71
+
37
72
  }
38
73
  func getbottomInset() -> CGFloat {
39
74
  let window = UIApplication.shared.windows.first?.rootViewController?.view
@@ -48,7 +83,7 @@ class InputCalculator: RCTSinglelineTextInputView {
48
83
  bridge.eventDispatcher().sendTextEvent(with: .change, reactTag: reactTag, text: value, key: "\(key)", eventCount: 1)
49
84
  }
50
85
  }
51
-
86
+
52
87
  func clearText() {
53
88
  value = ""
54
89
  (backedTextInputView as? UITextField)?.text = ""
@@ -56,7 +91,7 @@ class InputCalculator: RCTSinglelineTextInputView {
56
91
  bridge.eventDispatcher().sendTextEvent(with: .change, reactTag: reactTag, text: value, key: "clear", eventCount: 1)
57
92
  }
58
93
  }
59
-
94
+
60
95
  func onBackSpace() {
61
96
  value = value.dropLast().description
62
97
  DispatchQueue.main.async {
@@ -67,23 +102,23 @@ class InputCalculator: RCTSinglelineTextInputView {
67
102
  self.backedTextInputView.replace(newRange, withText: "")
68
103
  }
69
104
  }
70
-
105
+
71
106
  if let bridge = bridge {
72
107
  bridge.eventDispatcher().sendTextEvent(with: .change, reactTag: reactTag, text: value, key: "back", eventCount: 1)
73
108
  }
74
109
  }
75
-
110
+
76
111
  func calculateResult() {
77
112
  guard let textField = backedTextInputView as? UITextField,
78
113
  let text = textField.text?.replacingOccurrences(of: "×", with: "*").replacingOccurrences(of: "÷", with: "/")
79
114
  else {
80
115
  return
81
116
  }
82
-
117
+
83
118
  let pattern = "^\\s*(-?\\d+(\\.\\d+)?\\s*[-+*/]\\s*)*-?\\d+(\\.\\d+)?\\s*$"
84
119
  let regex = try? NSRegularExpression(pattern: pattern)
85
120
  let range = NSRange(location: 0, length: text.utf16.count)
86
-
121
+
87
122
  if regex?.firstMatch(in: text, options: [], range: range) != nil {
88
123
  let expression = NSExpression(format: text)
89
124
  if let result = expression.expressionValue(with: nil, context: nil) as? NSNumber {
@@ -98,6 +133,6 @@ class InputCalculator: RCTSinglelineTextInputView {
98
133
  print("Invalid expression")
99
134
  }
100
135
  }
101
-
136
+
102
137
  }
103
138
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/calculator-keyboard",
3
- "version": "0.150.1-rn80.9",
3
+ "version": "0.150.2-beta.12",
4
4
  "description": "react native calculator keyboard",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",