Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EuiHighlight: converted to Typescript #2681

Merged
merged 5 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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 & {
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
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.