Merge pull request #151 from NativeScript/auto-correct

New Feature: TextField and TextView now have autocapitalizationType prop...
This commit is contained in:
Rossen Hristov
2015-03-10 11:00:40 +02:00
9 changed files with 132 additions and 8 deletions

View File

@ -2,7 +2,7 @@
<StackLayout id="stack"> <StackLayout id="stack">
<Button id="button" text="textChanged" height="100" margin="20" tap="onTap" backgroundColor="Red"/> <Button id="button" text="textChanged" height="100" margin="20" tap="onTap" backgroundColor="Red"/>
<Label id="label" text="{{ text }}" height="100" margin="20"/> <Label id="label" text="{{ text }}" height="100" margin="20"/>
<TextField id="textField" text="{{ text }}" margin="20" updateTextTrigger="textChanged"/> <TextField id="textField" text="{{ text }}" margin="20" autocapitalizationType="none"/>
<TextView id="textView" text="{{ text }}" height="100" margin="20" updateTextTrigger="textChanged"/> <TextView id="textView" text="{{ text }}" height="100" margin="20" autocapitalizationType="none"/>
</StackLayout> </StackLayout>
</Page> </Page>

View File

@ -28,6 +28,12 @@ var updateTextTriggerProperty = new dependencyObservable.Property(
new proxy.PropertyMetadata(enums.UpdateTextTrigger.textChanged, dependencyObservable.PropertyMetadataSettings.None) new proxy.PropertyMetadata(enums.UpdateTextTrigger.textChanged, dependencyObservable.PropertyMetadataSettings.None)
); );
var autocapitalizationTypeProperty = new dependencyObservable.Property(
"autocapitalizationType",
"EditableTextBase",
new proxy.PropertyMetadata(enums.AutocapitalizationType.sentences, dependencyObservable.PropertyMetadataSettings.None)
);
function onKeyboardTypePropertyChanged(data: dependencyObservable.PropertyChangeData) { function onKeyboardTypePropertyChanged(data: dependencyObservable.PropertyChangeData) {
var editableTextBase = <EditableTextBase>data.object; var editableTextBase = <EditableTextBase>data.object;
editableTextBase._onKeyboardTypePropertyChanged(data); editableTextBase._onKeyboardTypePropertyChanged(data);
@ -49,6 +55,13 @@ function onEditablePropertyChanged(data: dependencyObservable.PropertyChangeData
(<proxy.PropertyMetadata>editableProperty.metadata).onSetNativeValue = onEditablePropertyChanged; (<proxy.PropertyMetadata>editableProperty.metadata).onSetNativeValue = onEditablePropertyChanged;
function onAutocapitalizationTypePropertyChanged(data: dependencyObservable.PropertyChangeData) {
var editableTextBase = <EditableTextBase>data.object;
editableTextBase._onAutocapitalizationTypePropertyChanged(data);
}
(<proxy.PropertyMetadata>autocapitalizationTypeProperty.metadata).onSetNativeValue = onAutocapitalizationTypePropertyChanged;
export class EditableTextBase extends textBase.TextBase implements definition.EditableTextBase { export class EditableTextBase extends textBase.TextBase implements definition.EditableTextBase {
public static keyboardTypeProperty = keyboardTypeProperty; public static keyboardTypeProperty = keyboardTypeProperty;
@ -59,6 +72,8 @@ export class EditableTextBase extends textBase.TextBase implements definition.Ed
public static updateTextTriggerProperty = updateTextTriggerProperty; public static updateTextTriggerProperty = updateTextTriggerProperty;
public static autocapitalizationTypeProperty = autocapitalizationTypeProperty;
constructor(options?: definition.Options) { constructor(options?: definition.Options) {
super(options); super(options);
} }
@ -95,6 +110,14 @@ export class EditableTextBase extends textBase.TextBase implements definition.Ed
this._setValue(EditableTextBase.updateTextTriggerProperty, value); this._setValue(EditableTextBase.updateTextTriggerProperty, value);
} }
get autocapitalizationType(): string {
return this._getValue(EditableTextBase.autocapitalizationTypeProperty);
}
set autocapitalizationType(value: string) {
this._setValue(EditableTextBase.autocapitalizationTypeProperty, value);
}
public dismissSoftInput() { public dismissSoftInput() {
// //
} }
@ -111,4 +134,8 @@ export class EditableTextBase extends textBase.TextBase implements definition.Ed
public _onEditablePropertyChanged(data: dependencyObservable.PropertyChangeData) { public _onEditablePropertyChanged(data: dependencyObservable.PropertyChangeData) {
// //
} }
public _onAutocapitalizationTypePropertyChanged(data: dependencyObservable.PropertyChangeData) {
//
}
} }

View File

@ -182,4 +182,34 @@ export class EditableTextBase extends common.EditableTextBase {
this.android.setKeyListener(null); this.android.setKeyListener(null);
} }
} }
public _onAutocapitalizationTypePropertyChanged(data: dependencyObservable.PropertyChangeData) {
var editableTextBase = <EditableTextBase>data.object;
if (!editableTextBase.android) {
return;
}
var inputType = editableTextBase.android.getInputType();
inputType = inputType & ~28762; //28762 (0x00007000) 13,14,15bits
switch (data.newValue) {
case enums.AutocapitalizationType.none:
//Do nothing, we have lowered the three bits above.
break;
case enums.AutocapitalizationType.words:
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS; //8192 (0x00002000) 14th bit
break;
case enums.AutocapitalizationType.sentences:
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; //16384(0x00004000) 15th bit
break;
case enums.AutocapitalizationType.allCharacters:
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS; //4096 (0x00001000) 13th bit
break;
default:
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
break;
}
editableTextBase.android.setInputType(inputType);
}
} }

