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

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