Skip to content

Commit 84be2a4

Browse files
Text always left align when input done #16
1 parent 24080c6 commit 84be2a4

File tree

9 files changed

+138
-91
lines changed

9 files changed

+138
-91
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ For iOS. Falls back to the best platform-provided numeric keyboard on Android. N
1717

1818
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/_readme-header.png" style="max-width: 100%"/>
1919

20+
> BREAKING CHANGE in plugin version 4.0.0: we used to extend a `TextView`, now it's a `TextField`, because I finally hacked my way around a problem that prevented a TextField from emitting a `textChange` event. Note that unless you use the `decorate()` function this will not affect you (bar some now-fixed UI glitches).
21+
2022
## Installation
2123
From the command prompt go to your app's root folder and execute:
2224

@@ -28,25 +30,26 @@ tns plugin add nativescript-numeric-keyboard
2830
Check out [the demo](demo) to play with the keyboard. You can run it by typing `npm run demo.iphone` / `npm run demo.ipad` at the root of the project.
2931

3032
## How it works
31-
This plugin wraps a [native keyboard library](https://cocoapods.org/?q=MMNumberKeyboard) and extends a regular [NativeScript TextView](https://docs.nativescript.org/cookbook/ui/text-view). You can set any property you'd normally set on this widget (`class`, `text`, etc) and a few plugin-specific properties as well.
33+
This plugin wraps a [native keyboard library](https://cocoapods.org/?q=MMNumberKeyboard) and extends a regular [NativeScript TextField](https://docs.nativescript.org/cookbook/ui/text-field).
34+
You can set any property you'd normally set on this widget (`class`, `text`, etc) and a few plugin-specific properties as well.
3235

3336
You can either define the keyboard in XML or in code - use whichever tickles your fancy.
3437

3538
## Screenshot-driven documentation
36-
After adding the plugin you can add a namespace to your view (using `NumKey` below) and use the `NumericKeyboardView` tag to render a TextView powered by this plugin.
39+
After adding the plugin you can add a namespace to your view (using `NumKey` below) and use the `NumericKeyboardView` tag to render a TextField powered by this plugin.
3740

3841
```xml
3942
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:NK="nativescript-numeric-keyboard">
4043
<NK:NumericKeyboardView text="123.45" maxLength="10" />
4144
</Page>
4245
```
4346

44-
For comparison sake we kick off with the default appearance of a `TextView` and then showcase the various properties this plugin exposes:
47+
For comparison sake we kick off with the default appearance of a `TextField` and then showcase the various properties this plugin exposes:
4548

4649
| Appearance | Declaration |
4750
| --- | --- |
48-
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/regular-number.png" width="187px" height="333px"/> | `<TextView keyboardType="number" text="1.23"/>` |
49-
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/regular-phone.png" width="187px" height="333px"/> | `<TextView keyboardType="phone" text="12.34"/>` |
51+
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/regular-number.png" width="187px" height="333px"/> | `<TextField keyboardType="number" text="1.23"/>` |
52+
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/regular-phone.png" width="187px" height="333px"/> | `<TextField keyboardType="phone" text="12.34"/>` |
5053
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/default-plugin-appearance.png" width="187px" height="333px"/> | `<NK:NumericKeyboardView text="123.45"/>` |
5154
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/custom-button-title.png" width="187px" height="333px"/> | `<NK:NumericKeyboardView returnKeyTitle="OK" text="234.56"/>` |
5255
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/no-decimals.png" width="187px" height="333px"/> | `<NK:NumericKeyboardView noDecimals="true" text="345"/>` |
@@ -60,7 +63,7 @@ It's similar (although a bit cleaner than in these screenshots), except for some
6063

6164
| Appearance | Declaration |
6265
| --- | --- |
63-
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/ipad-regular-phone.png" width="344px"/> | `<TextView keyboardType="phone" text="12.34"/>` |
66+
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/ipad-regular-phone.png" width="344px"/> | `<TextField keyboardType="phone" text="12.34"/>` |
6467
| <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-numeric-keyboard/master/screenshots/ipad-default-appearance.png" width="344px"/> | `<NK:NumericKeyboard text="123.45"/>` |
6568

6669
## Usage with Angular
@@ -89,27 +92,27 @@ For the views you can take a look at the examples above and just replace `NumKey
8992
```
9093

9194
## Programmatic usage
92-
Say you have a plain old `TextView` in your view:
95+
Say you have a plain old `TextField` in your view:
9396

9497
```html
9598
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded">
96-
<TextView id="myTextView" maxlength="8" keyboardType="number" text="{{ myTextPlugin }}" />
99+
<TextField id="myTextField" maxlength="8" keyboardType="number" text="{{ myTextPlugin }}" />
97100
</Page>
98101
```
99102

100-
Now you can enhance the `TextView` with this plugin by doing this in the `pageLoaded` event you've defined in the `<Page>` tag above:
103+
Now you can enhance the `TextField` with this plugin by doing fi. this in the `pageLoaded` event you've defined in the `<Page>` tag above:
101104

102105
```js
103106
import { NumericKeyboard } from "nativescript-numeric-keyboard";
104107

105108
export function pageLoaded(args: observable.EventData) {
106-
let page = <pages.Page>args.object;
107-
let textView = <TextView>page.getViewById("myTextView");
108-
// or even textView.ios
109+
const page = <pages.Page>args.object;
110+
const textField = <TextField>page.getViewById("myTextField");
111+
// or even textField.ios
109112

110113
// this is an example with all possible properties, not that they make sense combined :)
111114
new NumericKeyboard().decorate({
112-
textView: textView,
115+
textField: textField,
113116
returnKeyTitle: "Go!",
114117
locale: "en_US", // or "nl_NL", or any valid locale really (to define the decimal char)
115118
noReturnKey: true,
@@ -119,6 +122,4 @@ export function pageLoaded(args: observable.EventData) {
119122
}
120123
```
121124

122-
Note that you really need to use a `TextView`, not a `TextField` for this to work as rendering the keyboard works fine, but getting its value just seems to get messed up when we set a different keyboard view. Shouldn't be a problem though, just something to keep in mind.
123-
124-
Also note that on Android you'll just get a numeric keyboard as usual (since we specified `keyboardType="number"`).
125+
Note that on Android you'll just get a numeric keyboard as usual (since we specified `keyboardType="number"`).

demo/app/app.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Label {
1212
font-weight: bold;
1313
}
1414

15-
TextView, NumericKeyboardView {
15+
TextField, NumericKeyboardView {
1616
font-size: 12;
1717
padding: 10;
1818
width: 120;
1919
text-align: right;
2020
}
2121

22-
/* Enhanced TextViews are transparent by default */
23-
.notEnhancedTextView {
22+
/* Enhanced TextFields are transparent by default */
23+
.unenhancedTextField {
2424
background-color: transparent;
25-
}
25+
}

demo/app/main-page.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,42 @@ import * as observable from "data/observable";
22
import * as pages from "ui/page";
33
import { HelloWorldModel } from "./main-view-model";
44
import { NumericKeyboard } from "nativescript-numeric-keyboard";
5-
import { TextView } from "ui/text-view";
5+
import { TextField } from "tns-core-modules/ui/text-field";
6+
import { EventData } from "tns-core-modules/data/observable";
67

78
// Event handler for Page 'loaded' event attached in main-page.xml
89
export function pageLoaded(args: observable.EventData) {
910
let page = <pages.Page>args.object;
1011
page.bindingContext = new HelloWorldModel();
1112

12-
let textView = <TextView>page.getViewById("defaultPluginKeyboard");
13+
const textField = <TextField>page.getViewById("defaultPluginKeyboard");
1314

1415
new NumericKeyboard().decorate({
15-
textView: textView,
16+
textField: textField,
1617
returnKeyTitle: "Go!",
1718
locale: "en_US",
1819
noDecimals: true,
1920
noIpadInputBar: true
2021
});
21-
}
22+
}
23+
24+
export function onMyTextLoaded(args: EventData) {
25+
args.object.on("textChange", (args: EventData) => {
26+
let numkey = <TextField>args.object;
27+
console.log("onMyTextLoaded, text entered: " + numkey.text);
28+
});
29+
}
30+
31+
export function onMyTextLoadedPluginCode(args: EventData) {
32+
args.object.on("textChange", (args: EventData) => {
33+
let numkey = <TextField>args.object;
34+
console.log("onMyTextLoadedPluginCode, text entered: " + numkey.text);
35+
});
36+
}
37+
38+
export function onMyTextLoadedPluginView(args: EventData) {
39+
args.object.on("textChange", (args: EventData) => {
40+
let numkey = <TextField>args.object;
41+
console.log("onMyTextLoadedPluginCode, text entered: " + numkey.text);
42+
});
43+
}

demo/app/main-page.xml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
<ScrollView verticalAlignment="top" style="background-color: #eee">
55
<GridLayout rows="auto, auto, auto, auto, auto, auto, auto, auto" columns="*, auto">
66

7-
<Label row="0" col="0" text="Regular 'number' keyboard"/>
8-
<TextView maxLength="4" class="notEnhancedTextView" keyboardType="number" row="0" col="1" text="{{ myText }}" propertyChange="{{ onPropertyChange }}"/>
7+
<Label row="0" col="0" text="Regular 'number' keyboard (max 4)"/>
8+
<TextField maxLength="4" class="unenhancedTextField" keyboardType="number" row="0" col="1" text="{{ myText }}" loaded="onMyTextLoaded"/>
99

10-
<Label row="1" col="0" text="Regular 'phone' keyboard"/>
11-
<TextView class="notEnhancedTextView" keyboardType="phone" row="1" col="1" text="12.34"/>
10+
<Label row="1" col="0" text="Regular 'phone' keyboard (max 4)"/>
11+
<TextField class="unenhancedTextField" keyboardType="phone" row="1" col="1" text="12.34"/>
1212

13-
<Label row="2" col="0" text="Plugin keyboard (by code)"/>
14-
<!-- not 'TextField', because we can't get the changed value -->
15-
<TextView id="defaultPluginKeyboard" maxLength="4" keyboardType="number" row="2" col="1" text="{{ myTextPlugin }}" propertyChange="{{ onPropertyChangePlugin }}"/>
13+
<Label row="2" col="0" text="Plugin keyboard (by code, max 4)"/>
14+
<TextField id="defaultPluginKeyboard" maxLength="4" keyboardType="number" row="2" col="1" text="{{ myTextPlugin }}" loaded="onMyTextLoadedPluginCode"/>
1615

17-
<Label row="3" col="0" text="Custom button title"/>
18-
<NumKey:NumericKeyboardView maxLength="4" row="3" col="1" returnKeyTitle="OK :)" text="{{ myTextViewPlugin }}" propertyChange="{{ onPropertyChangeViewPlugin }}"/>
16+
<Label row="3" col="0" text="Custom button title (max 4)"/>
17+
<NumKey:NumericKeyboardView maxLength="4" row="3" col="1" returnKeyTitle="OK :)" text="{{ myTextFieldPlugin }}" loaded="onMyTextLoadedPluginView"/>
1918

2019
<Label row="4" col="0" text="No decimals"/>
2120
<NumKey:NumericKeyboardView row="4" col="1" noDecimals="true" text="345"/>

demo/app/main-view-model.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
import { EventData, Observable } from 'data/observable';
2-
import { TextField } from "ui/text-field";
3-
import { TextView } from "ui/text-view";
1+
import { Observable } from "tns-core-modules/data/observable";
42

53
export class HelloWorldModel extends Observable {
64
constructor() {
75
super();
86
}
9-
public myText: number = 1234;
10-
public myTextPlugin: number = 2345;
11-
public myTextViewPlugin: number = 3456;
12-
13-
public onPropertyChange(args: EventData) {
14-
let numkey = <TextField>args.object;
15-
console.log("Entered text a: " + numkey.text);
16-
}
17-
18-
public onPropertyChangePlugin(args: EventData) {
19-
let numkey = <TextField>args.object;
20-
console.log("Entered text b: " + numkey);
21-
}
22-
23-
public onPropertyChangeViewPlugin(args: EventData) {
24-
let numkey = <TextView>args.object;
25-
console.log("Entered text c: " + numkey.text);
26-
}
7+
public myText: number = 123;
8+
public myTextPlugin: number = 234;
9+
public myTextFieldPlugin: number = 345;
2710

2811
public onSubmit() {
2912
console.log("Value of this.myText: " + this.myText);
3013
console.log("Value of this.myTextPlugin: " + this.myTextPlugin);
31-
console.log("Value of this.myTextViewPlugin: " + this.myTextViewPlugin);
14+
console.log("Value of this.myTextFieldPlugin: " + this.myTextFieldPlugin);
3215
}
33-
}
16+
}

src/numeric-keyboard.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export class NumericKeyboardView extends NumericKeyboardViewBase {
1919
// see https://github.com/NativeScript/NativeScript/issues/4123
2020
public _onReturnPress(): void {
2121
}
22-
}
22+
}

src/numeric-keyboard.common.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { TextView } from "tns-core-modules/ui/text-view";
2-
import { Property, booleanConverter } from "tns-core-modules/ui/core/view";
1+
import { TextField } from "tns-core-modules/ui/text-field";
2+
import { booleanConverter, Property } from "tns-core-modules/ui/core/view";
33

44
export interface NumericKeyboardOptions {
5-
textView: TextView;
5+
textField: TextField;
66
noDecimals?: boolean;
77
noReturnKey?: boolean;
88
returnKeyTitle?: string;
@@ -19,9 +19,10 @@ export interface TextAndDecimalSeparatorHolder {
1919
getDecimalSeparator(): string;
2020
getText(): string;
2121
getMaxLength(): number;
22+
getNativeTextField(): any;
2223
}
2324

24-
export abstract class NumericKeyboardViewBase extends TextView implements TextAndDecimalSeparatorHolder {
25+
export abstract class NumericKeyboardViewBase extends TextField implements TextAndDecimalSeparatorHolder {
2526
_decimalSep: string = "unset";
2627

2728
get ios(): any {
@@ -43,6 +44,10 @@ export abstract class NumericKeyboardViewBase extends TextView implements TextAn
4344
getMaxLength(): number {
4445
return this.maxLength;
4546
}
47+
48+
getNativeTextField(): number {
49+
return this.nativeView;
50+
}
4651
}
4752

4853
export const returnKeyTitleProperty = new Property<NumericKeyboardViewBase, string>({

0 commit comments

Comments
 (0)