Skip to content

Commit 2e65d3d

Browse files
committed
Merge pull request #119 from PolymerElements/ixes
add prefix/suffix content selectors
2 parents b3150c1 + 87359d7 commit 2e65d3d

File tree

4 files changed

+97
-5
lines changed

4 files changed

+97
-5
lines changed

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"iron-validator-behavior": "PolymerElements/iron-validator-behavior#^1.0.0",
4141
"test-fixture": "PolymerElements/test-fixture#^1.0.0",
4242
"web-component-tester": "*",
43-
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
43+
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0",
44+
"iron-icon": "PolymerElements/iron-icon#^1.0.0",
45+
"paper-icon-button": "PolymerElements/paper-icon-button#^1.0.0"
4446
}
4547
}

demo/index.html

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,30 @@
2525
<link rel="import" href="../paper-input-char-counter.html">
2626
<link rel="import" href="../paper-textarea.html">
2727
<link rel="import" href="../../iron-input/iron-input.html">
28+
<link rel="import" href="../../iron-icon/iron-icon.html">
29+
<link rel="import" href="../../iron-icons/iron-icons.html">
30+
<link rel="import" href="../../paper-icon-button/paper-icon-button.html">
2831
<link rel="import" href="ssn-input.html">
2932

30-
<link rel="stylesheet" href="../../paper-styles/paper-styles.html">
33+
<link rel="stylesheet" href="../../paper-styles/color.html">
3134
<link rel="import" href="../../paper-styles/demo-pages.html">
3235

33-
<style>
36+
<style is="custom-style">
3437
body {
3538
padding: 40px;
3639
}
40+
41+
paper-input.short {
42+
width: 200px;
43+
}
44+
45+
paper-icon-button {
46+
color: var(--paper-red-300);
47+
--paper-icon-button-ink-color: var(--paper-red-a100);
48+
--iron-icon-width: 15px;
49+
--iron-icon-height: 15px;
50+
padding: 0px 4px;
51+
}
3752
</style>
3853

3954
</head>
@@ -78,6 +93,24 @@ <h4>Character counter</h4>
7893
<paper-textarea label="text area with rows and max-rows" rows="3" max-rows="4"><paper-textarea>
7994
</div>
8095

96+
<h4>Prefixes and Suffixes</h4>
97+
<div class="vertical-section">
98+
<paper-input class="short" label="total" type="number">
99+
<div prefix>$</div>
100+
</paper-input>
101+
102+
<paper-input class="short" label="username">
103+
<div suffix>@email.com</div>
104+
</paper-input>
105+
106+
<paper-input class="short" label="icons and buttons" id="inputWithButton">
107+
<iron-icon icon="search" prefix></iron-icon>
108+
<paper-icon-button suffix onclick="clearInput()"
109+
icon="clear" alt="clear" title="clear" tabindex="0">
110+
</paper-icon-button>
111+
</paper-input>
112+
</div>
113+
81114
<h4>Complex inputs</h4>
82115
<div class="vertical-section">
83116
<template is="dom-bind">
@@ -93,6 +126,10 @@ <h4>Complex inputs</h4>
93126
function validate() {
94127
document.getElementById('inputForValidation').validate();
95128
}
129+
130+
function clearInput() {
131+
document.getElementById('inputWithButton').value = '';
132+
}
96133
</script>
97134
</body>
98135
</html>

paper-input-container.html

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
You can use a custom input element in a `<paper-input-container>`, for example to implement a
3535
compound input field like a social security number input. The custom input element should have the
3636
`paper-input-input` class, have a `notify:true` value property and optionally implements
37-
`Polymer.IronValidatableBehavior` if it is validatble.
37+
`Polymer.IronValidatableBehavior` if it is validatable.
3838
3939
<paper-input-container attr-for-value="ssn-value">
4040
<label>Social security number</label>
@@ -53,6 +53,17 @@
5353
or validity changes, and may implement functionality such as error messages or character counters.
5454
They appear at the bottom of the input.
5555
56+
### Prefixes and suffixes
57+
These are child elements of a `<paper-input-container>` with the `prefix`
58+
or `suffix` attribute, and are displayed inline with the input, before or after.
59+
60+
<paper-input-container>
61+
<div prefix>$</div>
62+
<label>Total</label>
63+
<input is="iron-input">
64+
<paper-icon-button suffix icon="clear"></paper-icon-button>
65+
</paper-input-container>
66+
5667
### Styling
5768
5869
The following custom properties and mixins are available for styling:
@@ -150,8 +161,14 @@
150161
@apply(--paper-input-container-underline-disabled);
151162
}
152163

