Convert tab, tabs and searchBox to function components

This commit is contained in:
Labhansh Agrawal 2023-07-06 20:10:14 +05:30 committed by GitHub
parent a793a1d9a4
commit 6c8d0433d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 360 additions and 391 deletions

View file

@ -1,4 +1,4 @@
import React, {useCallback} from 'react'; import React, {useCallback, useRef, useEffect} from 'react';
import type {SearchBoxProps} from '../hyper'; import type {SearchBoxProps} from '../hyper';
import {VscArrowUp} from '@react-icons/all-files/vsc/VscArrowUp'; import {VscArrowUp} from '@react-icons/all-files/vsc/VscArrowUp';
import {VscArrowDown} from '@react-icons/all-files/vsc/VscArrowDown'; import {VscArrowDown} from '@react-icons/all-files/vsc/VscArrowDown';
@ -82,35 +82,7 @@ const SearchButton = ({
); );
}; };
class SearchBox extends React.PureComponent<SearchBoxProps> { const SearchBox = (props: SearchBoxProps) => {
searchTerm: string;
input: HTMLInputElement | null = null;
searchButtonColors: SearchButtonColors;
constructor(props: SearchBoxProps) {
super(props);
this.searchTerm = '';
this.searchButtonColors = {
backgroundColor: this.props.borderColor,
selectionColor: this.props.selectionColor,
foregroundColor: this.props.foregroundColor
};
}
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);
}
};
componentDidMount(): void {
this.input?.focus();
}
render() {
const { const {
caseSensitive, caseSensitive,
wholeWord, wholeWord,
@ -127,45 +99,47 @@ class SearchBox extends React.PureComponent<SearchBoxProps> {
borderColor, borderColor,
selectionColor, selectionColor,
font font
} = this.props; } = 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]
);
useEffect(() => {
inputRef.current?.focus();
}, [inputRef.current]);
const searchButtonColors: SearchButtonColors = {
backgroundColor: borderColor,
selectionColor,
foregroundColor
};
return ( return (
<div className="flex-row search-container"> <div className="flex-row search-container">
<div className="flex-row search-box"> <div className="flex-row search-box">
<input <input className="search-input" type="text" onKeyDown={handleChange} ref={inputRef} placeholder="Search" />
className="search-input"
type="text"
onKeyDown={this.handleChange}
ref={(input) => {
this.input = input;
}}
placeholder="Search"
></input>
<SearchButton <SearchButton onClick={toggleCaseSensitive} active={caseSensitive} title="Match Case" {...searchButtonColors}>
onClick={toggleCaseSensitive}
active={caseSensitive}
title="Match Case"
{...this.searchButtonColors}
>
<VscCaseSensitive size="14px" /> <VscCaseSensitive size="14px" />
</SearchButton> </SearchButton>
<SearchButton <SearchButton onClick={toggleWholeWord} active={wholeWord} title="Match Whole Word" {...searchButtonColors}>
onClick={toggleWholeWord}
active={wholeWord}
title="Match Whole Word"
{...this.searchButtonColors}
>
<VscWholeWord size="14px" /> <VscWholeWord size="14px" />
</SearchButton> </SearchButton>
<SearchButton <SearchButton onClick={toggleRegex} active={regex} title="Use Regular Expression" {...searchButtonColors}>
onClick={toggleRegex}
active={regex}
title="Use Regular Expression"
{...this.searchButtonColors}
>
<VscRegex size="14px" /> <VscRegex size="14px" />
</SearchButton> </SearchButton>
</div> </div>
@ -180,24 +154,24 @@ class SearchBox extends React.PureComponent<SearchBoxProps> {
<div className="flex-row"> <div className="flex-row">
<SearchButton <SearchButton
onClick={() => prev(this.searchTerm)} onClick={() => prev(searchTermRef.current)}
active={false} active={false}
title="Previous Match" title="Previous Match"
{...this.searchButtonColors} {...searchButtonColors}
> >
<VscArrowUp size="14px" /> <VscArrowUp size="14px" />
</SearchButton> </SearchButton>
<SearchButton <SearchButton
onClick={() => next(this.searchTerm)} onClick={() => next(searchTermRef.current)}
active={false} active={false}
title="Next Match" title="Next Match"
{...this.searchButtonColors} {...searchButtonColors}
> >
<VscArrowDown size="14px" /> <VscArrowDown size="14px" />
</SearchButton> </SearchButton>
<SearchButton onClick={() => close()} active={false} title="Close" {...this.searchButtonColors}> <SearchButton onClick={close} active={false} title="Close" {...searchButtonColors}>
<VscClose size="14px" /> <VscClose size="14px" />
</SearchButton> </SearchButton>
</div> </div>
@ -254,7 +228,6 @@ class SearchBox extends React.PureComponent<SearchBoxProps> {
</style> </style>
</div> </div>
); );
} };
}
export default SearchBox; export default SearchBox;

View file

