-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[search-in-workspace] Enable history for input fields
Signed-off-by: Gabriel Bodeen <[email protected]>
- Loading branch information
Showing
2 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
packages/search-in-workspace/src/browser/components/input-with-history.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import * as React from '@theia/core/shared/react'; | ||
import { Key, KeyCode } from '@theia/core/lib/browser'; | ||
import debounce = require('@theia/core/shared/lodash.debounce'); | ||
|
||
interface HistoryState { | ||
history: string[]; | ||
}; | ||
type InputAttributes = React.InputHTMLAttributes<HTMLInputElement>; | ||
|
||
export class InputWithHistory extends React.Component<InputAttributes, HistoryState> { | ||
static LIMIT = 100; | ||
|
||
private input = React.createRef<HTMLInputElement>(); | ||
|
||
constructor(props: InputAttributes) { | ||
super(props); | ||
this.state = { | ||
history: [], | ||
}; | ||
} | ||
|
||
componentDidUpdate(): void { | ||
// Catch updates from the StatefulWidget restore method | ||
if (this.value && !this.state.history.length) { | ||
this.setHistory([this.value]); | ||
} | ||
} | ||
|
||
setHistory(history: string[]): void { | ||
this.setState(prevState => ({ | ||
...prevState, | ||
history, | ||
})); | ||
} | ||
|
||
get value(): string { | ||
return this.input.current?.value ?? ''; | ||
} | ||
|
||
set value(value: string) { | ||
if (this.input.current) { | ||
this.input.current.value = value; | ||
} | ||
} | ||
|
||
/** | ||
* Handle history navigation without overriding the parent's onKeyDown handler, if any. | ||
*/ | ||
protected readonly onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>): void => { | ||
if (Key.ARROW_UP.keyCode === KeyCode.createKeyCode(e.nativeEvent).key?.keyCode) { | ||
e.preventDefault(); | ||
this.previousValue(); | ||
} else if (Key.ARROW_DOWN.keyCode === KeyCode.createKeyCode(e.nativeEvent).key?.keyCode) { | ||
e.preventDefault(); | ||
this.nextValue(); | ||
} | ||
this.props.onKeyDown?.(e); | ||
}; | ||
|
||
/** | ||
* Switch the input's text to the previous value, if any. | ||
*/ | ||
previousValue(): void { | ||
const { history } = this.state; | ||
const index = history.indexOf(this.value); | ||
if (index > 0 && index < history.length) { | ||
this.value = history[index - 1]; | ||
} | ||
} | ||
|
||
/** | ||
* Switch the input's text to the next value, if any. | ||
*/ | ||
nextValue(): void { | ||
const { history } = this.state; | ||
const index = history.indexOf(this.value); | ||
if (index >= 0 && index < history.length - 1) { | ||
this.value = history[index + 1]; | ||
} | ||
} | ||
|
||
/** | ||
* Handle history collection without overriding the parent's onChange handler, if any. | ||
*/ | ||
protected readonly onChange = (e: React.ChangeEvent<HTMLInputElement>): void => { | ||
this.addToHistory(); | ||
this.props.onChange?.(e); | ||
}; | ||
|
||
/** | ||
* Add a nonempty current value to the history, if not already present. (Debounced, 1 second delay.) | ||
*/ | ||
readonly addToHistory = debounce(this.doAddToHistory, 1000); | ||
|
||
private doAddToHistory(): void { | ||
if (!this.value) { | ||
return; | ||
} | ||
const { history } = this.state; | ||
const index = history.indexOf(this.value); | ||
if (index !== -1) { | ||
history.splice(index, 1); | ||
} | ||
this.applyLimit(history); | ||
this.setHistory(history.concat(this.value)); | ||
} | ||
|
||
private applyLimit(history: string[]): string[] { | ||
if (history.length > InputWithHistory.LIMIT) { | ||
history.splice(0, history.length - InputWithHistory.LIMIT); | ||
} | ||
return history; | ||
} | ||
|
||
render(): React.ReactNode { | ||
return ( | ||
<input | ||
{...this.props} | ||
onKeyDown={this.onKeyDown} | ||
onChange={this.onChange} | ||
ref={this.input} | ||
/> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters