Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(searchbar): properly align placeholder #20460

Merged
merged 1 commit into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 18 additions & 17 deletions core/src/components/searchbar/searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Component, ComponentInterface, Element, Event, EventEmitter, Host, Meth
import { config } from '../../global/config';
import { getIonMode } from '../../global/ionic-global';
import { Color, SearchbarChangeEventDetail, StyleEventDetail } from '../../interface';
import { debounceEvent } from '../../utils/helpers';
import { sanitizeDOMString } from '../../utils/sanitization';
import { debounceEvent, raf } from '../../utils/helpers';
import { createColorClasses } from '../../utils/theme';

/**
Expand Down Expand Up @@ -335,27 +334,29 @@ export class Searchbar implements ComponentInterface {
// Create a dummy span to get the placeholder width
const doc = document;
const tempSpan = doc.createElement('span');
tempSpan.innerHTML = sanitizeDOMString(this.placeholder) || '';
tempSpan.innerText = this.placeholder || '';
doc.body.appendChild(tempSpan);

// Get the width of the span then remove it
const textWidth = tempSpan.offsetWidth;
tempSpan.remove();
raf(() => {
const textWidth = tempSpan.offsetWidth;
tempSpan.remove();

// Calculate the input padding
const inputLeft = 'calc(50% - ' + (textWidth / 2) + 'px)';
// Calculate the input padding
const inputLeft = 'calc(50% - ' + (textWidth / 2) + 'px)';

// Calculate the icon margin
const iconLeft = 'calc(50% - ' + ((textWidth / 2) + 30) + 'px)';
// Calculate the icon margin
const iconLeft = 'calc(50% - ' + ((textWidth / 2) + 30) + 'px)';

// Set the input padding start and icon margin start
if (isRTL) {
inputEl.style.paddingRight = inputLeft;
iconEl.style.marginRight = iconLeft;
} else {
inputEl.style.paddingLeft = inputLeft;
iconEl.style.marginLeft = iconLeft;
}
// Set the input padding start and icon margin start
if (isRTL) {
inputEl.style.paddingRight = inputLeft;
iconEl.style.marginRight = iconLeft;
} else {
inputEl.style.paddingLeft = inputLeft;
iconEl.style.marginLeft = iconLeft;
}
});
}
}

Expand Down
4 changes: 4 additions & 0 deletions core/src/components/searchbar/test/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ <h5 class="ion-padding-start ion-padding-top"> Search - Animated and No Cancel</
<ion-searchbar id="dynamicAttr" placeholder="Search" animated="true" show-cancel-button="never">
</ion-searchbar>

<h5 class="ion-padding-start ion-padding-top"> Search - HTML Placeholder Treated As Text </h5>
<ion-searchbar mode="ios" animated="true" show-cancel-button="focus" placeholder="<ion-button>Hello</ion-button>">
</ion-searchbar>

<p class="ion-padding">
<ion-button expand="block" (click)="changeValue()">Change Value</ion-button>
</p>
Expand Down