Unverified Commit 7bbb9e62 authored by Andrei Eres's avatar Andrei Eres Committed by GitHub
Browse files

Debounce input (#123)

parent 789356a3
Pipeline #189896 passed with stages
in 3 minutes and 38 seconds
...@@ -2,7 +2,7 @@ import { useStore } from '@nanostores/react' ...@@ -2,7 +2,7 @@ import { useStore } from '@nanostores/react'
import { AccountJson } from '@polkadot/extension-base/background/types' import { AccountJson } from '@polkadot/extension-base/background/types'
import Identicon from '@polkadot/react-identicon' import Identicon from '@polkadot/react-identicon'
import { IconTheme } from '@polkadot/react-identicon/types' import { IconTheme } from '@polkadot/react-identicon/types'
import React, { useEffect, useState } from 'react' import React, { useCallback, useEffect, useState } from 'react'
import { useMetadata } from '../../hooks/useMetadata' import { useMetadata } from '../../hooks/useMetadata'
import { editAccount } from '../../messaging/uiActions' import { editAccount } from '../../messaging/uiActions'
import { accountsStore } from '../../stores/accounts' import { accountsStore } from '../../stores/accounts'
...@@ -33,8 +33,10 @@ export const Address: React.FC<Props> = ({ address, genesisHash, name }) => { ...@@ -33,8 +33,10 @@ export const Address: React.FC<Props> = ({ address, genesisHash, name }) => {
const chainLabel = ` · ${chain?.definition.chain.replace(RELAY_CHAIN, '')}` const chainLabel = ` · ${chain?.definition.chain.replace(RELAY_CHAIN, '')}`
const hashLabel = cropHash(recoded.formatted || address || UNKNOWN) const hashLabel = cropHash(recoded.formatted || address || UNKNOWN)
const changeName = (v: string) => const changeName = useCallback(
address && editAccount(address, v).catch(console.error) (v: string) => address && editAccount(address, v).catch(console.error),
[address]
)
useEffect(() => { useEffect(() => {
if (!address) return if (!address) return
......
import React from 'react' import React, { useCallback } from 'react'
import debounce from 'lodash.debounce'
type Props = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> & { type Props = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> & {
onChange: (value: string) => void onChange: (value: string) => void
...@@ -15,6 +16,11 @@ export const AutosizeInput: React.FC<Props> = ({ ...@@ -15,6 +16,11 @@ export const AutosizeInput: React.FC<Props> = ({
const span = React.useRef<HTMLSpanElement>(null) const span = React.useRef<HTMLSpanElement>(null)
const isContentChangedRef = React.useRef(false) const isContentChangedRef = React.useRef(false)
const debouncedOnChange = useCallback(
debounce((v: string) => onChange(v), 100),
[onChange]
)
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (!isContentChangedRef.current) isContentChangedRef.current = true if (!isContentChangedRef.current) isContentChangedRef.current = true
setContent(e.currentTarget.value) setContent(e.currentTarget.value)
...@@ -27,7 +33,7 @@ export const AutosizeInput: React.FC<Props> = ({ ...@@ -27,7 +33,7 @@ export const AutosizeInput: React.FC<Props> = ({
React.useEffect(() => { React.useEffect(() => {
setWidth(span.current?.offsetWidth || 0) setWidth(span.current?.offsetWidth || 0)
if (isContentChangedRef.current) onChange(content) if (isContentChangedRef.current) debouncedOnChange(content)
}, [content]) }, [content])
return ( return (
......
...@@ -1853,6 +1853,18 @@ ...@@ -1853,6 +1853,18 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"
integrity sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ== integrity sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==
"@types/lodash.debounce@^4.0.7":
version "4.0.7"
resolved "https://registry.yarnpkg.com/@types/lodash.debounce/-/lodash.debounce-4.0.7.tgz#0285879defb7cdb156ae633cecd62d5680eded9f"
integrity sha512-X1T4wMZ+gT000M2/91SYj0d/7JfeNZ9PeeOldSNoE/lunLeQXKvkmIumI29IaKMotU/ln/McOIvgzZcQ/3TrSA==
dependencies:
"@types/lodash" "*"
"@types/lodash@*":
version "4.14.182"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2"
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==
"@types/minimatch@^3.0.3": "@types/minimatch@^3.0.3":
version "3.0.5" version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment