@momo-kits/calculator-keyboard 0.150.2-beta.19 → 0.150.2-beta.20

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.
@@ -7,6 +7,7 @@ import android.graphics.drawable.GradientDrawable
7
7
  import android.view.Gravity
8
8
  import android.widget.Button
9
9
  import android.widget.ImageButton
10
+ import androidx.appcompat.app.AppCompatActivity
10
11
  import androidx.constraintlayout.widget.ConstraintLayout
11
12
  import com.facebook.react.uimanager.ThemedReactContext
12
13
  import org.mariuszgromada.math.mxparser.Expression
@@ -20,57 +21,53 @@ class CustomKeyboardView(
20
21
  context: ThemedReactContext,
21
22
  private val editText: CalculatorEditText
22
23
  ) : ConstraintLayout(context) {
23
- private val numWithCTAKeys = listOf(
24
+ private val keys = listOf(
24
25
  listOf("1", "2", "3", "÷", "back"),
25
26
  listOf("4", "5", "6", "×", "="),
26
- listOf("7", "8", "9", "-", "Tiếp"),
27
+ listOf("7", "8", "9", "-", "Xong"),
27
28
  listOf("000", "0", "+")
28
29
  )
29
- private val defaultKeys = listOf(
30
- listOf("1", "2", "3", "÷", "AC"),
31
- listOf("4", "5", "6", "×", "back"),
32
- listOf("7", "8", "9", "-", "="),
33
- listOf("000", "0", "+")
34
- )
35
- private val specialKeys = listOf("=", "-", "×", "÷", "back", "+", "AC")
30
+ private val specialKeys = listOf("=", "-", "×", "÷", "back", "+")
36
31
  private val separatorWidth = 8f
37
32
  private var specialButtonColor: Int = "#D8D8D8".toColorInt()
38
33
 
39
- private var keyboardMode: String = "NumDefault"
40
-
41
34
  private var customKeyButton: Button? = null
42
35
  private var customKeyButtonBackground: Int = "#D8D8D8".toColorInt()
43
36
  private var customKeyButtonTextColor: Int = Color.BLACK
44
37
  private var customKeyButtonState: String = "default"
45
38
 
46
39
  init {
40
+ val activity = context.currentActivity as? AppCompatActivity
41
+ if (activity != null) {
42
+ val displayMetrics = resources.displayMetrics
43
+ val widthButton = (displayMetrics.widthPixels - separatorWidth * 2 - 4 * separatorWidth) / 5f
44
+ val heightButton = (calculatorHeight - separatorWidth * 2 - 3 * separatorWidth) / 4
45
+
47
46
  isClickable = false
48
47
  isFocusable = false
49
48
  isFocusableInTouchMode = false
50
49
  clipToPadding = false
51
50
  clipChildren = false
52
- }
53
51
 
54
- private fun renderUI() {
55
- val displayMetrics = resources.displayMetrics
56
- val buttonWidth = (displayMetrics.widthPixels - separatorWidth * 2 - 4 * separatorWidth) / 5f
57
- val buttonHeight = (calculatorHeight - separatorWidth * 2 - 3 * separatorWidth) / 4
52
+ renderUI(widthButton, heightButton)
53
+ }
54
+
55
+ }
58
56
 
57
+ private fun renderUI(buttonWidth: Float, buttonHeight: Float) {
59
58
  var yOffset = separatorWidth
60
- val keys = if (keyboardMode == "NumWithCTA") numWithCTAKeys else defaultKeys
61
59
  for ((rowIndex, row) in keys.withIndex()) {
62
60
  var xOffset = separatorWidth
63
61
  for ((colIndex, key) in row.withIndex()) {
64
- val isMainKey = rowIndex == 2 && colIndex == 4
65
- val isMainCTAKey = isMainKey && keyboardMode == "NumWithCTA"
62
+ val isCustomKey = rowIndex == 2 && colIndex == 4
66
63
  val width = if (key == "000") buttonWidth * 2 + separatorWidth else buttonWidth
67
- val height = if (isMainKey) buttonHeight * 2 + separatorWidth else buttonHeight
64
+ val height = if (isCustomKey) buttonHeight * 2 + separatorWidth else buttonHeight
68
65
 
69
66
  val button = if (key == "back") {
70
67
  createImageButton(key, xOffset, yOffset, buttonWidth.toInt(), buttonHeight.toInt())
71
68
  } else {
72
- createButton(key, xOffset, yOffset, width.toInt(), height.toInt(), isMainKey, isMainCTAKey).also { b ->
73
- if (isMainCTAKey) customKeyButton = b
69
+ createButton(key, xOffset, yOffset, width.toInt(), height.toInt(), isCustomKey).also { b ->
70
+ if (isCustomKey) customKeyButton = b
74
71
  }
75
72
  }
76
73
 
@@ -88,8 +85,7 @@ class CustomKeyboardView(
88
85
  yOffset: Float,
89
86
  buttonWidth: Int,
90
87
  buttonHeight: Int,
91
- isMainKey: Boolean,
92
- isMainCTAKey: Boolean
88
+ isCustomKey: Boolean
93
89
  ): Button {
94
90
  return Button(context).apply {
95
91
  val shapeInit = GradientDrawable().apply {
@@ -102,7 +98,7 @@ class CustomKeyboardView(
102
98
  background = shapeInit
103
99
  text = key
104
100
  setTypeface(typeface)
105
- textSize = (if (isMainCTAKey) 18 else 24).toFloat()
101
+ textSize = (if (isCustomKey) 18 else 24).toFloat()
106
102
  setTextColor(Color.BLACK)
107
103
  stateListAnimator = null
108
104
  maxLines = 1
@@ -114,7 +110,7 @@ class CustomKeyboardView(
114
110
  constrainedWidth = false
115
111
  }
116
112
 
117
- if (specialKeys.contains(key) || isMainKey) {
113
+ if (specialKeys.contains(key)) {
118
114
  background = GradientDrawable().apply {
119
115
  shape = GradientDrawable.RECTANGLE
120
116
  cornerRadius = 24f
@@ -129,7 +125,7 @@ class CustomKeyboardView(
129
125
 
130
126
  translationX = xOffset.toInt().toFloat()
131
127
  translationY = yOffset.toInt().toFloat()
132
- setOnClickListener { onKeyPress(key, isMainCTAKey) }
128
+ setOnClickListener { onKeyPress(key, isCustomKey) }
133
129
  }
134
130
  }
135
131
 
@@ -199,15 +195,14 @@ class CustomKeyboardView(
199
195
  }
200
196
  }
201
197
 
202
- private fun onKeyPress(key: String, isMainCTAKey: Boolean) {
203
- if (isMainCTAKey) {
198
+ private fun onKeyPress(key: String, isCustomKey: Boolean) {
199
+ if (isCustomKey) {
204
200
  emitCustomKey()
205
201
  return
206
202
  }
207
203
 
208
204
  emitKeyPress(key)
209
205
  when (key) {
210
- "AC" -> clearText()
211
206
  "back" -> onBackSpace()
212
207
  "=" -> calculateResult()
213
208
  "×", "+", "-", "÷" -> keyDidPress(" $key ")
@@ -220,10 +215,6 @@ class CustomKeyboardView(
220
215
  editText.text?.replace(editText.selectionStart, editText.selectionEnd, key)
221
216
  }
222
217
 
223
- private fun clearText() {
224
- editText.text?.clear()
225
- }
226
-
227
218
  private fun onBackSpace() {
228
219
  val start = editText.selectionStart
229
220
  val end = editText.selectionEnd
@@ -279,29 +270,25 @@ class CustomKeyboardView(
279
270
  customKeyButton?.text = text
280
271
  }
281
272
 
282
- fun setMode(mode: String) {
283
- keyboardMode = mode
284
- renderUI()
285
- }
286
-
287
273
  fun setCustomKeyBackground(background: Int) {
288
274
  customKeyButtonBackground = background
289
- updateCustomKeyUI(background, customKeyButtonTextColor)
275
+ updateCustomKeyUI(background, customKeyButtonTextColor, customKeyButtonState)
290
276
  }
291
277
 
292
278
  fun setCustomKeyTextColor(textColor: Int) {
293
279
  customKeyButtonTextColor = textColor
294
- updateCustomKeyUI(customKeyButtonBackground, textColor)
280
+ updateCustomKeyUI(customKeyButtonBackground, textColor, customKeyButtonState)
295
281
  }
296
282
 
297
283
 
298
284
  fun setCustomKeyState(state: String) {
299
285
  customKeyButtonState = state
300
286
  customKeyButton?.isEnabled = state != "disable"
301
- updateCustomKeyUI(customKeyButtonBackground, customKeyButtonTextColor)
287
+ updateCustomKeyUI(customKeyButtonBackground, customKeyButtonTextColor, state)
302
288
  }
303
289
 
304
- private fun updateCustomKeyUI(background: Int, textColor: Int){
290
+ private fun updateCustomKeyUI(background: Int, textColor: Int, state: String){
291
+
305
292
  customKeyButton?.background = GradientDrawable().apply {
306
293
  shape = GradientDrawable.RECTANGLE
307
294
  cornerRadius = 24f
@@ -310,4 +297,5 @@ class CustomKeyboardView(
310
297
  customKeyButton?.setTextColor(textColor)
311
298
  }
312
299
 
300
+
313
301
  }
@@ -48,14 +48,9 @@ class RCTInputCalculator : ReactTextInputManager() {
48
48
  }
49
49
  }
50
50
 
51
- @ReactProp(name = "mode")
52
- fun setMode(view: ReactEditText, mode: String?) {
53
- keyboardView?.setMode(mode ?: "NumDefault")
54
- }
55
-
56
51
  @ReactProp(name = "customKeyText")
57
52
  fun setCustomKeyText(view: ReactEditText, text: String?) {
58
- keyboardView?.setCustomKeyText(text ?: "Tiếp")
53
+ keyboardView?.setCustomKeyText(text ?: "Xong")
59
54
  }
60
55
 
61
56
  @ReactProp(name = "customKeyBackground")
@@ -7,29 +7,14 @@ import Foundation
7
7
  class CalculatorKeyboardView: UIView {
8
8
  weak var input: InputCalculator?
9
9
 
10
- private let numWithCTAKeys: [[String]] = [
10
+ private let keys: [[String]] = [
11
11
  ["1", "2", "3", "÷", "back"],
12
12
  ["4", "5", "6", "×", "="],
13
- ["7", "8", "9", "-", "Tiếp"],
13
+ ["7", "8", "9", "-", "Xong"],
14
14
  ["000", "0", "+"],
15
15
  ]
16
-
17
- private let defaultKeys: [[String]] = [
18
- ["1", "2", "3", "÷", "AC"],
19
- ["4", "5", "6", "×", "back"],
20
- ["7", "8", "9", "-", "="],
21
- ["000", "0", "+"],
22
- ]
23
-
24
16
  private let SEPARATOR_WIDTH: CGFloat = 4
25
- private let specialKeys: Set<String> = ["=", "-", "×", "÷", "back", "+", "AC"]
26
-
27
- var keyboardMode: String? {
28
- didSet {
29
- guard oldValue != keyboardMode else { return }
30
- setup()
31
- }
32
- }
17
+ private let specialKeys: Set<String> = ["=", "-", "×", "÷", "back", "+"]
33
18
 
34
19
  var customKeyText: String? { didSet { updateCustomKeyTitle() } }
35
20
  var customKeyBackground: String? { didSet { updateCustomKeyBackground() } }
@@ -48,10 +33,10 @@ class CalculatorKeyboardView: UIView {
48
33
  }
49
34
 
50
35
  public func setKeyboardColor(_ color: UIColor) {
51
- self.setup()
36
+ self.setup(color)
52
37
  }
53
38
 
54
- private func setup() {
39
+ private func setup(_ color: UIColor = UIColor(hex: "#d8d8d8")) {
55
40
  self.subviews.forEach { $0.removeFromSuperview() }
56
41
 
57
42
  backgroundColor = UIColor(hex: "#f2f2f6")
@@ -73,26 +58,22 @@ class CalculatorKeyboardView: UIView {
73
58
 
74
59
  // Add buttons to the wrapper view
75
60
  var yOffset: CGFloat = 0
76
-
77
- let keys = keyboardMode == "NumDefault" ? defaultKeys : numWithCTAKeys
78
-
79
61
  for (rowIndex, row) in keys.enumerated() {
80
62
  var xOffset: CGFloat = 0
81
63
  for (colIndex, key) in row.enumerated() {
82
- let isMainKey = colIndex == 4 && rowIndex == 2
83
- let isMainCTAKey = isMainKey && keyboardMode == "NumWithCTA"
64
+ let isCustomKey = colIndex == 4 && rowIndex == 2
84
65
  let button = UIButton(type: .system)
85
66
  button.backgroundColor = UIColor.white
86
67
  button.layer.cornerRadius = 8
87
- let title = isMainCTAKey ? (customKeyText ?? key) : key
68
+ let title = isCustomKey ? (customKeyText ?? key) : key
88
69
  button.setTitle(title, for: .normal)
89
70
  button.setTitleColor(.black, for: .normal)
90
- button.titleLabel?.font = UIFont.systemFont(ofSize: isMainCTAKey ? 18 : 24, weight: .medium)
71
+ button.titleLabel?.font = UIFont.systemFont(ofSize: isCustomKey ? 18 : 24, weight: .medium)
91
72
  button.nativeID = key
92
- button.tag = isMainCTAKey ? 1 : 0
73
+ button.tag = isCustomKey ? 1 : 0
93
74
 
94
75
  var buttonFrame = CGRect(x: xOffset, y: yOffset, width: buttonWidth, height: buttonHeight)
95
- if isMainKey {
76
+ if isCustomKey {
96
77
  buttonFrame.size.height = buttonHeight * 2 + SEPARATOR_WIDTH
97
78
  }
98
79
  if key == "000" {
@@ -108,12 +89,12 @@ class CalculatorKeyboardView: UIView {
108
89
  button.tintColor = .black
109
90
  }
110
91
 
111
- if specialKeys.contains(key) || isMainKey {
92
+ if specialKeys.contains(key) {
112
93
  button.setTitleColor(.black, for: .normal)
113
- button.backgroundColor = UIColor(hex: "#d8d8d8")
94
+ button.backgroundColor = color
114
95
  }
115
96
 
116
- if isMainKey {
97
+ if isCustomKey {
117
98
  self.customKeyButton = button
118
99
  }
119
100
 
@@ -136,33 +117,29 @@ class CalculatorKeyboardView: UIView {
136
117
 
137
118
  private func updateCustomKeyBackground() {
138
119
  guard let btn = customKeyButton,
139
- let mode = keyboardMode,
140
120
  let background = customKeyBackground,
141
121
  let textColor = customKeyTextColor,
142
122
  let state = customKeyState else { return }
143
123
 
144
- if (mode == "numWithCTA") {
145
- btn.isEnabled = state != "disable"
146
- } else {
147
- btn.isEnabled = true
148
- }
124
+
125
+
126
+ btn.isEnabled = state != "disable"
149
127
  btn.backgroundColor = UIColor(hex: background)
150
128
  btn.setTitleColor(UIColor(hex: textColor), for: .normal)
129
+
151
130
  }
152
131
 
153
132
 
154
133
  @objc private func keyPressed(_ sender: UIButton) {
155
134
  guard let key = sender.nativeID else { return }
156
- let isCustomKeyCTA = sender.tag == 1
157
- if (isCustomKeyCTA) {
135
+ let isCustomKey = sender.tag == 1
136
+ if (isCustomKey) {
158
137
  input?.emitCustomKey()
159
138
  return
160
139
  }
161
140
 
162
141
  input?.emitKeyPress(key)
163
142
  switch key {
164
- case "AC":
165
- input?.clearText()
166
143
  case "back":
167
144
  input?.onBackSpace()
168
145
  case "=":
@@ -56,7 +56,7 @@ RCT_EXPORT_SHADOW_PROPERTY(onContentSizeChange, RCTDirectEventBlock)
56
56
  RCT_EXPORT_VIEW_PROPERTY(value, NSString)
57
57
  RCT_EXPORT_VIEW_PROPERTY(onFocus, RCTBubblingEventBlock)
58
58
  RCT_EXPORT_VIEW_PROPERTY(onBlur, RCTBubblingEventBlock)
59
- RCT_EXPORT_VIEW_PROPERTY(onKeyPress, RCTBubblingEventBlock)
59
+ RCT_EXPORT_VIEW_PROPERTY(onKeyPress, RCTBubblingEventBlock)
60
60
  RCT_EXPORT_VIEW_PROPERTY(onCustomKeyEvent, RCTDirectEventBlock)
61
61
 
62
62
  RCT_EXPORT_METHOD(focus : (nonnull NSNumber *)viewTag)
@@ -75,7 +75,6 @@ RCT_EXPORT_METHOD(blur : (nonnull NSNumber *)viewTag)
75
75
  }];
76
76
  }
77
77
 
78
- RCT_EXPORT_VIEW_PROPERTY(mode, NSString)
79
78
  RCT_EXPORT_VIEW_PROPERTY(keyboardColor, UIColor)
80
79
  RCT_EXPORT_VIEW_PROPERTY(customKeyText, NSString)
81
80
  RCT_EXPORT_VIEW_PROPERTY(customKeyBackground, NSString)
@@ -22,22 +22,18 @@ class InputCalculator: RCTSinglelineTextInputView {
22
22
  @objc var onKeyPress: RCTBubblingEventBlock?
23
23
  @objc var onCustomKeyEvent: RCTDirectEventBlock?
24
24
 
25
- @objc var mode: String? {
26
- didSet { keyboardView?.keyboardMode = mode }
27
- }
28
-
29
25
  @objc var customKeyText: String? {
30
- didSet { keyboardView?.customKeyText = customKeyText }
26
+ didSet { keyboardView?.customKeyText = customKeyText as String? }
31
27
  }
32
-
28
+
33
29
  @objc var customKeyBackground: String? {
34
30
  didSet { keyboardView?.customKeyBackground = customKeyBackground }
35
31
  }
36
-
32
+
37
33
  @objc var customKeyTextColor: String? {
38
34
  didSet { keyboardView?.customKeyTextColor = customKeyTextColor }
39
35
  }
40
-
36
+
41
37
  @objc var customKeyState: String? {
42
38
  didSet { keyboardView?.customKeyState = customKeyState }
43
39
  }
@@ -101,23 +97,23 @@ class InputCalculator: RCTSinglelineTextInputView {
101
97
  func keyDidPress(_ key: String) {
102
98
  backedTextInputView.insertText(key)
103
99
  value += key
104
-
100
+
105
101
  if let bridge = bridge {
106
102
  bridge.eventDispatcher().sendTextEvent(with: .change, reactTag: reactTag, text: value, key: "\(key)", eventCount: 1)
107
103
  }
108
104
  }
109
105
 
110
- func clearText() {
111
- value = ""
112
- (backedTextInputView as? UITextField)?.text = ""
113
- if let bridge = bridge {
114
- bridge.eventDispatcher().sendTextEvent(with: .change, reactTag: reactTag, text: value, key: "clear", eventCount: 1)
115
- }
116
- }
117
-
118
106
  func onBackSpace() {
119
107
  value = value.dropLast().description
120
-
108
+ DispatchQueue.main.async {
109
+ if let range = self.backedTextInputView.selectedTextRange,
110
+ let fromRange = self.backedTextInputView.position(from: range.start, offset: -1),
111
+ let newRange = self.backedTextInputView.textRange(from: fromRange, to: range.start)
112
+ {
113
+ self.backedTextInputView.replace(newRange, withText: "")
114
+ }
115
+ }
116
+
121
117
  if let bridge = bridge {
122
118
  bridge.eventDispatcher().sendTextEvent(with: .change, reactTag: reactTag, text: value, key: "back", eventCount: 1)
123
119
  }
@@ -139,7 +135,7 @@ class InputCalculator: RCTSinglelineTextInputView {
139
135
  if let result = expression.expressionValue(with: nil, context: nil) as? NSNumber {
140
136
  textField.text = result.stringValue
141
137
  value = result.stringValue
142
-
138
+
143
139
  if let bridge = bridge {
144
140
  bridge.eventDispatcher().sendTextEvent(with: .change, reactTag: reactTag, text: value, key: "=", eventCount: 1)
145
141
  }
@@ -149,11 +145,11 @@ class InputCalculator: RCTSinglelineTextInputView {
149
145
  print("Invalid expression")
150
146
  }
151
147
  }
152
-
148
+
153
149
  func emitCustomKey() {
154
150
  onCustomKeyEvent?([:])
155
151
  }
156
-
152
+
157
153
  func emitKeyPress(_ key: String) {
158
154
  onKeyPress?(["key": key])
159
155
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/calculator-keyboard",
3
- "version": "0.150.2-beta.19",
3
+ "version": "0.150.2-beta.20",
4
4
  "description": "react native calculator keyboard",
5
5
  "main": "./src/index.tsx",
6
6
  "files": [
@@ -27,7 +27,7 @@
27
27
  "typecheck": "tsc",
28
28
  "lint": "eslint \"**/*.{js,ts,tsx}\"",
29
29
  "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
30
- "build": "echo",
30
+ "build": "echo 'build step'",
31
31
  "release": "release-it"
32
32
  },
33
33
  "keywords": [
package/src/index.tsx CHANGED
@@ -18,7 +18,6 @@ type KeyPressEvent = { nativeEvent: { key: string } };
18
18
  interface InputCalculatorProps extends TextInputProps {
19
19
  text?: string | undefined;
20
20
  keyboardColor?: ColorValue;
21
- mode?: Mode;
22
21
  onKeyPress?: (e: KeyPressEvent) => void;
23
22
  customKeyText?: string | undefined;
24
23
  customKeyBackground?: CustomKeyBackground;
@@ -33,11 +32,6 @@ export enum CustomKeyState {
33
32
  Disable = 'disable',
34
33
  }
35
34
 
36
- export enum Mode {
37
- NumDefault = 'NumDefault',
38
- NumWithCTA = 'NumWithCTA',
39
- }
40
-
41
35
  export type InputCalculatorRef = {
42
36
  focus: () => void;
43
37
  blur: () => void;
@@ -51,7 +45,6 @@ const InputCalculator = React.forwardRef<
51
45
  {
52
46
  customKeyBackground = 'default',
53
47
  keyboardColor = '',
54
- mode = Mode.NumDefault,
55
48
  customKeyText,
56
49
  onKeyPress,
57
50
  customKeyState = CustomKeyState.Default,
@@ -73,16 +66,14 @@ const InputCalculator = React.forwardRef<
73
66
  let keyBackground = Colors.black_06;
74
67
  let textKeyColor = Colors.black_20;
75
68
 
76
- if (mode === Mode.NumWithCTA) {
77
- if (customKeyBackground === 'primary') {
78
- keyBackground = theme.colors.primary;
79
- textKeyColor = Colors.black_01;
80
- }
69
+ if (customKeyBackground === 'primary') {
70
+ keyBackground = theme.colors.primary;
71
+ textKeyColor = Colors.black_01;
72
+ }
81
73
 
82
- if (customKeyState === CustomKeyState.Disable) {
83
- keyBackground = theme.colors.background.disable;
84
- textKeyColor = Colors.black_01;
85
- }
74
+ if (customKeyState === CustomKeyState.Disable) {
75
+ keyBackground = theme.colors.background.disable;
76
+ textKeyColor = Colors.black_01;
86
77
  }
87
78
 
88
79
  React.useImperativeHandle(ref, () => ({
@@ -111,7 +102,6 @@ const InputCalculator = React.forwardRef<
111
102
  onChange={_onChange}
112
103
  onKeyPress={onKeyPress}
113
104
  value={text}
114
- mode={mode}
115
105
  keybardColor={processColor(keyboardColor)}
116
106
  customKeyText={customKeyText}
117
107
  customKeyBackground={keyBackground}