/
githubmr
/
facebook-react
Обзор
Документация
Войти
/
githubmr
/
facebook-react
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
packages/react-devtools-shared/src/devtools/views/Components/IndexableDisplayName.js
63 строки
2 KB
Ruslan Lesiutin
feat[devtools]: display Forget badge for the relevant components (#27709)
23 ноя 2023, 21:37
Не верифицирован
23 ноя 2023, 21:37
6c7b41d
Код
Авторство
О чём код?
/** * Copyright (c) Meta Platforms, Inc. and 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 {createRegExp} from '../utils'; import {TreeStateContext} from './TreeContext'; import styles from './Element.css'; const {useMemo, useContext} = React; type Props = { displayName: string | null, id: number, }; function IndexableDisplayName({displayName, id}: Props): React.Node { const {searchIndex, searchResults, searchText} = useContext(TreeStateContext); const isSearchResult = useMemo(() => { return searchResults.includes(id); }, [id, searchResults]); const isCurrentResult = searchIndex !== null && id === searchResults[searchIndex]; if (!isSearchResult || displayName === null) { return displayName; } const match = createRegExp(searchText).exec(displayName); if (match === null) { return displayName; } const startIndex = match.index; const stopIndex = startIndex + match[0].length; const children = []; if (startIndex > 0) { children.push(<span key="begin">{displayName.slice(0, startIndex)}</span>); } children.push( <mark key="middle" className={isCurrentResult ? styles.CurrentHighlight : styles.Highlight}> {displayName.slice(startIndex, stopIndex)} </mark>, ); if (stopIndex < displayName.length) { children.push(<span key="end">{displayName.slice(stopIndex)}</span>); } return children; } export default IndexableDisplayName;