Skip to content

Commit 4caec22

Browse files
Let's make sure the delegate is there. Fi. with a SearchBar it's not.
1 parent 84be2a4 commit 4caec22

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.vscode
33
*.js
44
*.log
5+
*.tgz
56
!src/angular/*.js
67
!demo/karma.conf.js
78
!demo/app/tests/*.js
@@ -11,4 +12,4 @@ src/*.d.ts
1112
!src/references.d.ts
1213
demo/lib
1314
demo/platforms
14-
node_modules
15+
node_modules

demo/app/main-page.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
1-
import * as observable from "data/observable";
2-
import * as pages from "ui/page";
3-
import { HelloWorldModel } from "./main-view-model";
4-
import { NumericKeyboard } from "nativescript-numeric-keyboard";
51
import { TextField } from "tns-core-modules/ui/text-field";
62
import { EventData } from "tns-core-modules/data/observable";
3+
import { SearchBar } from "tns-core-modules/ui/search-bar";
4+
import { isIOS, Page } from "tns-core-modules/ui/page";
5+
import { HelloWorldModel } from "./main-view-model";
6+
import { NumericKeyboard } from "nativescript-numeric-keyboard";
77

88
// Event handler for Page 'loaded' event attached in main-page.xml
9-
export function pageLoaded(args: observable.EventData) {
10-
let page = <pages.Page>args.object;
9+
export function pageLoaded(args: EventData) {
10+
let page = <Page>args.object;
1111
page.bindingContext = new HelloWorldModel();
1212

1313
const textField = <TextField>page.getViewById("defaultPluginKeyboard");
14-
1514
new NumericKeyboard().decorate({
1615
textField: textField,
1716
returnKeyTitle: "Go!",
1817
locale: "en_US",
1918
noDecimals: true,
2019
noIpadInputBar: true
2120
});
21+
22+
if (isIOS) {
23+
const searchBar = <SearchBar>page.getViewById("searchBar");
24+
25+
searchBar.on("textChange", (args: EventData) => {
26+
let sb = <SearchBar>args.object;
27+
console.log("searchBar, text entered: " + sb.text);
28+
});
29+
30+
const textFieldContainerSubviews = searchBar.ios.subviews.objectAtIndex(0).subviews;
31+
if (textFieldContainerSubviews.count > 1) {
32+
new NumericKeyboard().decorate({
33+
textField: textFieldContainerSubviews.objectAtIndex(1),
34+
noReturnKey: true,
35+
noDecimals: true,
36+
noIpadInputBar: true
37+
});
38+
}
39+
}
2240
}
2341

2442
export function onMyTextLoaded(args: EventData) {

demo/app/main-page.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:NumKey="nativescript-numeric-keyboard" loaded="pageLoaded">
22
<StackLayout horizontalAlignment="center">
3-
<Label class="title" text="Tap the numbers to see various versions of this awesome iOS numeric keyboard" textWrap="true" textAlignment="center"/>
3+
<Label class="title" text="Tap the numbers on the right" textWrap="true" textAlignment="center"/>
44
<ScrollView verticalAlignment="top" style="background-color: #eee">
5-
<GridLayout rows="auto, auto, auto, auto, auto, auto, auto, auto" columns="*, auto">
5+
<GridLayout rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto" columns="*, auto">
66

77
<Label row="0" col="0" text="Regular 'number' keyboard (max 4)"/>
88
<TextField maxLength="4" class="unenhancedTextField" keyboardType="number" row="0" col="1" text="{{ myText }}" loaded="onMyTextLoaded"/>
@@ -27,7 +27,12 @@
2727

2828
<Label row="7" col="0" text="No return key"/>
2929
<NumKey:NumericKeyboardView id="noReturnKey" noIpadInputBar="true" noReturnKey="true" row="7" col="1" text="678"/>
30-
</GridLayout>e
30+
31+
<iOS>
32+
<Label row="8" colSpan="2" text="BONUS ROUND: a SearchBar"/>
33+
<SearchBar row="9" colSpan="2" hint="Only numbers allowed :)" id="searchBar"/>
34+
</iOS>
35+
</GridLayout>
3136
</ScrollView>
3237
<Button horizontalAlignment="center" text="submit" tap="{{ onSubmit }}"/>
3338
</StackLayout>

src/numeric-keyboard.ios.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,6 @@ class MMNumberKeyboardDelegateImpl extends NSObject implements MMNumberKeyboardD
193193
}
194194

195195
public numberKeyboardShouldInsertText(keyboard, text): boolean {
196-
return this.numberKeyboardShouldInsertTextForTextField(keyboard, text);
197-
}
198-
199-
private numberKeyboardShouldInsertTextForTextView(keyboard, text): boolean {
200-
const oldText: string = "" + this._owner.get().getText();
201-
202-
const decimalSeparator: string = this._owner.get().getDecimalSeparator();
203-
if (text === decimalSeparator) {
204-
return oldText.indexOf(decimalSeparator) === -1;
205-
}
206-
207-
const maxLength: number = this._owner.get().getMaxLength();
208-
return !(maxLength && oldText.length + text.length > maxLength);
209-
}
210-
211-
private numberKeyboardShouldInsertTextForTextField(keyboard, text): boolean {
212196
const owner = <any>this._owner.get();
213197
const nativeView = owner.getNativeTextField();
214198
const oldText = "" + this._owner.get().getText();
@@ -229,7 +213,9 @@ class MMNumberKeyboardDelegateImpl extends NSObject implements MMNumberKeyboardD
229213
length: nativeView.text.length === 0 ? 0 : nativeView.text.length
230214
};
231215

232-
nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString(nativeView, range, nativeView.text + text);
216+
if (nativeView.delegate && nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString) {
217+
nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString(nativeView, range, nativeView.text + text);
218+
}
233219
return true;
234220
}
235221

@@ -244,7 +230,9 @@ class MMNumberKeyboardDelegateImpl extends NSObject implements MMNumberKeyboardD
244230
let current = nativeView.text;
245231
current = current.substring(0, current.length - 1);
246232

247-
nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString(nativeView, range, current);
233+
if (nativeView.delegate && nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString) {
234+
nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString(nativeView, range, current);
235+
}
248236
return true;
249237
}
250238

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-numeric-keyboard",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "Handy and elegant numeric keyboard for iOS NativeScript apps. On Android we fall back to the regular numeric keyboard.",
55
"main": "numeric-keyboard",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)