From 1e6c5965a3e911ab8d2d1cf80bbc364232e6ccf2 Mon Sep 17 00:00:00 2001 From: mistakia <1823355+mistakia@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:24:26 -0500 Subject: [PATCH] feat: add keyboard shortcut for account & block search input --- .../representatives-search.styl | 15 ---- src/views/components/search-bar/search-bar.js | 79 +++++++++++-------- .../components/search-bar/search-bar.styl | 17 +++- 3 files changed, 62 insertions(+), 49 deletions(-) diff --git a/src/views/components/representatives-search/representatives-search.styl b/src/views/components/representatives-search/representatives-search.styl index 828ff196..d07c9580 100644 --- a/src/views/components/representatives-search/representatives-search.styl +++ b/src/views/components/representatives-search/representatives-search.styl @@ -16,18 +16,3 @@ &.left margin 8px -.search__shortcut-icon - display flex - align-items center - justify-content center - padding 4px - cursor pointer - margin-right 4px - - border 1px solid #e0e0e0 - border-radius 6px - - .MuiSvgIcon-root - width 14px - height 14px - diff --git a/src/views/components/search-bar/search-bar.js b/src/views/components/search-bar/search-bar.js index 93afa50f..ef5ae076 100644 --- a/src/views/components/search-bar/search-bar.js +++ b/src/views/components/search-bar/search-bar.js @@ -1,6 +1,8 @@ -import React from 'react' +import React, { useState, useEffect, useRef } from 'react' +import KeyboardCommandKeyIcon from '@mui/icons-material/KeyboardCommandKey' import ClearIcon from '@mui/icons-material/Clear' import SearchIcon from '@mui/icons-material/Search' +import hotkeys from 'hotkeys-js' import history from '@core/history' @@ -9,49 +11,60 @@ import './search-bar.styl' const ACCOUNT_REGEX = /((nano|xrb)_)?[13][13-9a-km-uw-z]{59}/ const BLOCK_REGEX = /[0-9A-F]{64}/ -export default class SearchBar extends React.Component { - constructor(props) { - super(props) +const SearchBar = () => { + const [value, setValue] = useState('') + const [invalid, setInvalid] = useState(false) + const input_ref = useRef(null) - this.state = { - value: '', - invalid: false + useEffect(() => { + const handle_hotkeys = (event, handler) => { + event.preventDefault() + input_ref.current.focus() } - } - handleClick = () => { - const value = '' - this.setState({ value }) + hotkeys('command+l,ctrl+l', handle_hotkeys) + + return () => { + hotkeys.unbind('command+l,ctrl+l') + } + }, []) + + const handle_click = () => { + setValue('') } - handleChange = (event) => { + const handle_change = (event) => { const { value } = event.target - this.setState({ value }) + setValue(value) if (ACCOUNT_REGEX.test(value) || BLOCK_REGEX.test(value)) { history.push(`/${value}`) } else { - this.setState({ invalid: true }) + setInvalid(true) } } - render() { - const isFilled = Boolean(this.state.value) - return ( -