Skip to content

Commit

Permalink
feat(inputs): add focus() method
Browse files Browse the repository at this point in the history
fixes #15266
fixes #15268
  • Loading branch information
manucorporat committed Aug 22, 2018
1 parent d1ed57e commit c66a34a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 9 deletions.
9 changes: 8 additions & 1 deletion core/src/components/input/input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Element, Event, EventEmitter, Prop, State, Watch } from '@stencil/core';
import { Component, Element, Event, EventEmitter, Method, Prop, State, Watch } from '@stencil/core';

import { Color, Mode, StyleEvent, TextFieldTypes, TextInputChangeEvent } from '../../interface';
import { debounceEvent, deferEvent, renderHiddenInput } from '../../utils/helpers';
Expand Down Expand Up @@ -246,6 +246,13 @@ export class Input implements InputComponent {
this.ionInputDidUnload.emit();
}

@Method()
focus() {
if (this.nativeInput) {
this.nativeInput.focus();
}
}

private emitStyle() {
this.ionStyle.emit({
'interactive': true,
Expand Down
7 changes: 7 additions & 0 deletions core/src/components/input/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ It is meant for text `type` inputs only, such as `"text"`, `"password"`, `"email
| `ionStyle` | Emitted when the styles change. |


## Methods

| Method | Description |
| ------- | ----------- |
| `focus` | |


----------------------------------------------

*Built with [StencilJS](https://stenciljs.com/)*
7 changes: 7 additions & 0 deletions core/src/components/searchbar/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ A Searchbar should be used instead of an input to search lists. A clear button i
| `ionInput` | Emitted when a keyboard input ocurred. |


## Methods

| Method | Description |
| ------- | ----------- |
| `focus` | |


----------------------------------------------

*Built with [StencilJS](https://stenciljs.com/)*
9 changes: 8 additions & 1 deletion core/src/components/searchbar/searchbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Element, Event, EventEmitter, Prop, State, Watch } from '@stencil/core';
import { Component, Element, Event, EventEmitter, Method, Prop, State, Watch } from '@stencil/core';

import { Color, Mode, TextInputChangeEvent } from '../../interface';
import { debounceEvent } from '../../utils/helpers';
Expand Down Expand Up @@ -152,6 +152,13 @@ export class Searchbar {
this.debounceChanged();
}

@Method()
focus() {
if (this.nativeInput) {
this.nativeInput.focus();
}
}

/**
* Clears the input field and triggers the control change.
*/
Expand Down
7 changes: 7 additions & 0 deletions core/src/components/textarea/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ The textarea component accepts the [native textarea attributes](https://develope
| `ionStyle` | Emitted when the styles change. |


## Methods

| Method | Description |
| ------- | ----------- |
| `focus` | |


## CSS Custom Properties

| Name | Description |
Expand Down
21 changes: 14 additions & 7 deletions core/src/components/textarea/textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Element, Event, EventEmitter, Prop, State, Watch } from '@stencil/core';
import { Component, Element, Event, EventEmitter, Method, Prop, State, Watch } from '@stencil/core';

import { Color, Mode, StyleEvent, TextInputChangeEvent } from '../../interface';
import { debounceEvent, deferEvent, renderHiddenInput } from '../../utils/helpers';
Expand All @@ -15,7 +15,7 @@ import { TextareaComponent } from '../input/input-base';
})
export class Textarea implements TextareaComponent {

private inputEl?: HTMLTextAreaElement;
private nativeInput?: HTMLTextAreaElement;
private inputId = `ion-input-${textareaIds++}`;

didBlurAfterEdit = false;
Expand Down Expand Up @@ -157,9 +157,9 @@ export class Textarea implements TextareaComponent {
*/
@Watch('value')
protected valueChanged() {
const { inputEl, value } = this;
if (inputEl!.value !== value) {
inputEl!.value = value;
const { nativeInput, value } = this;
if (nativeInput!.value !== value) {
nativeInput!.value = value;
}
this.ionChange.emit({ value });
}
Expand All @@ -170,6 +170,13 @@ export class Textarea implements TextareaComponent {
this.emitStyle();
}

@Method()
focus() {
if (this.nativeInput) {
this.nativeInput.focus();
}
}

private emitStyle() {
this.ionStyle.emit({
'interactive': true,
Expand All @@ -182,7 +189,7 @@ export class Textarea implements TextareaComponent {
}

private onInput(ev: KeyboardEvent) {
this.value = this.inputEl!.value;
this.value = this.nativeInput!.value;
this.emitStyle();
this.ionInput.emit(ev);
}
Expand Down Expand Up @@ -249,7 +256,7 @@ export class Textarea implements TextareaComponent {
return (
<textarea
class="native-textarea"
ref={el => this.inputEl = el as HTMLTextAreaElement}
ref={el => this.nativeInput = el as HTMLTextAreaElement}
autoCapitalize={this.autocapitalize}
autoFocus={this.autofocus}
disabled={this.disabled}
Expand Down
3 changes: 3 additions & 0 deletions core/src/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,7 @@ export namespace StencilComponents {
* If true, the user cannot interact with the input. Defaults to `false`.
*/
'disabled': boolean;
'focus': () => void;
/**
* A hint to the browser for which keyboard to display. This attribute applies when the value of the type attribute is `"text"`, `"password"`, `"email"`, or `"url"`. Possible values are: `"verbatim"`, `"latin"`, `"latin-name"`, `"latin-prose"`, `"full-width-latin"`, `"kana"`, `"katakana"`, `"numeric"`, `"tel"`, `"email"`, `"url"`.
*/
Expand Down Expand Up @@ -3681,6 +3682,7 @@ export namespace StencilComponents {
* Set the amount of time, in milliseconds, to wait to trigger the `ionChange` event after each keystroke. Default `250`.
*/
'debounce': number;
'focus': () => void;
/**
* The mode determines which platform styles to use. Possible values are: `"ios"` or `"md"`.
*/
Expand Down Expand Up @@ -4695,6 +4697,7 @@ export namespace StencilComponents {
* If true, the user cannot interact with the textarea. Defaults to `false`.
*/
'disabled': boolean;
'focus': () => void;
/**
* If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the maximum number of characters that the user can enter.
*/
Expand Down

0 comments on commit c66a34a

Please sign in to comment.