-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EuiHighlight: converted to Typescript (#2681)
* Tranlates EuiHighlight to Typescript * Adds CHANGELOG line * Corrects what was pointed out in the review * Removes propType * Removes className from props
- Loading branch information
1 parent
ecbe941
commit e811631
Showing
6 changed files
with
74 additions
and
54 deletions.
There are no files selected for viewing
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,73 @@ | ||
import React, { Fragment, HTMLAttributes, FunctionComponent } from 'react'; | ||
import { CommonProps } from '../common'; | ||
|
||
export type EuiHighlightProps = HTMLAttributes<HTMLSpanElement> & | ||
CommonProps & { | ||
children: string; | ||
|
||
/** | ||
* What to search for | ||
*/ | ||
search: string; | ||
|
||
/** | ||
* Should the search be strict or not | ||
*/ | ||
strict?: boolean; | ||
}; | ||
|
||
const highlight = ( | ||
searchSubject: string, | ||
searchValue: string, | ||
isStrict: boolean = false | ||
) => { | ||
if (!searchValue) { | ||
return searchSubject; | ||
} | ||
|
||
if (!searchSubject) { | ||
return null; | ||
} | ||
|
||
const normalizedSearchSubject: string = isStrict | ||
? searchSubject | ||
: searchSubject.toLowerCase(); | ||
const normalizedSearchValue: string = isStrict | ||
? searchValue | ||
: searchValue.toLowerCase(); | ||
|
||
const indexOfMatch: number = normalizedSearchSubject.indexOf( | ||
normalizedSearchValue | ||
); | ||
if (indexOfMatch === -1) { | ||
return searchSubject; | ||
} | ||
|
||
const preMatch: string = searchSubject.substr(0, indexOfMatch); | ||
const match: string = searchSubject.substr(indexOfMatch, searchValue.length); | ||
const postMatch: string = searchSubject.substr( | ||
indexOfMatch + searchValue.length | ||
); | ||
|
||
return ( | ||
<Fragment> | ||
{preMatch} | ||
<strong>{match}</strong> | ||
{postMatch} | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export const EuiHighlight: FunctionComponent<EuiHighlightProps> = ({ | ||
children, | ||
className, | ||
search, | ||
strict, | ||
...rest | ||
}) => { | ||
return ( | ||
<span className={className} {...rest}> | ||
{highlight(children, search, strict)} | ||
</span> | ||
); | ||
}; |
File renamed without changes.