Skip to content

convert tab, tabs and searchBox to function components #7320

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

Merged
merged 1 commit into from
Jul 6, 2023
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
295 changes: 134 additions & 161 deletions lib/components/searchBox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback} from 'react';
import React, {useCallback, useRef, useEffect} from 'react';
import type {SearchBoxProps} from '../hyper';
import {VscArrowUp} from '@react-icons/all-files/vsc/VscArrowUp';
import {VscArrowDown} from '@react-icons/all-files/vsc/VscArrowDown';
Expand Down Expand Up @@ -82,179 +82,152 @@ const SearchButton = ({
);
};

class SearchBox extends React.PureComponent<SearchBoxProps> {
searchTerm: string;
input: HTMLInputElement | null = null;
searchButtonColors: SearchButtonColors;
const SearchBox = (props: SearchBoxProps) => {
const {
caseSensitive,
wholeWord,
regex,
results,
toggleCaseSensitive,
toggleWholeWord,
toggleRegex,
next,
prev,
close,
backgroundColor,
foregroundColor,
borderColor,
selectionColor,
font
} = props;

const searchTermRef = useRef<string>('');
const inputRef = useRef<HTMLInputElement | null>(null);

const handleChange = useCallback(
(event: React.KeyboardEvent<HTMLInputElement>) => {
searchTermRef.current = event.currentTarget.value;
if (event.shiftKey && event.key === 'Enter') {
prev(searchTermRef.current);
} else if (event.key === 'Enter') {
next(searchTermRef.current);
}
},
[prev, next]
);

constructor(props: SearchBoxProps) {
super(props);
this.searchTerm = '';
this.searchButtonColors = {
backgroundColor: this.props.borderColor,
selectionColor: this.props.selectionColor,
foregroundColor: this.props.foregroundColor
};
}
useEffect(() => {
inputRef.current?.focus();
}, [inputRef.current]);

handleChange = (event: React.KeyboardEvent<HTMLInputElement>) => {
this.searchTerm = event.currentTarget.value;
if (event.shiftKey && event.key === 'Enter') {
this.props.prev(this.searchTerm);
} else if (event.key === 'Enter') {
this.props.next(this.searchTerm);
}
const searchButtonColors: SearchButtonColors = {
backgroundColor: borderColor,
selectionColor,
foregroundColor
};

componentDidMount(): void {
this.input?.focus();
}

render() {
const {
caseSensitive,
wholeWord,
regex,
results,
toggleCaseSensitive,
toggleWholeWord,
toggleRegex,
next,
prev,
close,
backgroundColor,
foregroundColor,
borderColor,
selectionColor,
font
} = this.props;

return (
<div className="flex-row search-container">
<div className="flex-row search-box">
<input
className="search-input"
type="text"
onKeyDown={this.handleChange}
ref={(input) => {
this.input = input;
}}
placeholder="Search"
></input>

<SearchButton
onClick={toggleCaseSensitive}
active={caseSensitive}
title="Match Case"
{...this.searchButtonColors}
>
<VscCaseSensitive size="14px" />
</SearchButton>

<SearchButton
onClick={toggleWholeWord}
active={wholeWord}
title="Match Whole Word"
{...this.searchButtonColors}
>
<VscWholeWord size="14px" />
</SearchButton>

<SearchButton
onClick={toggleRegex}
active={regex}
title="Use Regular Expression"
{...this.searchButtonColors}
>
<VscRegex size="14px" />
</SearchButton>
</div>
return (
<div className="flex-row search-container">
<div className="flex-row search-box">
<input className="search-input" type="text" onKeyDown={handleChange} ref={inputRef} placeholder="Search" />

<span style={{minWidth: '60px', marginLeft: '4px'}}>
{results === undefined
? ''
: results.resultCount === 0
? 'No results'
: `${results.resultIndex + 1} of ${results.resultCount}`}
</span>
<SearchButton onClick={toggleCaseSensitive} active={caseSensitive} title="Match Case" {...searchButtonColors}>
<VscCaseSensitive size="14px" />
</SearchButton>

<div className="flex-row">
<SearchButton
onClick={() => prev(this.searchTerm)}
active={false}
title="Previous Match"
{...this.searchButtonColors}
>
<VscArrowUp size="14px" />
</SearchButton>
<SearchButton onClick={toggleWholeWord} active={wholeWord} title="Match Whole Word" {...searchButtonColors}>
<VscWholeWord size="14px" />
</SearchButton>

<SearchButton
onClick={() => next(this.searchTerm)}
active={false}
title="Next Match"
{...this.searchButtonColors}
>
<VscArrowDown size="14px" />
</SearchButton>
<SearchButton onClick={toggleRegex} active={regex} title="Use Regular Expression" {...searchButtonColors}>
<VscRegex size="14px" />
</SearchButton>
</div>

<SearchButton onClick={() => close()} active={false} title="Close" {...this.searchButtonColors}>
<VscClose size="14px" />
</SearchButton>
</div>
<span style={{minWidth: '60px', marginLeft: '4px'}}>
{results === undefined
? ''
: results.resultCount === 0
? 'No results'
: `${results.resultIndex + 1} of ${results.resultCount}`}
</span>

<div className="flex-row">
<SearchButton
onClick={() => prev(searchTermRef.current)}
active={false}
title="Previous Match"
{...searchButtonColors}
>
<VscArrowUp size="14px" />
</SearchButton>

<SearchButton
onClick={() => next(searchTermRef.current)}
active={false}
title="Next Match"
{...searchButtonColors}
>
<VscArrowDown size="14px" />
</SearchButton>

<SearchButton onClick={close} active={false} title="Close" {...searchButtonColors}>
<VscClose size="14px" />
</SearchButton>
</div>

<style jsx>
{`
.search-container {
background-color: ${backgroundColor};
border: 1px solid ${borderColor};
border-radius: 2px;
position: absolute;
right: 13px;
top: 4px;
z-index: 10;
padding: 4px;
font-family: ${font};
font-size: 12px;
}
<style jsx>
{`
.search-container {
background-color: ${backgroundColor};
border: 1px solid ${borderColor};
border-radius: 2px;
position: absolute;
right: 13px;
top: 4px;
z-index: 10;
padding: 4px;
font-family: ${font};
font-size: 12px;
}

.search-input {
outline: none;
background-color: transparent;
border: none;
color: ${foregroundColor};
align-self: stretch;
width: 100px;
}
.search-input {
outline: none;
background-color: transparent;
border: none;
color: ${foregroundColor};
align-self: stretch;
width: 100px;
}

.flex-row {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 4px;
}
.flex-row {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 4px;
}

.search-box {
border: none;
border-radius: 2px;
outline: ${borderColor} solid 1px;
background-color: ${backgroundColor};
color: ${foregroundColor};
padding: 0px 4px;
}
.search-box {
border: none;
border-radius: 2px;
outline: ${borderColor} solid 1px;
background-color: ${backgroundColor};
color: ${foregroundColor};
padding: 0px 4px;
}

.search-input::placeholder {
color: ${foregroundColor};
}
.search-input::placeholder {
color: ${foregroundColor};
}

.search-box:focus-within {
outline: ${selectionColor} solid 2px;
}
`}
</style>
</div>
);
}
}
.search-box:focus-within {
outline: ${selectionColor} solid 2px;
}
`}
</style>
</div>
);
};

export default SearchBox;
Loading