@ -1,55 +1,50 @@
import React from 'react'; import React from 'react';
import type {TabProps} from '../hyper'; import type {TabProps} from '../hyper';
export default class Tab extends React.PureComponent<TabProps> { const Tab = (props: TabProps) => {
constructor(props: TabProps) { const handleClick = (event: React.MouseEvent) => {
super(props);
}
handleClick = (event: React.MouseEvent) => {
const isLeftClick = event.nativeEvent.which === 1; const isLeftClick = event.nativeEvent.which === 1;
if (isLeftClick && !this.props.isActive) { if (isLeftClick && !props.isActive) {
this.props.onSelect(); props.onSelect();
} }
}; };
handleMouseUp = (event: React.MouseEvent) => { const handleMouseUp = (event: React.MouseEvent) => {
const isMiddleClick = event.nativeEvent.which === 2; const isMiddleClick = event.nativeEvent.which === 2;
if (isMiddleClick) { if (isMiddleClick) {
this.props.onClose(); props.onClose();
} }
}; };
render() { const {isActive, isFirst, isLast, borderColor, hasActivity} = props;
const {isActive, isFirst, isLast, borderColor, hasActivity} = this.props;
return ( return (
<React.Fragment> <>
<li <li
onClick={this.props.onClick} onClick={props.onClick}
style={{borderColor}} style={{borderColor}}
className={`tab_tab ${isFirst ? 'tab_first' : ''} ${isActive ? 'tab_active' : ''} ${ className={`tab_tab ${isFirst ? 'tab_first' : ''} ${isActive ? 'tab_active' : ''} ${
isFirst && isActive ? 'tab_firstActive' : '' isFirst && isActive ? 'tab_firstActive' : ''
} ${hasActivity ? 'tab_hasActivity' : ''}`} } ${hasActivity ? 'tab_hasActivity' : ''}`}
> >
{this.props.customChildrenBefore} {props.customChildrenBefore}
<span <span
className={`tab_text ${isLast ? 'tab_textLast' : ''} ${isActive ? 'tab_textActive' : ''}`} className={`tab_text ${isLast ? 'tab_textLast' : ''} ${isActive ? 'tab_textActive' : ''}`}
onClick={this.handleClick} onClick={handleClick}
onMouseUp={this.handleMouseUp} onMouseUp={handleMouseUp}
> >
<span title={this.props.text} className="tab_textInner"> <span title={props.text} className="tab_textInner">
{this.props.text} {props.text}
</span> </span>
</span> </span>
<i className="tab_icon" onClick={this.props.onClose}> <i className="tab_icon" onClick={props.onClose}>
<svg className="tab_shape"> <svg className="tab_shape">
<use xlinkHref="./renderer/assets/icons.svg#close-tab" /> <use xlinkHref="./renderer/assets/icons.svg#close-tab" />
</svg> </svg>
</i> </i>
{this.props.customChildren} {props.customChildren}
</li> </li>
<style jsx>{` <style jsx>{`
@ -158,7 +153,8 @@ export default class Tab extends React.PureComponent<TabProps> {
shape-rendering: crispEdges; shape-rendering: crispEdges;
} }
`}</style> `}</style>
</React.Fragment> </>
); );
} };
}
export default Tab;

View file

@ -9,22 +9,21 @@ import DropdownButton from './new-tab';
const Tab = decorate(Tab_, 'Tab'); const Tab = decorate(Tab_, 'Tab');
const isMac = /Mac/.test(navigator.userAgent); const isMac = /Mac/.test(navigator.userAgent);
export default class Tabs extends React.PureComponent<TabsProps> { const Tabs = (props: TabsProps) => {
render() { const {tabs = [], borderColor, onChange, onClose, fullScreen} = props;
const {tabs = [], borderColor, onChange, onClose, fullScreen} = this.props;
const hide = !isMac && tabs.length === 1; const hide = !isMac && tabs.length === 1;
return ( return (
<nav className={`tabs_nav ${hide ? 'tabs_hiddenNav' : ''}`}> <nav className={`tabs_nav ${hide ? 'tabs_hiddenNav' : ''}`}>
{this.props.customChildrenBefore} {props.customChildrenBefore}
{tabs.length === 1 && isMac ? <div className="tabs_title">{tabs[0].title}</div> : null} {tabs.length === 1 && isMac ? <div className="tabs_title">{tabs[0].title}</div> : null}
{tabs.length > 1 {tabs.length > 1 ? (
? [ <>
<ul key="list" className={`tabs_list ${fullScreen && isMac ? 'tabs_fullScreen' : ''}`}> <ul key="list" className={`tabs_list ${fullScreen && isMac ? 'tabs_fullScreen' : ''}`}>
{tabs.map((tab, i) => { {tabs.map((tab, i) => {
const {uid, title, isActive, hasActivity} = tab; const {uid, title, isActive, hasActivity} = tab;
const props = getTabProps(tab, this.props, { const tabProps = getTabProps(tab, props, {
text: title === '' ? 'Shell' : title, text: title === '' ? 'Shell' : title,
isFirst: i === 0, isFirst: i === 0,
isLast: tabs.length - 1 === i, isLast: tabs.length - 1 === i,
@ -34,20 +33,20 @@ export default class Tabs extends React.PureComponent<TabsProps> {
onSelect: onChange.bind(null, uid), onSelect: onChange.bind(null, uid),
onClose: onClose.bind(null, uid) onClose: onClose.bind(null, uid)
}); });
return <Tab key={`tab-${uid}`} {...props} />; return <Tab key={`tab-${uid}`} {...tabProps} />;
})} })}
</ul>, </ul>
isMac && ( {isMac && (
<div <div
key="shim" key="shim"
style={{borderColor}} style={{borderColor}}
className={`tabs_borderShim ${fullScreen ? 'tabs_borderShimUndo' : ''}`} className={`tabs_borderShim ${fullScreen ? 'tabs_borderShimUndo' : ''}`}
/> />
) )}
] </>
: null} ) : null}
<DropdownButton {...this.props} tabsVisible={tabs.length > 1} /> <DropdownButton {...props} tabsVisible={tabs.length > 1} />
{this.props.customChildren} {props.customChildren}
<style jsx>{` <style jsx>{`
.tabs_nav { .tabs_nav {
@ -107,5 +106,6 @@ export default class Tabs extends React.PureComponent<TabsProps> {
`}</style> `}</style>
</nav> </nav>
); );
} };
}
export default Tabs;