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

Feature: Add Expanding Query Input Field #1020

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getId, getTheme, Icon, ITextField, KeyCodes, Spinner, TextField, TooltipHost } from 'office-ui-fabric-react';
import { getId, getTheme, Icon, ITextField, KeyCodes, Spinner, TooltipHost, TextField } from 'office-ui-fabric-react';
ayubun marked this conversation as resolved.
Show resolved Hide resolved
import { ITooltipHostStyles } from 'office-ui-fabric-react/lib/components/Tooltip/TooltipHost.types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
Expand All @@ -23,6 +23,7 @@ import SuggestionsList from './SuggestionsList';

class AutoComplete extends Component<IAutoCompleteProps, IAutoCompleteState> {
private autoCompleteRef: React.RefObject<ITextField>;
private element: HTMLDivElement | null | undefined;

constructor(props: IAutoCompleteProps) {
super(props);
Expand All @@ -36,7 +37,8 @@ class AutoComplete extends Component<IAutoCompleteProps, IAutoCompleteState> {
showSuggestions: false,
userInput: this.props.sampleQuery.sampleUrl,
queryUrl: this.props.sampleQuery.sampleUrl,
compare: ''
compare: '',
multiline: false
};
}

Expand All @@ -57,13 +59,34 @@ class AutoComplete extends Component<IAutoCompleteProps, IAutoCompleteState> {
this.props.contentChanged(userInput);
};

public isOverflowing = (input: string) => {

function getTextWidth(text: string) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');

if (context === null) {
return 0;
}

context.font = getComputedStyle(document.body).font;
return context.measureText(text).width + 5;

}

return !!this.element
&& getTextWidth(input) > this.element.scrollWidth;

}

public onChange = (e: any) => {
const { suggestions, showSuggestions, userInput: previousUserInput, compare } = this.state;
const userInput = e.target.value;

this.setState({
userInput,
queryUrl: userInput
queryUrl: userInput,
multiline: this.isOverflowing(userInput)
ayubun marked this conversation as resolved.
Show resolved Hide resolved
});

if (showSuggestions && suggestions.length) {
Expand Down Expand Up @@ -247,7 +270,8 @@ class AutoComplete extends Component<IAutoCompleteProps, IAutoCompleteState> {
if (newUrl !== this.state.queryUrl) {
this.setState({
queryUrl: newUrl,
userInput: newUrl
userInput: newUrl,
multiline: this.isOverflowing(newUrl)
});
}
}
Expand Down Expand Up @@ -323,6 +347,7 @@ class AutoComplete extends Component<IAutoCompleteProps, IAutoCompleteState> {
userInput: selectedSuggestion,
compare: '',
queryUrl: selectedSuggestion,
multiline: this.isOverflowing(selectedSuggestion)
});
this.props.contentChanged(selectedSuggestion);
this.setFocus();
Expand Down Expand Up @@ -378,7 +403,8 @@ class AutoComplete extends Component<IAutoCompleteProps, IAutoCompleteState> {
filteredSuggestions,
showSuggestions,
userInput,
queryUrl
queryUrl,
multiline
} = this.state;

const currentTheme = getTheme();
Expand All @@ -388,20 +414,25 @@ class AutoComplete extends Component<IAutoCompleteProps, IAutoCompleteState> {

return (
<div onBlur={this.closeSuggestionDialog}>
<TextField
className={autoInput}
type='text'
autoComplete='off'
onChange={this.onChange}
onBlur={this.updateUrlContent}
onKeyDown={this.onKeyDown}
value={queryUrl}
componentRef={this.autoCompleteRef}
onRenderSuffix={(this.renderSuffix()) ? this.renderSuffix : undefined}
ariaLabel={translateMessage('Query Sample Input')}
role='textbox'
errorMessage={!queryUrl ? translateMessage('Missing url') : ''}
/>
<div ref={(el) => { this.element = el }}>
<TextField
className={autoInput}
multiline={multiline}
autoAdjustHeight
resizable={false}
type='text'
autoComplete='off'
onChange={this.onChange}
onBlur={this.updateUrlContent}
onKeyDown={this.onKeyDown}
value={queryUrl}
componentRef={this.autoCompleteRef}
onRenderSuffix={(this.renderSuffix()) ? this.renderSuffix : undefined}
ariaLabel={translateMessage('Query Sample Input')}
role='textbox'
errorMessage={!queryUrl ? translateMessage('Missing url') : ''}
/>
</div>
{showSuggestions && userInput && filteredSuggestions.length > 0 &&
<SuggestionsList
filteredSuggestions={filteredSuggestions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const autoCompleteStyles = (theme: ITheme) => {
borderWidth: '1px',
borderStyle: 'solid',
borderColor: theme.palette.neutralLight,
overflow: 'hidden',
overflow: 'hidden'
},
suggestionActive: {
display: 'block',
Expand All @@ -61,6 +61,7 @@ export const autoCompleteStyles = (theme: ITheme) => {
borderWidth: '1px',
borderStyle: 'solid',
overflow: 'hidden',
wordWrap: "normal",
backgroundColor: theme.palette.neutralLight
},
suggestionTitle: {
Expand Down
1 change: 1 addition & 0 deletions src/types/auto-complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface IAutoCompleteState {
userInput: string;
compare: string;
queryUrl: string;
multiline: boolean;
}

export interface ISuggestionsList {
Expand Down