fix(label): only show placeholder with floating label when focused (#22958)

fixes #17571
This commit is contained in:
Brandy Carney
2021-03-01 15:36:29 -05:00
committed by GitHub
parent b83e00934e
commit 9282aa6871
6 changed files with 199 additions and 32 deletions

View File

@ -169,3 +169,18 @@
:host(.has-focus) button {
pointer-events: auto;
}
// Item Floating: Placeholder
// ----------------------------------------------------------------
// When used with a floating item the placeholder should hide
:host-context(.item-label-floating.item-has-placeholder:not(.item-has-value)) {
transition: opacity 0.15s cubic-bezier(0.4, 0, 0.2, 1);
opacity: 0;
}
:host-context(.item-label-floating.item-has-placeholder:not(.item-has-value).item-has-focus) {
opacity: 1;
}

View File

@ -190,15 +190,6 @@ export class Input implements ComponentInterface {
*/
@Prop({ mutable: true }) value?: string | number | null = '';
/**
* Update the native input element when the value changes
*/
@Watch('value')
protected valueChanged() {
this.emitStyle();
this.ionChange.emit({ value: this.value == null ? this.value : this.value.toString() });
}
/**
* Emitted when a keyboard input occurred.
*/
@ -225,6 +216,23 @@ export class Input implements ComponentInterface {
*/
@Event() ionStyle!: EventEmitter<StyleEventDetail>;
/**
* Update the item classes when the placeholder changes
*/
@Watch('placeholder')
protected placeholderChanged() {
this.emitStyle();
}
/**
* Update the native input element when the value changes
*/
@Watch('value')
protected valueChanged() {
this.emitStyle();
this.ionChange.emit({ value: this.value == null ? this.value : this.value.toString() });
}
componentWillLoad() {
this.inheritedAttributes = inheritAttributes(this.el, ['tabindex', 'title']);
}

View File

@ -132,11 +132,11 @@
<ion-label>Right</ion-label>
<ion-input class="ion-text-right" value="Narrow input"></ion-input>
</ion-item>
</ion-content>
<script>
document.querySelector('ion-input').addEventListener('ionBlur', (ev) => { console.log(ev)})
document.querySelector('ion-input').addEventListener('ionBlur', (ev) => { console.log(ev)});
function toggleBoolean(id, prop) {
var el = document.getElementById(id);

View File

@ -13,27 +13,130 @@
<body>
<ion-app>
<ion-header>
<ion-toolbar>
<ion-title>Input - Spec</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<h1>Floating Inputs</h1>
<div class="grid">
<div class="column">
<h2>Inactive</h2>
<ion-item>
<ion-label position="floating">Label</ion-label>
<ion-input placeholder="Placeholder Text"></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Focused</h2>
<ion-item class="item-has-focus">
<ion-label position="floating">Label</ion-label>
<ion-input placeholder="Placeholder Text"></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Activated</h2>
<ion-item>
<ion-label position="floating">Label</ion-label>
<ion-input placeholder="Placeholder Text" value="Input Text"></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Hover</h2>
<ion-item class="item-hovered">
<ion-label position="floating">Label</ion-label>
<ion-input></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Disabled</h2>
<ion-item>
<ion-label position="floating">Label</ion-label>
<ion-input disabled></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Toggle Placeholder</h2>
<ion-item>
<ion-label position="floating">Label</ion-label>
<ion-input id="floatingToggle" type="password"></ion-input>
<ion-button fill="clear" slot="end" onClick="togglePlaceholder('#floatingToggle')" class="ion-align-self-center">
Toggle
</ion-button>
</ion-item>
</div>
</div>
<h1>Stacked Inputs</h1>
<div class="grid">
<div class="column">
<h2>Inactive</h2>
<ion-item>
<ion-label position="stacked">Label</ion-label>
<ion-input></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Focused</h2>
<ion-item class="item-has-focus">
<ion-label position="stacked">Label</ion-label>
<ion-input placeholder="Placeholder Text"></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Activated</h2>
<ion-item>
<ion-label position="stacked">Label</ion-label>
<ion-input placeholder="Placeholder Text" value="Input Text"></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Hover</h2>
<ion-item class="item-hovered">
<ion-label position="stacked">Label</ion-label>
<ion-input></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Disabled</h2>
<ion-item>
<ion-label position="stacked">Label</ion-label>
<ion-input disabled></ion-input>
</ion-item>
</div>
<div class="column">
<h2>Toggle Placeholder</h2>
<ion-item>
<ion-label position="stacked">Label</ion-label>
<ion-input id="stackedToggle" type="password"></ion-input>
<ion-button fill="clear" slot="end" onClick="togglePlaceholder('#stackedToggle')" class="ion-align-self-center">
Toggle
</ion-button>
</ion-item>
</div>
</div>
<hr>
<h2>Stacked Div</h2>
<ion-item>
<ion-label position="floating">Floating: input</ion-label>
<ion-input></ion-input>
</ion-item>
<ion-item class="item-has-focus">
<ion-label position="floating">Floating: input focused value</ion-label>
<ion-input value="value"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Stacked: input</ion-label>
<ion-input></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Stacked: input value</ion-label>
<ion-input value="value"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Stacked: div</ion-label>
<ion-label position="stacked">Label</ion-label>
<div>A div</div>
</ion-item>
<ion-item class="ion-align-items-center">
<ion-icon slot="start" name="planet"></ion-icon>
<ion-label position="stacked">Align items: center</ion-label>
@ -68,8 +171,45 @@
</ion-content>
<style>
h1 {
font-size: 14px;
color: #54575e;
margin: 25px 0 5px 25px;
}
h2 {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 12px;
font-weight: normal;
color: #a1a7b0;
margin-top: 10px;
margin-left: 5px;
}
hr {
background: #eff1f3;
margin-top: 18px;
margin-bottom: 25px;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
row-gap: 20px;
column-gap: 20px;
padding: 0 20px 20px;
}
ion-item {
--background: #f5f5f5;
--background: #e0e0e0;
--background-hover: #d3d3d3;
}
.custom {
@ -84,6 +224,13 @@
color: purple !important;
}
</style>
<script>
function togglePlaceholder(id) {
const input = document.querySelector(id);
input.placeholder = input.placeholder ? undefined : 'Placeholder Text';
}
</script>
</ion-app>
</body>

View File

@ -35,12 +35,10 @@
}
:host-context(.item-has-focus).label-floating,
:host-context(.item-has-placeholder).label-floating,
:host-context(.item-has-value).label-floating {
@include transform(translate3d(0, 0, 0), scale(.82));
}
// iOS Typography
// --------------------------------------------------

View File

@ -39,7 +39,6 @@
}
:host-context(.item-has-focus).label-floating,
:host-context(.item-has-placeholder).label-floating,
:host-context(.item-has-value).label-floating {
@include transform(translateY(50%), scale(.75));
}