View File

@ -10,6 +10,7 @@
public static returnKeyTypeProperty: dependencyObservable.Property; public static returnKeyTypeProperty: dependencyObservable.Property;
public static editableProperty: dependencyObservable.Property; public static editableProperty: dependencyObservable.Property;
public static updateTextTriggerProperty: dependencyObservable.Property; public static updateTextTriggerProperty: dependencyObservable.Property;
public static autocapitalizationTypeProperty: dependencyObservable.Property;
constructor(options?: Options); constructor(options?: Options);
@ -34,6 +35,11 @@
*/ */
updateTextTrigger: string; updateTextTrigger: string;
/**
* Gets or sets the autocapitalization type. Possible values are contained in the [AutocapitalizationType enumeration](../enums/AutocapitalizationType/README.md).
*/
autocapitalizationType: string;
/** /**
* Hides the soft input method, ususally a soft keyboard. * Hides the soft input method, ususally a soft keyboard.
*/ */
@ -64,5 +70,10 @@
* Possible values are contained in the UpdateTextTrigger enumeration located in "ui/enums" module. * Possible values are contained in the UpdateTextTrigger enumeration located in "ui/enums" module.
*/ */
updateTextTrigger?: string; updateTextTrigger?: string;
/**
* Gets or sets the autocapitalization type.
*/
autocapitalizationType?: string;
} }
} }

View File

@ -9,7 +9,7 @@ export class EditableTextBase extends common.EditableTextBase {
} }
public dismissSoftInput() { public dismissSoftInput() {
this.ios.resignFirstResponder(); (<UIResponder>this.ios).resignFirstResponder();
} }
public _onKeyboardTypePropertyChanged(data: dependencyObservable.PropertyChangeData) { public _onKeyboardTypePropertyChanged(data: dependencyObservable.PropertyChangeData) {
@ -35,7 +35,7 @@ export class EditableTextBase extends common.EditableTextBase {
break; break;
} }
this.ios.keyboardType = newKeyboardType; (<UITextInputTraits>this.ios).keyboardType = newKeyboardType;
} }
public _onReturnKeyTypePropertyChanged(data: dependencyObservable.PropertyChangeData) { public _onReturnKeyTypePropertyChanged(data: dependencyObservable.PropertyChangeData) {
@ -61,6 +61,29 @@ export class EditableTextBase extends common.EditableTextBase {
break; break;
} }
this.ios.returnKeyType = newReturnKeyType; (<UITextInputTraits>this.ios).returnKeyType = newReturnKeyType;
}
public _onAutocapitalizationTypePropertyChanged(data: dependencyObservable.PropertyChangeData) {
var newValue;
switch (data.newValue) {
case enums.AutocapitalizationType.none:
newValue = UITextAutocapitalizationType.UITextAutocapitalizationTypeNone;
break;
case enums.AutocapitalizationType.words:
newValue = UITextAutocapitalizationType.UITextAutocapitalizationTypeWords;
break;
case enums.AutocapitalizationType.sentences:
newValue = UITextAutocapitalizationType.UITextAutocapitalizationTypeSentences;
break;
case enums.AutocapitalizationType.allCharacters:
newValue = UITextAutocapitalizationType.UITextAutocapitalizationTypeAllCharacters;
break;
default:
newValue = UITextAutocapitalizationType.UITextAutocapitalizationTypeSentences;
break;
}
(<UITextInputTraits>this.ios).autocapitalizationType = newValue;
} }
} }

26
ui/enums/enums.d.ts vendored
View File

@ -285,4 +285,30 @@
*/ */
export var bottom: string; export var bottom: string;
} }
/**
* Represents the auto-capitalization style for a text input.
*/
module AutocapitalizationType {
/**
* Do not capitalize any text automatically.
*/
export var none: string;
/**
* Capitalize the first letter of each word automatically.
*/
export var words: string;
/**
* Capitalize the first letter of each sentence automatically.
*/
export var sentences: string;
/**
* Capitalize all characters automatically.
*/
export var allCharacters: string;
}
} }

View File

@ -78,3 +78,10 @@ export module Dock {
export var right: string = "right"; export var right: string = "right";
export var bottom: string = "bottom"; export var bottom: string = "bottom";
} }
export module AutocapitalizationType {
export var none: string = "none";
export var words: string = "words";
export var sentences: string = "sentences";
export var allCharacters: string = "allCharacters";
}

View File

@ -60,6 +60,6 @@ export class TextField extends common.TextField {
this.android.setLines(1); this.android.setLines(1);
this.android.setMaxLines(1); this.android.setMaxLines(1);
this.android.setHorizontallyScrolling(true); this.android.setHorizontallyScrolling(true);
this.android.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_NORMAL); this.android.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_NORMAL | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
} }
} }

View File

@ -9,6 +9,6 @@ export class TextView extends common.TextView {
super._createUI(); super._createUI();
this.android.setGravity(android.view.Gravity.TOP | android.view.Gravity.LEFT); this.android.setGravity(android.view.Gravity.TOP | android.view.Gravity.LEFT);
this.android.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE); this.android.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
} }
} }