Browse Source

Merge pull request #1736 from valpinkman/fix/autofocus-add-account

LL-659 fix autofocus on add account modal
gre-patch-1
Gaëtan Renaudeau 6 years ago
committed by GitHub
parent
commit
ed04304e82
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      src/components/SelectCurrency/index.js
  2. 18
      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}

18
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 = {
@ -37,6 +38,19 @@ export type Option = {
} }
class Select extends PureComponent<Props> { class Select extends PureComponent<Props> {
componentDidMount() {
if (this.ref && this.props.autoFocus) {
// $FlowFixMe
this.timeout = requestAnimationFrame(() => this.ref.focus())
}
}
componentWillUnmount() {
if (this.timeout) {
cancelAnimationFrame(this.timeout)
}
}
handleChange = (value, { action }) => { handleChange = (value, { action }) => {
const { onChange } = this.props const { onChange } = this.props
if (action === 'select-option') { if (action === 'select-option') {
@ -47,6 +61,9 @@ class Select extends PureComponent<Props> {
} }
} }
ref: *
timeout: *
render() { render() {
const { const {
value, value,
@ -68,6 +85,7 @@ class Select extends PureComponent<Props> {
return ( return (
<ReactSelect <ReactSelect
ref={c => (this.ref = c)}
value={value} value={value}
maxMenuHeight={300} maxMenuHeight={300}
classNamePrefix="select" classNamePrefix="select"

Loading…
Cancel
Save