ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Add clear ("x") button for resetting input fields

Open asgeo1 opened this issue 12 years ago • 24 comments

It would be cool if Ionic had a "clear" / "x" button that could appear inside inputs. I suppose it could be implemented as an AngularJS directive.

Kind of like how jQuery Mobile has a data-clear-btn="true" attribute that can be applied to input elements:

http://demos.jquerymobile.com/1.4.0/textinput/#Text

Any plans for this?

asgeo1 avatar Jan 13 '14 06:01 asgeo1

That is cool, I'll add it to our 1.0 milestone // @bensperry

adamdbradley avatar Jan 13 '14 14:01 adamdbradley

http://forum.ionicframework.com/t/ionic-search-directive/3402

gastonbesada avatar Apr 30 '14 22:04 gastonbesada

@gastonbesada ionSearch is cool. But when you wrap it in < label class="item item-input" >...< /label >, the label will occupy half the space, and "X" will not show in the view.

zwz avatar May 03 '14 13:05 zwz

@zwz Yes, it is a custom development made by @jgx and fits its needs. @adamdbradley mention that they will include your suggested feature in ionic 1! You can wait it or take @jgx ionsearch directive.

gastonbesada avatar May 03 '14 14:05 gastonbesada

I've implemented this in the upcoming HN app, it could be converted in to a directive. I may suggest passing a method though rather than a simple yes/no since other things may need to be done when it's cleared, such as clearing search results.

perrygovier avatar May 20 '14 19:05 perrygovier

@perrygovier I totally agree with you. Besides search, any input type may have the clear button.

zwz avatar May 22 '14 04:05 zwz

Hey, you guys have the wrong JGx.

On Thu, May 22, 2014 at 12:50 AM, zwz [email protected] wrote:

@perrygovier https://github.com/perrygovier I totally agree with you. Besides search, any input type may have the clear button.

— Reply to this email directly or view it on GitHubhttps://github.com/driftyco/ionic/issues/405#issuecomment-43848331 .

  • Jonathan

JGx avatar May 22 '14 23:05 JGx

JGx, you can unsubscribe from the thread at the bottom.

and the input clear button is really simple to do with some css plus angular.

<input type="search" 
       placeholder="Filter contacts..." 
       ng-model="search">
<button ng-if="search.length"
        class="input-button button button-icon ion-android-close"
        ng-click="clearSearch()">
</button>
.button.button-icon.input-button {
  position: absolute;
  right: 0;
  top: 5px;
  color: #bbb;
}

We could definitely consider adding this sort of CSS to Ionic.

ajoslin avatar May 23 '14 00:05 ajoslin

I worked ajoslin's html/css into a small reusable directive. It just clears the model, doesn't accept a function, if anyone else wants to add that.

http://codepen.io/udfalkso/pen/cdfsp

udfalkso avatar May 27 '14 14:05 udfalkso

@udfalkso Nice! But when I combine your directive with the validation directive (see the example of the validation directive at http://plnkr.co/edit/4oTEYmKM5GobbXlL8mrL?p=preview), it shows the "x" button, but can not be clicked to reset the field. Meanwhile, the field is validated unexpectedly before clicking the submit button.

zwz avatar May 28 '14 16:05 zwz

Moving feature requests from GH Issues to Trello. Please continue the discussion and vote for this and other issues that are important to you here: https://trello.com/c/GLmSSOk6/28-add-clear-x-button-for-resetting-input-fields

perrygovier avatar Jul 28 '14 20:07 perrygovier

billraythree

http://wondrew.net/opcion_opcion

22.03.2015, 19:30, "billraythree" [email protected]:This is a directive that you can use to wrap an input that adds the click to clear icon, plus clear on ESC key. It's for bootstrap, but you can easily adapt for ionicons.

or

Cheers, Bill

—Reply to this email directly or .

Fayozjon avatar Mar 24 '15 18:03 Fayozjon

Look at this

http://plnkr.co/edit/Dje0l1?p=preview

EdgarArguelles avatar Jul 09 '15 16:07 EdgarArguelles

Here's a good discussion that relates to this: http://forum.ionicframework.com/t/cannot-tap-click-a-clear-search-icon-inside-an-input-label/4237/8 Use the same technique: Either rename the enclosing <label class="item-input-wrapper> to be a <div>, or use the "on-touch" directive instead of "ng-click".

jesseplymale avatar Jul 31 '15 18:07 jesseplymale

+1

Any updates on this?

ghost avatar Aug 19 '15 00:08 ghost

We wont be adding this to Ionic core, but it'd be a welcome addition to market.ionic.io

perrygovier avatar Aug 19 '15 00:08 perrygovier

Thanks, @perrygovier.

ghost avatar Aug 19 '15 00:08 ghost

@perrygovier. Why isn't the X button going to be added to ionic? I'm a noob to this cordava/ionic world and I wasted multiple days and continue to waste more time on trying to get my text field to have an x button that works when you start typing. Makes me want to switch back to native development.

salqadri avatar Sep 04 '15 16:09 salqadri

@salqadri Hi Salqadri, have a look at my directive, based on @udfalkso's work: https://github.com/VinceOPS/ionic-clearable-input

VinceOPS avatar Sep 27 '15 08:09 VinceOPS

@salqadri - This works for me. X button appears based on angular's ng-if directive. Input get's focus after tapping the X button thanks to ionic's on-touch directive. It also works if you use a div instead of a label tag.


<label class="item-input-wrapper">

   <i class="icon ion-ios-search-strong placeholder-icon"></i>

   <input type="search"
          placeholder="Search"
          ng-model="search.string">

    <a ng-if="search.string != ''" 
       on-touch="search.string=''">

       <i class="icon ion-ios-close placeholder-icon"></i>

    </a>

</label>

cosnofski avatar Oct 03 '15 18:10 cosnofski