Browse Source

autofocus on input only if autoFocus prop is present

gre-patch-1
Valentin D. Pinkman 6 years ago
parent
commit
d0c0c2c415
No known key found for this signature in database GPG Key ID: E7D110669FFB8D3E
  1. 12
      src/components/SelectCurrency/index.js
  2. 7
      src/components/base/Select/index.js

12
src/components/SelectCurrency/index.js

@ -18,6 +18,7 @@ type OwnProps = {
currencies?: CryptoCurrency[], currencies?: CryptoCurrency[],
value?: CryptoCurrency, value?: CryptoCurrency,
placeholder: string, placeholder: string,
autoFocus?: boolean,
t: T, t: T,
} }
@ -29,12 +30,21 @@ const mapStateToProps = (state, props: OwnProps) => ({
currencies: props.currencies || availableCurrencies(state), currencies: props.currencies || availableCurrencies(state),
}) })
const SelectCurrency = ({ onChange, value, t, placeholder, currencies, ...props }: Props) => { const SelectCurrency = ({
onChange,
value,
t,
placeholder,
currencies,
autoFocus,
...props
}: Props) => {
const options = currencies const options = currencies
? currencies.map(c => ({ ...c, value: c.id, label: c.name, currency: c })) ? currencies.map(c => ({ ...c, value: c.id, label: c.name, currency: c }))
: [] : []
return ( return (
<Select <Select
autoFocus={autoFocus}
value={value} value={value}
renderOption={renderOption} renderOption={renderOption}
renderValue={renderOption} renderValue={renderOption}

7
src/components/base/Select/index.js

@ -28,6 +28,7 @@ type Props = {
small: boolean, small: boolean,
width: number, width: number,
minWidth: number, minWidth: number,
autoFocus: boolean,
} }
export type Option = { export type Option = {
@ -38,15 +39,15 @@ export type Option = {
class Select extends PureComponent<Props> { class Select extends PureComponent<Props> {
componentDidMount() { componentDidMount() {
if (this.ref) { if (this.ref && this.props.autoFocus) {
// $FlowFixMe // $FlowFixMe
this.timeout = setTimeout(() => this.ref.focus(), 16) this.timeout = requestAnimationFrame(() => this.ref.focus())
} }
} }
componentWillUnmount() { componentWillUnmount() {
if (this.timeout) { if (this.timeout) {
clearTimeout(this.timeout) cancelAnimationFrame(this.timeout)
} }
} }

Loading…
Cancel
Save