@momo-kits/calculator-keyboard 0.150.2-beta.21 → 0.150.2-beta.22
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.
- package/README.md +3 -45
- package/android/src/main/java/com/calculatorkeyboard/CalculatorKeyboardPackage.kt +1 -1
- package/android/src/main/java/com/calculatorkeyboard/CustomKeyboardView.kt +77 -68
- package/android/src/main/java/com/calculatorkeyboard/{InputCalculatorViewManager.kt → RCTInputCalculator.kt} +27 -100
- package/ios/CalculatorKeyboardView.swift +153 -0
- package/ios/InputCalculator-Bridging-Header.h +23 -0
- package/ios/InputCalculator.m +85 -0
- package/ios/InputCalculator.swift +158 -0
- package/ios/extension.swift +23 -0
- package/package.json +8 -19
- package/react-native-calculator-keyboard.podspec +4 -5
- package/src/index.tsx +30 -58
- package/android/src/main/java/com/calculatorkeyboard/Event.kt +0 -36
- package/ios/CalculatorKeyboardView.h +0 -30
- package/ios/CalculatorKeyboardView.mm +0 -231
- package/ios/NativeInputCalculator.h +0 -11
- package/ios/NativeInputCalculator.mm +0 -230
- package/src/InputCalculatorNativeComponent.ts +0 -62
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
package com.calculatorkeyboard
|
|
2
|
-
|
|
3
|
-
import com.facebook.react.bridge.WritableMap
|
|
4
|
-
import com.facebook.react.uimanager.events.Event
|
|
5
|
-
|
|
6
|
-
class OnChangeEvent(
|
|
7
|
-
surfaceId: Int,
|
|
8
|
-
viewId: Int,
|
|
9
|
-
private val payload: WritableMap
|
|
10
|
-
) : Event<OnChangeEvent>(surfaceId, viewId) {
|
|
11
|
-
override fun getEventName() = "onChange"
|
|
12
|
-
|
|
13
|
-
override fun getEventData() = payload
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
class OnKeyPressEvent(
|
|
17
|
-
surfaceId: Int,
|
|
18
|
-
viewId: Int,
|
|
19
|
-
private val payload: WritableMap
|
|
20
|
-
) : Event<OnChangeEvent>(surfaceId, viewId) {
|
|
21
|
-
override fun getEventName() = "onKeyPress"
|
|
22
|
-
|
|
23
|
-
override fun getEventData() = payload
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
class OnCustomKeyPressEvent(
|
|
27
|
-
surfaceId: Int,
|
|
28
|
-
viewId: Int
|
|
29
|
-
) : Event<OnChangeEvent>(surfaceId, viewId) {
|
|
30
|
-
override fun getEventName() = "onCustomKeyEvent"
|
|
31
|
-
|
|
32
|
-
override fun getEventData() = null
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
#import <UIKit/UIKit.h>
|
|
2
|
-
|
|
3
|
-
NS_ASSUME_NONNULL_BEGIN
|
|
4
|
-
|
|
5
|
-
@protocol CalculatorKeyboardInput <NSObject>
|
|
6
|
-
|
|
7
|
-
- (void)keyDidPress:(NSString *)key;
|
|
8
|
-
- (void)clearText;
|
|
9
|
-
- (void)onBackSpace;
|
|
10
|
-
- (void)calculateResult;
|
|
11
|
-
- (void)emitCustomKey;
|
|
12
|
-
- (void)emitKeyPress:(NSString *)key;
|
|
13
|
-
|
|
14
|
-
@end
|
|
15
|
-
|
|
16
|
-
@interface CalculatorKeyboardView : UIView
|
|
17
|
-
|
|
18
|
-
@property (nonatomic, weak, nullable) id<CalculatorKeyboardInput> input;
|
|
19
|
-
@property (nonatomic, strong, nullable) NSString *keyboardMode;
|
|
20
|
-
@property (nonatomic, strong, nullable) NSString *customKeyText;
|
|
21
|
-
@property (nonatomic, strong, nullable) NSString *customKeyBackground;
|
|
22
|
-
@property (nonatomic, strong, nullable) NSString *customKeyTextColor;
|
|
23
|
-
@property (nonatomic, strong, nullable) NSString *customKeyState;
|
|
24
|
-
|
|
25
|
-
- (void)setKeyboardColor:(UIColor *)color;
|
|
26
|
-
|
|
27
|
-
@end
|
|
28
|
-
|
|
29
|
-
NS_ASSUME_NONNULL_END
|
|
30
|
-
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
#import "CalculatorKeyboardView.h"
|
|
2
|
-
|
|
3
|
-
@interface CalculatorKeyboardView ()
|
|
4
|
-
@property (nonatomic, strong) NSArray<NSArray<NSString *> *> *numWithCTAKeys;
|
|
5
|
-
@property (nonatomic, strong) NSArray<NSArray<NSString *> *> *defaultKeys;
|
|
6
|
-
@property (nonatomic, strong) NSSet<NSString *> *specialKeys;
|
|
7
|
-
@property (nonatomic, weak) UIButton *customKeyButton;
|
|
8
|
-
@end
|
|
9
|
-
|
|
10
|
-
@implementation CalculatorKeyboardView {
|
|
11
|
-
CGFloat _separatorWidth;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
- (instancetype)initWithFrame:(CGRect)frame
|
|
15
|
-
{
|
|
16
|
-
if (self = [super initWithFrame:frame]) {
|
|
17
|
-
_separatorWidth = 4.0;
|
|
18
|
-
|
|
19
|
-
_numWithCTAKeys = @[
|
|
20
|
-
@[@"1", @"2", @"3", @"÷", @"back"],
|
|
21
|
-
@[@"4", @"5", @"6", @"×", @"="],
|
|
22
|
-
@[@"7", @"8", @"9", @"-", @"Tiếp"],
|
|
23
|
-
@[@"000", @"0", @"+"]
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
_defaultKeys = @[
|
|
27
|
-
@[@"1", @"2", @"3", @"÷", @"AC"],
|
|
28
|
-
@[@"4", @"5", @"6", @"×", @"back"],
|
|
29
|
-
@[@"7", @"8", @"9", @"-", @"="],
|
|
30
|
-
@[@"000", @"0", @"+"]
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
_specialKeys = [NSSet setWithArray:@[@"=", @"-", @"×", @"÷", @"back", @"+", @"AC"]];
|
|
34
|
-
|
|
35
|
-
[self setup];
|
|
36
|
-
}
|
|
37
|
-
return self;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
- (void)setKeyboardMode:(NSString *)keyboardMode
|
|
41
|
-
{
|
|
42
|
-
if ([_keyboardMode isEqualToString:keyboardMode]) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
_keyboardMode = keyboardMode;
|
|
46
|
-
[self setup];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
- (void)setCustomKeyText:(NSString *)customKeyText
|
|
50
|
-
{
|
|
51
|
-
_customKeyText = customKeyText;
|
|
52
|
-
[self updateCustomKeyTitle];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
- (void)setCustomKeyBackground:(NSString *)customKeyBackground
|
|
56
|
-
{
|
|
57
|
-
_customKeyBackground = customKeyBackground;
|
|
58
|
-
[self updateCustomKeyBackground];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
- (void)setCustomKeyTextColor:(NSString *)customKeyTextColor
|
|
62
|
-
{
|
|
63
|
-
_customKeyTextColor = customKeyTextColor;
|
|
64
|
-
[self updateCustomKeyBackground];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
- (void)setCustomKeyState:(NSString *)customKeyState
|
|
68
|
-
{
|
|
69
|
-
_customKeyState = customKeyState;
|
|
70
|
-
[self updateCustomKeyBackground];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
- (void)setKeyboardColor:(UIColor *)color
|
|
74
|
-
{
|
|
75
|
-
[self setup];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
- (void)setup
|
|
79
|
-
{
|
|
80
|
-
// Remove all subviews
|
|
81
|
-
for (UIView *subview in self.subviews) {
|
|
82
|
-
[subview removeFromSuperview];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
self.backgroundColor = [self colorFromHex:@"#f2f2f6"];
|
|
86
|
-
|
|
87
|
-
CGFloat buttonWidth = (UIScreen.mainScreen.bounds.size.width - _separatorWidth * 2 - 4 * _separatorWidth) / 5;
|
|
88
|
-
CGFloat buttonHeight = (240 - _separatorWidth * 2 - 3 * _separatorWidth) / 4;
|
|
89
|
-
|
|
90
|
-
// Create content view
|
|
91
|
-
UIView *contentView = [[UIView alloc] init];
|
|
92
|
-
contentView.translatesAutoresizingMaskIntoConstraints = NO;
|
|
93
|
-
[self addSubview:contentView];
|
|
94
|
-
|
|
95
|
-
// Set contentView constraints
|
|
96
|
-
[NSLayoutConstraint activateConstraints:@[
|
|
97
|
-
[contentView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:_separatorWidth],
|
|
98
|
-
[contentView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-_separatorWidth],
|
|
99
|
-
[contentView.topAnchor constraintEqualToAnchor:self.topAnchor constant:_separatorWidth],
|
|
100
|
-
[contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-_separatorWidth]
|
|
101
|
-
]];
|
|
102
|
-
|
|
103
|
-
CGFloat yOffset = 0;
|
|
104
|
-
NSArray<NSArray<NSString *> *> *keys = [_keyboardMode isEqualToString:@"NumDefault"] ? _defaultKeys : _numWithCTAKeys;
|
|
105
|
-
|
|
106
|
-
for (NSInteger rowIndex = 0; rowIndex < keys.count; rowIndex++) {
|
|
107
|
-
NSArray<NSString *> *row = keys[rowIndex];
|
|
108
|
-
CGFloat xOffset = 0;
|
|
109
|
-
|
|
110
|
-
for (NSInteger colIndex = 0; colIndex < row.count; colIndex++) {
|
|
111
|
-
NSString *key = row[colIndex];
|
|
112
|
-
BOOL isMainKey = (colIndex == 4 && rowIndex == 2);
|
|
113
|
-
BOOL isMainCTAKey = isMainKey && [_keyboardMode isEqualToString:@"NumWithCTA"];
|
|
114
|
-
|
|
115
|
-
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
|
|
116
|
-
button.backgroundColor = UIColor.whiteColor;
|
|
117
|
-
button.layer.cornerRadius = 8;
|
|
118
|
-
|
|
119
|
-
NSString *title = isMainCTAKey ? (_customKeyText ?: key) : key;
|
|
120
|
-
[button setTitle:title forState:UIControlStateNormal];
|
|
121
|
-
[button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
|
|
122
|
-
button.titleLabel.font = [UIFont systemFontOfSize:isMainCTAKey ? 18 : 24 weight:UIFontWeightMedium];
|
|
123
|
-
button.accessibilityIdentifier = key;
|
|
124
|
-
button.tag = isMainCTAKey ? 1 : 0;
|
|
125
|
-
|
|
126
|
-
CGRect buttonFrame = CGRectMake(xOffset, yOffset, buttonWidth, buttonHeight);
|
|
127
|
-
if (isMainKey) {
|
|
128
|
-
buttonFrame.size.height = buttonHeight * 2 + _separatorWidth;
|
|
129
|
-
}
|
|
130
|
-
if ([key isEqualToString:@"000"]) {
|
|
131
|
-
buttonFrame.size.width = buttonWidth * 2 + _separatorWidth;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
button.frame = buttonFrame;
|
|
135
|
-
|
|
136
|
-
if ([key isEqualToString:@"back"]) {
|
|
137
|
-
[button setTitle:@"" forState:UIControlStateNormal];
|
|
138
|
-
UIImageSymbolConfiguration *config = [UIImageSymbolConfiguration configurationWithWeight:UIImageSymbolWeightBold];
|
|
139
|
-
UIImage *image = [UIImage systemImageNamed:@"delete.backward" withConfiguration:config];
|
|
140
|
-
[button setImage:image forState:UIControlStateNormal];
|
|
141
|
-
button.tintColor = UIColor.blackColor;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if ([_specialKeys containsObject:key] || isMainKey) {
|
|
145
|
-
[button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
|
|
146
|
-
button.backgroundColor = [self colorFromHex:@"#d8d8d8"];
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (isMainKey) {
|
|
150
|
-
self.customKeyButton = button;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
[button addTarget:self action:@selector(keyPressed:) forControlEvents:UIControlEventTouchUpInside];
|
|
154
|
-
[contentView addSubview:button];
|
|
155
|
-
|
|
156
|
-
xOffset += buttonFrame.size.width + _separatorWidth;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
yOffset += buttonHeight + _separatorWidth;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
- (void)updateCustomKeyTitle
|
|
164
|
-
{
|
|
165
|
-
if (!self.customKeyButton || !_customKeyText) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
[self.customKeyButton setTitle:_customKeyText forState:UIControlStateNormal];
|
|
169
|
-
[self.customKeyButton setImage:nil forState:UIControlStateNormal];
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
- (void)updateCustomKeyBackground
|
|
173
|
-
{
|
|
174
|
-
if (!self.customKeyButton || !_keyboardMode || !_customKeyBackground || !_customKeyTextColor || !_customKeyState) {
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if ([_keyboardMode isEqualToString:@"numWithCTA"]) {
|
|
179
|
-
self.customKeyButton.enabled = ![_customKeyState isEqualToString:@"disable"];
|
|
180
|
-
} else {
|
|
181
|
-
self.customKeyButton.enabled = YES;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
self.customKeyButton.backgroundColor = [self colorFromHex:_customKeyBackground];
|
|
185
|
-
[self.customKeyButton setTitleColor:[self colorFromHex:_customKeyTextColor] forState:UIControlStateNormal];
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
- (void)keyPressed:(UIButton *)sender
|
|
189
|
-
{
|
|
190
|
-
NSString *key = sender.accessibilityIdentifier;
|
|
191
|
-
if (!key) {
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
BOOL isCustomKeyCTA = (sender.tag == 1);
|
|
196
|
-
if (isCustomKeyCTA) {
|
|
197
|
-
[self.input emitCustomKey];
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
[self.input emitKeyPress:key];
|
|
202
|
-
|
|
203
|
-
if ([key isEqualToString:@"AC"]) {
|
|
204
|
-
[self.input clearText];
|
|
205
|
-
} else if ([key isEqualToString:@"back"]) {
|
|
206
|
-
[self.input onBackSpace];
|
|
207
|
-
} else if ([key isEqualToString:@"="]) {
|
|
208
|
-
[self.input calculateResult];
|
|
209
|
-
} else if ([key isEqualToString:@"+"] || [key isEqualToString:@"-"] ||
|
|
210
|
-
[key isEqualToString:@"÷"] || [key isEqualToString:@"×"]) {
|
|
211
|
-
[self.input keyDidPress:[NSString stringWithFormat:@" %@ ", key]];
|
|
212
|
-
} else {
|
|
213
|
-
[self.input keyDidPress:key];
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
- (UIColor *)colorFromHex:(NSString *)hexString
|
|
218
|
-
{
|
|
219
|
-
unsigned rgbValue = 0;
|
|
220
|
-
NSString *cleanHex = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
|
|
221
|
-
NSScanner *scanner = [NSScanner scannerWithString:cleanHex];
|
|
222
|
-
[scanner scanHexInt:&rgbValue];
|
|
223
|
-
|
|
224
|
-
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0
|
|
225
|
-
green:((rgbValue & 0x00FF00) >> 8) / 255.0
|
|
226
|
-
blue:(rgbValue & 0x0000FF) / 255.0
|
|
227
|
-
alpha:1.0];
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
@end
|
|
231
|
-
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
#import "NativeInputCalculator.h"
|
|
2
|
-
#import "CalculatorKeyboardView.h"
|
|
3
|
-
|
|
4
|
-
#import <React/RCTConversions.h>
|
|
5
|
-
#import <React/RCTFabricComponentsPlugins.h>
|
|
6
|
-
#import <react/renderer/components/CalculatorKeyboardSpecs/ComponentDescriptors.h>
|
|
7
|
-
#import <react/renderer/components/CalculatorKeyboardSpecs/EventEmitters.h>
|
|
8
|
-
#import <react/renderer/components/CalculatorKeyboardSpecs/Props.h>
|
|
9
|
-
#import <react/renderer/components/CalculatorKeyboardSpecs/RCTComponentViewHelpers.h>
|
|
10
|
-
|
|
11
|
-
using namespace facebook::react;
|
|
12
|
-
|
|
13
|
-
@interface NativeInputCalculator () <RCTNativeInputCalculatorViewProtocol>
|
|
14
|
-
@end
|
|
15
|
-
|
|
16
|
-
@implementation NativeInputCalculator {
|
|
17
|
-
UITextField *_textField;
|
|
18
|
-
CalculatorKeyboardView *_keyboardView;
|
|
19
|
-
NSString *_lastValue;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
- (instancetype)initWithFrame:(CGRect)frame
|
|
23
|
-
{
|
|
24
|
-
if (self = [super initWithFrame:frame]) {
|
|
25
|
-
static const auto defaultProps = std::make_shared<const NativeInputCalculatorProps>();
|
|
26
|
-
_props = defaultProps;
|
|
27
|
-
|
|
28
|
-
// Create text field
|
|
29
|
-
_textField = [[UITextField alloc] initWithFrame:self.bounds];
|
|
30
|
-
_textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
31
|
-
_textField.delegate = (id<UITextFieldDelegate>)self;
|
|
32
|
-
|
|
33
|
-
// Create keyboard view
|
|
34
|
-
CGFloat bottomInset = [self getBottomInset];
|
|
35
|
-
CGRect keyboardFrame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 240 + bottomInset);
|
|
36
|
-
_keyboardView = [[CalculatorKeyboardView alloc] initWithFrame:keyboardFrame];
|
|
37
|
-
_keyboardView.input = (id)self; // Bridge pattern
|
|
38
|
-
|
|
39
|
-
// Set custom keyboard
|
|
40
|
-
_textField.inputView = _keyboardView;
|
|
41
|
-
|
|
42
|
-
[self addSubview:_textField];
|
|
43
|
-
}
|
|
44
|
-
return self;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
- (CGFloat)getBottomInset
|
|
48
|
-
{
|
|
49
|
-
UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
|
|
50
|
-
CGFloat bottom = window.safeAreaInsets.bottom > 0 ? 21 : 0;
|
|
51
|
-
return bottom;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
#pragma mark - RCTComponentViewProtocol
|
|
55
|
-
|
|
56
|
-
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
|
57
|
-
{
|
|
58
|
-
return concreteComponentDescriptorProvider<NativeInputCalculatorComponentDescriptor>();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
|
|
62
|
-
{
|
|
63
|
-
const auto &oldViewProps = *std::static_pointer_cast<const NativeInputCalculatorProps>(_props);
|
|
64
|
-
const auto &newViewProps = *std::static_pointer_cast<const NativeInputCalculatorProps>(props);
|
|
65
|
-
|
|
66
|
-
// Update value
|
|
67
|
-
if (oldViewProps.value != newViewProps.value) {
|
|
68
|
-
NSString *newValue = RCTNSStringFromString(newViewProps.value);
|
|
69
|
-
if (![_lastValue isEqualToString:newValue]) {
|
|
70
|
-
_textField.text = newValue;
|
|
71
|
-
_lastValue = newValue;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Update mode
|
|
76
|
-
if (oldViewProps.mode != newViewProps.mode) {
|
|
77
|
-
_keyboardView.keyboardMode = RCTNSStringFromString(newViewProps.mode);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Update customKeyText
|
|
81
|
-
if (oldViewProps.customKeyText != newViewProps.customKeyText) {
|
|
82
|
-
_keyboardView.customKeyText = RCTNSStringFromString(newViewProps.customKeyText);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Update customKeyBackground
|
|
86
|
-
if (oldViewProps.customKeyBackground != newViewProps.customKeyBackground) {
|
|
87
|
-
_keyboardView.customKeyBackground = RCTNSStringFromString(newViewProps.customKeyBackground);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Update customKeyTextColor
|
|
91
|
-
if (oldViewProps.customKeyTextColor != newViewProps.customKeyTextColor) {
|
|
92
|
-
_keyboardView.customKeyTextColor = RCTNSStringFromString(newViewProps.customKeyTextColor);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Update customKeyState
|
|
96
|
-
if (oldViewProps.customKeyState != newViewProps.customKeyState) {
|
|
97
|
-
_keyboardView.customKeyState = RCTNSStringFromString(newViewProps.customKeyState);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// ===== textAttributes (fontSize, fontWeight) =====
|
|
101
|
-
// Codegen should have a prop like: std::optional<folly::dynamic> textAttributes;
|
|
102
|
-
if (oldViewProps.textAttributes.fontSize != newViewProps.textAttributes.fontSize > 0.0f) {
|
|
103
|
-
CGFloat newSize = (CGFloat)newViewProps.textAttributes.fontSize;
|
|
104
|
-
UIFont *current = _textField.font ?: [UIFont systemFontOfSize:UIFont.systemFontSize];
|
|
105
|
-
_textField.font = [current fontWithSize:newSize];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (oldViewProps.textAttributes.fontWeight != newViewProps.textAttributes.fontWeight) {
|
|
109
|
-
UIFontWeight weight = _UIFontWeightFromString(newViewProps.textAttributes.fontWeight);
|
|
110
|
-
CGFloat size = _textField.font ? _textField.font.pointSize : UIFont.systemFontSize;
|
|
111
|
-
_textField.font = [UIFont systemFontOfSize:size weight:weight];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
[super updateProps:props oldProps:oldProps];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
static UIFontWeight _UIFontWeightFromString(std::string_view s) {
|
|
118
|
-
std::string v(s);
|
|
119
|
-
for (auto &c : v) c = (char)tolower((unsigned char)c);
|
|
120
|
-
|
|
121
|
-
if (v == "normal" || v == "400") return UIFontWeightRegular;
|
|
122
|
-
if (v == "bold" || v == "700") return UIFontWeightBold;
|
|
123
|
-
if (v == "100") return UIFontWeightUltraLight;
|
|
124
|
-
if (v == "200") return UIFontWeightThin;
|
|
125
|
-
if (v == "300") return UIFontWeightLight;
|
|
126
|
-
if (v == "500") return UIFontWeightMedium;
|
|
127
|
-
if (v == "600") return UIFontWeightSemibold;
|
|
128
|
-
if (v == "800") return UIFontWeightHeavy;
|
|
129
|
-
if (v == "900") return UIFontWeightBlack;
|
|
130
|
-
return UIFontWeightRegular;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
|
|
134
|
-
{
|
|
135
|
-
RCTNativeInputCalculatorHandleCommand(self, commandName, args);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
#pragma mark - RCTNativeInputCalculatorViewProtocol
|
|
139
|
-
|
|
140
|
-
- (void)focus
|
|
141
|
-
{
|
|
142
|
-
[_textField becomeFirstResponder];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
- (void)blur
|
|
146
|
-
{
|
|
147
|
-
[_textField resignFirstResponder];
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
#pragma mark - Keyboard callbacks (called by CalculatorKeyboardView)
|
|
151
|
-
|
|
152
|
-
- (void)keyDidPress:(NSString *)key
|
|
153
|
-
{
|
|
154
|
-
[_textField insertText:key];
|
|
155
|
-
[self notifyTextChange];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
- (void)clearText
|
|
159
|
-
{
|
|
160
|
-
_textField.text = @"";
|
|
161
|
-
[self notifyTextChange];
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
- (void)onBackSpace
|
|
165
|
-
{
|
|
166
|
-
if (_textField.text.length > 0) {
|
|
167
|
-
_textField.text = [_textField.text substringToIndex:_textField.text.length - 1];
|
|
168
|
-
[self notifyTextChange];
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
- (void)calculateResult
|
|
173
|
-
{
|
|
174
|
-
NSString *text = [_textField.text stringByReplacingOccurrencesOfString:@"×" withString:@"*"];
|
|
175
|
-
text = [text stringByReplacingOccurrencesOfString:@"÷" withString:@"/"];
|
|
176
|
-
|
|
177
|
-
NSString *pattern = @"^\\s*(-?\\d+(\\.\\d+)?\\s*[-+*/]\\s*)*-?\\d+(\\.\\d+)?\\s*$";
|
|
178
|
-
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
|
|
179
|
-
NSRange range = NSMakeRange(0, text.length);
|
|
180
|
-
|
|
181
|
-
if ([regex firstMatchInString:text options:0 range:range]) {
|
|
182
|
-
NSExpression *expression = [NSExpression expressionWithFormat:text];
|
|
183
|
-
id result = [expression expressionValueWithObject:nil context:nil];
|
|
184
|
-
if ([result isKindOfClass:[NSNumber class]]) {
|
|
185
|
-
_textField.text = [result stringValue];
|
|
186
|
-
[self notifyTextChange];
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
- (void)emitCustomKey
|
|
192
|
-
{
|
|
193
|
-
if (_eventEmitter) {
|
|
194
|
-
auto emitter = std::static_pointer_cast<const NativeInputCalculatorEventEmitter>(_eventEmitter);
|
|
195
|
-
NativeInputCalculatorEventEmitter::OnCustomKeyEvent event{};
|
|
196
|
-
emitter->onCustomKeyEvent(event);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
- (void)emitKeyPress:(NSString *)key
|
|
201
|
-
{
|
|
202
|
-
if (_eventEmitter) {
|
|
203
|
-
auto emitter = std::static_pointer_cast<const NativeInputCalculatorEventEmitter>(_eventEmitter);
|
|
204
|
-
NativeInputCalculatorEventEmitter::OnKeyPress event{
|
|
205
|
-
.key = std::string([key UTF8String])
|
|
206
|
-
};
|
|
207
|
-
emitter->onKeyPress(event);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
- (void)notifyTextChange
|
|
212
|
-
{
|
|
213
|
-
_lastValue = _textField.text;
|
|
214
|
-
|
|
215
|
-
if (_eventEmitter) {
|
|
216
|
-
auto emitter = std::static_pointer_cast<const NativeInputCalculatorEventEmitter>(_eventEmitter);
|
|
217
|
-
NativeInputCalculatorEventEmitter::OnChange event{
|
|
218
|
-
.text = std::string([_textField.text UTF8String])
|
|
219
|
-
};
|
|
220
|
-
emitter->onChange(event);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
@end
|
|
225
|
-
|
|
226
|
-
Class<RCTNativeInputCalculatorViewProtocol> NativeInputCalculatorCls(void)
|
|
227
|
-
{
|
|
228
|
-
return NativeInputCalculator.class;
|
|
229
|
-
}
|
|
230
|
-
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import type * as React from 'react';
|
|
2
|
-
import type { HostComponent, ViewProps } from 'react-native';
|
|
3
|
-
import {
|
|
4
|
-
CodegenTypes,
|
|
5
|
-
codegenNativeComponent,
|
|
6
|
-
codegenNativeCommands,
|
|
7
|
-
} from 'react-native';
|
|
8
|
-
|
|
9
|
-
export type CustomKeyBackground = 'primary' | 'default' | string;
|
|
10
|
-
|
|
11
|
-
export enum CustomKeyState {
|
|
12
|
-
Default = 'default',
|
|
13
|
-
Disable = 'disable',
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export enum Mode {
|
|
17
|
-
NumDefault = 'NumDefault',
|
|
18
|
-
NumWithCTA = 'NumWithCTA',
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type OnKeyPressEvent = Readonly<{
|
|
22
|
-
key: string;
|
|
23
|
-
}>;
|
|
24
|
-
|
|
25
|
-
export type OnChangeEvent = Readonly<{
|
|
26
|
-
text: string;
|
|
27
|
-
}>;
|
|
28
|
-
|
|
29
|
-
export type TextAttributes = Readonly<{
|
|
30
|
-
fontSize?: CodegenTypes.Float;
|
|
31
|
-
fontWeight?: string;
|
|
32
|
-
}>;
|
|
33
|
-
|
|
34
|
-
export interface NativeInputCalculatorProps extends ViewProps {
|
|
35
|
-
value?: string;
|
|
36
|
-
textAttributes?: TextAttributes;
|
|
37
|
-
mode?: string;
|
|
38
|
-
customKeyText?: string;
|
|
39
|
-
customKeyBackground?: string;
|
|
40
|
-
customKeyTextColor?: string;
|
|
41
|
-
customKeyState?: string;
|
|
42
|
-
onChange?: CodegenTypes.BubblingEventHandler<OnChangeEvent>;
|
|
43
|
-
onKeyPress?: CodegenTypes.BubblingEventHandler<OnKeyPressEvent>;
|
|
44
|
-
onCustomKeyEvent?: CodegenTypes.DirectEventHandler<{}>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface NativeInputCalculatorCommands {
|
|
48
|
-
focus(
|
|
49
|
-
viewRef: React.ElementRef<HostComponent<NativeInputCalculatorProps>>,
|
|
50
|
-
): void;
|
|
51
|
-
blur(
|
|
52
|
-
viewRef: React.ElementRef<HostComponent<NativeInputCalculatorProps>>,
|
|
53
|
-
): void;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export const Commands = codegenNativeCommands<NativeInputCalculatorCommands>({
|
|
57
|
-
supportedCommands: ['focus', 'blur'],
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
export default codegenNativeComponent<NativeInputCalculatorProps>(
|
|
61
|
-
'NativeInputCalculator',
|
|
62
|
-
);
|