Skip to content

Commit

Permalink
EuiHighlight: converted to Typescript (#2681)
Browse files Browse the repository at this point in the history
* Tranlates EuiHighlight to Typescript

* Adds CHANGELOG line

* Corrects what was pointed out in the review

* Removes propType

* Removes className from props
  • Loading branch information
ffknob authored and chandlerprall committed Dec 17, 2019
1 parent ecbe941 commit e811631
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 54 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `17.2.1`.
- Converted `EuiHighlight` to Typescript ([#2681](https://github.com/elastic/eui/pull/2681))

## [`17.2.1`](https://github.com/elastic/eui/tree/v17.2.1)

Expand Down
53 changes: 0 additions & 53 deletions src/components/highlight/highlight.js

This file was deleted.

File renamed without changes.
73 changes: 73 additions & 0 deletions src/components/highlight/highlight.tsx
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.

0 comments on commit e811631

Please sign in to comment.