mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Builds on top of the existing Playwright tests to plug in the test selector API: https://gist.github.com/bvaughn/d3c8b8842faf2ac2439bb11773a19cec My goals in doing this are to... 1. Experiment with the new API to see what works and what doesn't. 2. Add some test selector attributes (and remove DOM-structure based selectors). 3. Focus the tests on DevTools itself (rather than the test app). I also took this opportunity to add a few new test cases– like named hooks, editable props, component search, and profiling- just to play around more with the Playwright API. Relates to issue #22646
141 lines
4.0 KiB
JavaScript
141 lines
4.0 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import * as React from 'react';
|
|
import {useEffect, useRef} from 'react';
|
|
import Button from './Button';
|
|
import ButtonIcon from './ButtonIcon';
|
|
import Icon from './Icon';
|
|
|
|
import styles from './SearchInput.css';
|
|
|
|
type Props = {|
|
|
goToNextResult: () => void,
|
|
goToPreviousResult: () => void,
|
|
placeholder: string,
|
|
search: (text: string) => void,
|
|
searchIndex: number,
|
|
searchResultsCount: number,
|
|
searchText: string,
|
|
testName?: ?string,
|
|
|};
|
|
|
|
export default function SearchInput({
|
|
goToNextResult,
|
|
goToPreviousResult,
|
|
placeholder,
|
|
search,
|
|
searchIndex,
|
|
searchResultsCount,
|
|
searchText,
|
|
testName,
|
|
}: Props) {
|
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
const resetSearch = () => search('');
|
|
|
|
const handleChange = ({currentTarget}) => {
|
|
search(currentTarget.value);
|
|
};
|
|
const handleKeyPress = ({key, shiftKey}) => {
|
|
if (key === 'Enter') {
|
|
if (shiftKey) {
|
|
goToPreviousResult();
|
|
} else {
|
|
goToNextResult();
|
|
}
|
|
}
|
|
};
|
|
|
|
// Auto-focus search input
|
|
useEffect(() => {
|
|
if (inputRef.current === null) {
|
|
return () => {};
|
|
}
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
const {key, metaKey} = event;
|
|
if (key === 'f' && metaKey) {
|
|
if (inputRef.current !== null) {
|
|
inputRef.current.focus();
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
}
|
|
};
|
|
|
|
// It's important to listen to the ownerDocument to support the browser extension.
|
|
// Here we use portals to render individual tabs (e.g. Profiler),
|
|
// and the root document might belong to a different window.
|
|
const ownerDocument = inputRef.current.ownerDocument;
|
|
ownerDocument.addEventListener('keydown', handleKeyDown);
|
|
|
|
return () => ownerDocument.removeEventListener('keydown', handleKeyDown);
|
|
}, []);
|
|
|
|
return (
|
|
<div className={styles.SearchInput} data-testname={testName}>
|
|
<Icon className={styles.InputIcon} type="search" />
|
|
<input
|
|
data-testname={testName ? `${testName}-Input` : undefined}
|
|
className={styles.Input}
|
|
onChange={handleChange}
|
|
onKeyPress={handleKeyPress}
|
|
placeholder={placeholder}
|
|
ref={inputRef}
|
|
value={searchText}
|
|
/>
|
|
{!!searchText && (
|
|
<React.Fragment>
|
|
<span
|
|
className={styles.IndexLabel}
|
|
data-testname={testName ? `${testName}-ResultsCount` : undefined}>
|
|
{Math.min(searchIndex + 1, searchResultsCount)} |{' '}
|
|
{searchResultsCount}
|
|
</span>
|
|
<div className={styles.LeftVRule} />
|
|
<Button
|
|
data-testname={testName ? `${testName}-PreviousButton` : undefined}
|
|
className={styles.IconButton}
|
|
disabled={!searchText}
|
|
onClick={goToPreviousResult}
|
|
title={
|
|
<React.Fragment>
|
|
Scroll to previous search result (<kbd>Shift</kbd> +{' '}
|
|
<kbd>Enter</kbd>)
|
|
</React.Fragment>
|
|
}>
|
|
<ButtonIcon type="up" />
|
|
</Button>
|
|
<Button
|
|
data-testname={testName ? `${testName}-NextButton` : undefined}
|
|
className={styles.IconButton}
|
|
disabled={!searchText}
|
|
onClick={goToNextResult}
|
|
title={
|
|
<React.Fragment>
|
|
Scroll to next search result (<kbd>Enter</kbd>)
|
|
</React.Fragment>
|
|
}>
|
|
<ButtonIcon type="down" />
|
|
</Button>
|
|
<Button
|
|
data-testname={testName ? `${testName}-ResetButton` : undefined}
|
|
className={styles.IconButton}
|
|
disabled={!searchText}
|
|
onClick={resetSearch}
|
|
title="Reset search">
|
|
<ButtonIcon type="close" />
|
|
</Button>
|
|
</React.Fragment>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|