nativescript-numeric-keyboard
Version:
Handy and elegant numeric keyboard for iOS NativeScript apps. On Android we fall back to the regular numeric keyboard.
214 lines • 9.12 kB
JavaScript
import { Color, TextField } from "@nativescript/core";
import { localeProperty, noDecimalsProperty, noIpadInputBarProperty, noReturnKeyProperty, NumericKeyboardViewBase, returnKeyButtonBackgroundColorProperty, returnKeyTitleProperty } from "./numeric-keyboard.common";
const _numkeyboard = [];
export class NumericKeyboard {
constructor() {
this._decimalSep = "unset";
}
getDecimalSeparator() {
return this._decimalSep;
}
getText() {
return this._nativeTextField.text;
}
getMaxLength() {
return this._maxLength;
}
getNativeTextField() {
return this._nativeTextField;
}
decorate(args) {
_numkeyboard.push(this);
return new Promise((resolve, reject) => {
if (!args || !args.textField) {
reject("Setting the 'textField' property is mandatory.");
return;
}
this._maxLength = args.textField.maxLength;
let nslocale;
if (args.locale) {
nslocale = NSLocale.localeWithLocaleIdentifier(args.locale);
}
else {
nslocale = NSLocale.currentLocale;
}
this._decimalSep = nslocale.decimalSeparator;
this._keyboard = MMNumberKeyboard.alloc().initWithFrameInputViewStyleLocale(CGRectZero, 1, nslocale);
if (args.returnKeyTitle) {
this._keyboard.returnKeyTitle = args.returnKeyTitle;
}
this._keyboard.allowsDecimalPoint = !args.noDecimals;
this._keyboardDelegate = MMNumberKeyboardDelegateImpl.initWithOwner(new WeakRef(this), args.textField);
this._keyboard.delegate = this._keyboardDelegate;
if (args.onReturnKeyPressed) {
this._keyboardDelegate.setCallback(args.onReturnKeyPressed);
}
if (args.noReturnKey) {
this._keyboardDelegate.setCallback(() => false);
this._keyboard.returnKeyTitle = " ";
this._keyboard.returnKeyButtonStyle = 1;
}
else if (!args.returnKeyTitle) {
this._keyboard.returnKeyTitle = " ";
}
if (args.returnKeyButtonBackgroundColor) {
this._keyboard.returnKeyButtonBackgroundColor = args.returnKeyButtonBackgroundColor.ios;
}
this._nativeTextField = args.textField.ios ? args.textField.ios : args.textField;
this._nativeTextField.inputView = this._keyboard;
if (args.textField.ios !== undefined && !args.textField.backgroundColor) {
args.textField.backgroundColor = new Color("transparent");
}
if (args.noIpadInputBar && this._nativeTextField.inputAssistantItem) {
this._nativeTextField.inputAssistantItem.leadingBarButtonGroups = [];
this._nativeTextField.inputAssistantItem.trailingBarButtonGroups = [];
}
if (args.textField.on) {
args.textField.on("unloaded", () => {
_numkeyboard.splice(_numkeyboard.indexOf(this), 1);
});
}
resolve();
});
}
}
export class NumericKeyboardView extends NumericKeyboardViewBase {
constructor() {
super(...arguments);
this._keyboardDelegate = null;
}
createNativeView() {
const v = super.createNativeView();
this.nativeView = v;
this.applyProperties();
return v;
}
applyProperties() {
let nslocale;
if (this.locale) {
nslocale = NSLocale.localeWithLocaleIdentifier(this.locale);
}
else {
nslocale = NSLocale.currentLocale;
}
this._decimalSep = nslocale.decimalSeparator;
this._keyboard = MMNumberKeyboard.alloc().initWithFrameInputViewStyleLocale(CGRectZero, 0, nslocale);
if (this.returnKeyTitle) {
this._keyboard.returnKeyTitle = this.returnKeyTitle;
}
this._keyboard.allowsDecimalPoint = !this.noDecimals;
this._keyboardDelegate = MMNumberKeyboardDelegateImpl.initWithOwner(new WeakRef(this));
this._keyboard.delegate = this._keyboardDelegate;
if (this.onReturnKeyPressed) {
this._keyboardDelegate.setCallback(this.onReturnKeyPressed);
}
if (this.noReturnKey) {
this._keyboardDelegate.setCallback(() => false);
this._keyboard.returnKeyTitle = " ";
this._keyboard.returnKeyButtonStyle = 1;
}
else if (!this.returnKeyTitle) {
this._keyboard.returnKeyTitle = " ";
}
if (this.returnKeyButtonBackgroundColor) {
this._keyboard.returnKeyButtonBackgroundColor = this.returnKeyButtonBackgroundColor.ios;
}
this.nativeView.inputView = this._keyboard;
if (this.noIpadInputBar && this.nativeView.inputAssistantItem) {
this.nativeView.inputAssistantItem.leadingBarButtonGroups = [];
this.nativeView.inputAssistantItem.trailingBarButtonGroups = [];
}
if (!this.backgroundColor) {
this.backgroundColor = new Color("transparent");
}
}
[returnKeyTitleProperty.setNative](value) {
this.returnKeyTitle = value;
}
[localeProperty.setNative](value) {
this.locale = value;
}
[noDecimalsProperty.setNative](value) {
this.noDecimals = value;
}
[noReturnKeyProperty.setNative](value) {
this.noReturnKey = value;
}
[noIpadInputBarProperty.setNative](value) {
this.noIpadInputBar = value;
}
[returnKeyButtonBackgroundColorProperty.setNative](value) {
this.returnKeyButtonBackgroundColor = value;
}
}
var MMNumberKeyboardDelegateImpl = /** @class */ (function (_super) {
__extends(MMNumberKeyboardDelegateImpl, _super);
function MMNumberKeyboardDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
MMNumberKeyboardDelegateImpl.initWithOwner = function (owner, textFieldToNotify) {
var delegate = MMNumberKeyboardDelegateImpl.new();
delegate._owner = owner;
delegate._textFieldToNotify = textFieldToNotify;
return delegate;
};
MMNumberKeyboardDelegateImpl.prototype.setCallback = function (callback) {
this._onReturnKeyPressedCallback = callback;
};
MMNumberKeyboardDelegateImpl.prototype.numberKeyboardShouldInsertText = function (keyboard, text) {
var owner = this._owner.get();
var nativeView = owner.getNativeTextField();
var oldText = "" + this._owner.get().getText();
var decimalSeparator = this._owner.get().getDecimalSeparator();
if (text === decimalSeparator) {
return oldText.indexOf(decimalSeparator) === -1;
}
var maxLength = this._owner.get().getMaxLength();
var shouldInsert = !(maxLength && this._owner.get().getText() && this._owner.get().getText().length + text.length > maxLength);
if (!shouldInsert) {
return false;
}
var range = {
location: 0,
length: nativeView.text.length === 0 ? 0 : nativeView.text.length
};
if (nativeView.delegate && nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString) {
nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString(nativeView, range, nativeView.text + text);
}
return true;
};
MMNumberKeyboardDelegateImpl.prototype.numberKeyboardShouldDeleteBackward = function (keyboard) {
var owner = this._owner.get();
var nativeView = owner.getNativeTextField();
var range = {
location: 0,
length: nativeView.text.length === 0 ? 0 : nativeView.text.length
};
var current = nativeView.text;
current = current.substring(0, current.length - 1);
if (nativeView.delegate && nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString) {
nativeView.delegate.textFieldShouldChangeCharactersInRangeReplacementString(nativeView, range, current);
}
return true;
};
MMNumberKeyboardDelegateImpl.prototype.numberKeyboardShouldReturn = function (keyboard) {
if (this._textFieldToNotify && this._textFieldToNotify.notify) {
this._textFieldToNotify.notify({ eventName: TextField.returnPressEvent, object: this._textFieldToNotify });
}
else {
var owner = this._owner.get();
if (owner && owner.notify) {
owner.notify({ eventName: TextField.returnPressEvent, object: owner });
}
}
if (this._onReturnKeyPressedCallback) {
return this._onReturnKeyPressedCallback();
}
else {
return true;
}
};
MMNumberKeyboardDelegateImpl.ObjCProtocols = [MMNumberKeyboardDelegate];
return MMNumberKeyboardDelegateImpl;
}(NSObject));
//# sourceMappingURL=numeric-keyboard.ios.js.map