Last week I posted Extending Oracle JET Components through Custom Bindings (Input Text with Buttons). About three quarters of the way through that post, I included a jsFiddle that shows how to add a delete button inside ojInputText. One of my readers asked if it was possible to hide the delete button if the ojInputText is empty. The answer, of course, is YES. The first thing we need to do is modify our ojoptionchange
event handler to listen for the rawValue
option instead of the value
option. rawValue
changes with each key press whereas value
changes when the component loses focus.
.on({ 'ojoptionchange': function(event, data) { // use option === "value" for final value // use option === "rawValue" for each character if (data.option === "rawValue") { var observable = valueAccessor(); // // ** show/hide icon code goes here // observable($(element).ojInputText("option", "value")); } } })
If all we want to do is show/hide the "button" within the ojInputText
, then we can toggle the oj-helper-hidden
utility class on the i
button element. That will result in the button instantly appearing or disappearing based on the length of the ojInputText
element's value. I'm not a fan of jarring UX changes. If something is going to appear on the screen, I want it to fade in. The gradual appearance helps prepare my mind for the existence of a new user interface element while the movement draws my attention to this new element. Unfortunately, we can't animate the oj-helper-hidden
class. The oj-helper-hidden
class changes the display
attribute and CSS does not support animating changes between the states of that attribute. We can, however, change the appearance of an element by animating the element's opacity. The following jsFiddle uses the opacity approach to toggle the appearance of the delete/clear button within ojInputText
.
Extra credit: Compare these jsFiddles: http://jsfiddle.net/jmarion/tbr4k30n/ and http://jsfiddle.net/jmarion/5e10mx8z/. On line 62 of the first fiddle (my first post on this subject), I use jQuery to identify the value option of the ojInputText. In the second fiddle (this fiddle), lines 87 and 91 do the exact same thing, but without the jQuery method invocation. I got smarter the second time around. the ojOptionChange
event actually includes the current value in the data object. I don't need to invoke a jQuery plugin method to retrieve the current value.
Note: I included CSS to change the cursor when the button becomes transparent, but the JavaScript click event will still trigger. Since the button is transparent when the ojInputText
has no value, this should not be an issue. The net result of deleting nothing is... yep, you guessed it: nothing.
No comments:
Post a Comment