164+
.label-and-input-container {
165+
@apply(--layout-flex);
166+
@apply(--layout-relative);
167+
}
168+
153169
.input-content {
154170
position: relative;
171+
@apply(--layout-horizontal);
155172
}
156173

157174
.input-content ::content label,
@@ -213,6 +230,12 @@
213230
@apply(--paper-input-container-input);
214231
}
215232

233+
::content [prefix],
234+
::content [suffix] {
235+
@apply(--paper-font-subhead);
236+
@apply(--paper-input-container-input);
237+
}
238+
216239
/* Firefox sets a min-width on the input, which can cause layout issues */
217240
.input-content ::content input {
218241
min-width: 0;
@@ -239,7 +262,11 @@
239262
</template>
240263

241264
<div class$="[[_computeInputContentClass(noLabelFloat,alwaysFloatLabel,focused,invalid,_inputHasContent)]]">
242-
<content select=":not([add-on])"></content>
265+
<content select="[prefix]" id="prefix"></content>
266+
<div class="label-and-input-container">
267+
<content select=":not([add-on]):not([prefix]):not([suffix])"></content>
268+
</div>
269+
<content select="[suffix]"></content>
243270
</div>
244271

245272
<div class$="[[_computeUnderlineClass(focused,invalid)]]">
@@ -476,13 +503,25 @@
476503
_computeInputContentClass: function(noLabelFloat, alwaysFloatLabel, focused, invalid, _inputHasContent) {
477504
var cls = 'input-content';
478505
if (!noLabelFloat) {
506+
var label = this.querySelector('label');
507+
479508
if (alwaysFloatLabel || _inputHasContent) {
480509
cls += ' label-is-floating';
481510
if (invalid) {
482511
cls += ' is-invalid';
483512
} else if (focused) {
484513
cls += " label-is-highlighted";
485514
}
515+
// The label might have a horizontal offset if a prefix element exists
516+
// which needs to be undone when displayed as a floating label.
517+
if (this.$.prefix && label && label.offsetParent) {
518+
label.style.left = -label.offsetParent.offsetLeft + 'px';
519+
}
520+
} else {
521+
// When the label is not floating, it should overlap the input element.
522+
if (label) {
523+
label.style.left = 0;
524+
}
486525
}
487526
} else {
488527
if (_inputHasContent) {

paper-input.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
<paper-input error-message="Invalid input!" label="Input label"></paper-input>
2626
<paper-input char-counter label="Input label"></paper-input>
2727
28+
It can also include custom prefix or suffix elements, which are displayed
29+
before or after the text input itself. In order for an element to be
30+
considered as a prefix, it must have the `prefix` attribute (and similarly
31+
for `suffix`).
32+
33+
<paper-input label="total">
34+
<div prefix>$</div>
35+
<paper-icon-button suffix icon="clear"></paper-icon-button>
36+
</paper-input>
37+
2838
See `Polymer.PaperInputBehavior` for more API docs.
2939
3040
### Styling
@@ -68,6 +78,8 @@
6878

6979
<paper-input-container no-label-float="[[noLabelFloat]]" always-float-label="[[_computeAlwaysFloatLabel(alwaysFloatLabel,placeholder)]]" auto-validate$="[[autoValidate]]" disabled$="[[disabled]]" invalid="[[invalid]]">
7080

81+
<content select="[prefix]"></content>
82+
7183
<label hidden$="[[!label]]">[[label]]</label>
7284

7385
<input is="iron-input" id="input"
@@ -95,6 +107,8 @@
95107
autocapitalize$="[[autocapitalize]]"
96108
autocorrect$="[[autocorrect]]">
97109

110+
<content select="[suffix]"></content>
111+
98112
<template is="dom-if" if="[[errorMessage]]">
99113
<paper-input-error>[[errorMessage]]</paper-input-error>
100114
</template>

0 commit comments

Comments
 (0)