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 possibly mis-measured text width in DatePicker #320

Merged
merged 2 commits into from
Dec 7, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/datetime/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
* Measure width of a string displayed with styles provided by `className`.
* Should only be used if measuring can't be done with existing DOM elements.
*/
export function measureTextWidth(text: string, className = "") {
export function measureTextWidth(text: string, className = "", containerElement = document.body) {
const span = document.createElement("span");
span.classList.add(className);
span.innerHTML = text;

document.body.appendChild(span);
containerElement.appendChild(span);
const spanWidth = span.offsetWidth;
span.remove();

Expand Down
25 changes: 16 additions & 9 deletions packages/datetime/src/datePickerCaption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export class DatePickerCaption extends React.Component<IDatePickerCaptionProps,
private displayedMonthText: string;
private displayedYearText: string;

private monthArrow: HTMLElement;
private yearArrow: HTMLElement;
private containerElement: HTMLElement;
private monthArrowElement: HTMLElement;
private yearArrowElement: HTMLElement;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 great renames


public render() {
const { date, locale, localeUtils, minDate, maxDate } = this.props;
Expand Down Expand Up @@ -62,7 +63,10 @@ export class DatePickerCaption extends React.Component<IDatePickerCaptionProps,

const caretClasses = classNames("pt-icon-standard", "pt-icon-caret-down", Classes.DATEPICKER_CAPTION_CARET);
return (
<div className={Classes.DATEPICKER_CAPTION}>
<div
className={Classes.DATEPICKER_CAPTION}
ref={this.containerRefHandler}
>
<div className={Classes.DATEPICKER_CAPTION_SELECT}>
<select
className={Classes.DATEPICKER_MONTH_SELECT}
Expand Down Expand Up @@ -101,16 +105,19 @@ export class DatePickerCaption extends React.Component<IDatePickerCaptionProps,
this.positionArrows();
}

private monthArrowRefHandler = (r: HTMLElement) => this.monthArrow = r;
private yearArrowRefHandler = (r: HTMLElement) => this.yearArrow = r;
private containerRefHandler = (r: HTMLElement) => this.containerElement = r;
private monthArrowRefHandler = (r: HTMLElement) => this.monthArrowElement = r;
private yearArrowRefHandler = (r: HTMLElement) => this.yearArrowElement = r;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated: I would like to move to the following pattern for ref handlers:

private refHandlers = {
    container: (r: HTMLElement) => this.containerElement = r,
    monthArrow: ...,
    yearArrow: ...,
};

Copy link
Contributor

@giladgray giladgray Dec 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm accumulating a list of major refactors to datetime code, for the new year...
will add this!


private positionArrows() {
// pass our container element to the measureTextWidth utility to ensure
// that we're measuring the width of text as sized within this component.
const textClass = "pt-datepicker-caption-measure";
const monthWidth = Utils.measureTextWidth(this.displayedMonthText, textClass);
this.monthArrow.setAttribute("style", `left:${monthWidth}`);
const monthWidth = Utils.measureTextWidth(this.displayedMonthText, textClass, this.containerElement);
this.monthArrowElement.setAttribute("style", `left:${monthWidth}`);

const yearWidth = Utils.measureTextWidth(this.displayedYearText, textClass);
this.yearArrow.setAttribute("style", `left:${yearWidth}`);
const yearWidth = Utils.measureTextWidth(this.displayedYearText, textClass, this.containerElement);
this.yearArrowElement.setAttribute("style", `left:${yearWidth}`);
}

private handleMonthSelectChange = (e: React.FormEvent<HTMLSelectElement>) => {
Expand Down