Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Commit

Permalink
Fix width bug with proxy (with long values, proxy would bleed out). A…
Browse files Browse the repository at this point in the history
…lso a change to maxlength, handles whitespace transparently
  • Loading branch information
zachleat committed Jun 5, 2015
1 parent b7611d5 commit 0f2850a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
16 changes: 16 additions & 0 deletions demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
Default <code>input[type=text]</code> with <code>maxlength="7"</code>. <em>You must account for whitespace in the maxlength.</em>
<input type="text" data-politespace maxlength="7" value="123456">
</label>
<label>
Default <code>input[type=number]</code> with <code>maxlength="7"</code>.
<input type="number" data-politespace maxlength="7" value="123456">
</label>
<label>
Default textarea
<textarea data-politespace></textarea>
Expand Down Expand Up @@ -128,6 +132,18 @@
<code>input[type=tel]</code> (US Phone Number) with <code>data-grouplength="4,3,3" data-reverse</code>
<input type="tel" data-politespace data-grouplength="4,3,3" data-reverse value="1234567890">
</label>
<label>
<code>input[type=number]</code> with a long value <code>data-grouplength="4"</code>
<div>
<input type="number" pattern="[0-9]*" data-politespace data-grouplength="4" value="123456789012345678901234567890">
</div>
</label>
<label>
<code>input[type=number]</code> with a long value <code>data-grouplength="4"</code>
<div>
<input type="number" pattern="[0-9]*" data-politespace data-grouplength="4" value="12345678901234567890" style="width: 40%">
</div>
</label>
</form>
</body>
</html>
1 change: 1 addition & 0 deletions src/politespace-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
.bind( "blur", function() {
$( this ).closest( ".politespace-proxy" ).addClass( "active" );
polite.update();
polite.updateProxy();
})
.bind( "focus", function() {
$( this ).closest( ".politespace-proxy" ).removeClass( "active" );
Expand Down
14 changes: 14 additions & 0 deletions src/politespace.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.politespace-proxy {
position: relative;
overflow: hidden; /* clearfix for floating siblings */
display: inline-block;
}
.politespace-proxy :first-child {
display: none;
Expand All @@ -11,7 +12,20 @@
left: 0;
top: 0;
pointer-events: none;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.politespace-proxy.active input {
color: transparent;
}
.politespace-proxy.active input[type=number] {
-moz-appearance: textfield;
}
.politespace-proxy.active input[type=number]::-webkit-inner-spin-button,
.politespace-proxy.active input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
35 changes: 20 additions & 15 deletions src/politespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@
return this._divideIntoArray( val ).join( this.delimiter );
};

Politespace.prototype.val = function() {
return this.format( this.element.value );
};

Politespace.prototype.update = function() {
var maxlength = this.element.getAttribute( "maxlength" ),
val = this.val();

Politespace.prototype.trimMaxlength = function( value ) {
var maxlength = this.element.getAttribute( "maxlength" );
// Note input type="number" maxlength does nothing
if( maxlength ) {
val = val.substr( 0, maxlength );
value = value.substr( 0, maxlength );
}
return value;
};

if( !this.useProxy() ) {
this.element.value = val;
}
Politespace.prototype.getValue = function() {
return this.trimMaxlength( this.element.value );
};

Politespace.prototype.update = function() {
this.element.value = this.useProxy() ? this.getValue() : this.format( this.getValue() );
};

Politespace.prototype.unformat = function( value ) {
Expand All @@ -91,8 +91,11 @@
};

Politespace.prototype.updateProxy = function() {
var proxy;
if( this.useProxy() ) {
this.element.parentNode.firstChild.innerHTML = this.val();
proxy = this.element.parentNode.firstChild;
proxy.innerHTML = this.format( this.getValue() );
proxy.style.width = this.element.offsetWidth + "px";
}
};

Expand All @@ -115,15 +118,17 @@
var parent = this.element.parentNode;
var el = document.createElement( "div" );
var proxy = document.createElement( "div" );
proxy.innerHTML = this.val();
proxy.style.font = getStyle( this.element, "font" );
proxy.style.left = sumStyles( this.element, [ "padding-left", "border-left-width" ] ) + "px";
proxy.style.paddingLeft = sumStyles( this.element, [ "padding-left", "border-left-width" ] ) + "px";
proxy.style.paddingRight = sumStyles( this.element, [ "padding-right", "border-right-width" ] ) + "px";
proxy.style.top = sumStyles( this.element, [ "padding-top", "border-top-width", "margin-top" ] ) + "px";

el.appendChild( proxy );
el.className = "politespace-proxy active";
var formEl = parent.replaceChild( el, this.element );
el.appendChild( formEl );

this.updateProxy();
};

w.Politespace = Politespace;
Expand Down

0 comments on commit 0f2850a

Please sign in to comment.