Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix(ui): ensure request form amount autofocus
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Aug 19, 2019
1 parent 07c6d7e commit cb9ccb6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
12 changes: 8 additions & 4 deletions renderer/components/Form/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const SearchIcon = styled(Search)`
// Create an html input element that accepts all style props from styled-system.
export const SystemInput = createSystemInput('input')

const maybeFocusRef = ref => {
if (ref.current && document.activeElement !== ref.current) {
ref.current.focus()
}
}

const Input = ({
description,
onChange,
Expand Down Expand Up @@ -55,10 +61,8 @@ const Input = ({
const { value, maskedValue, error, asyncError } = fieldState

useLayoutEffect(() => {
if (willAutoFocus) {
forwardedRef.current.focus()
}
}, [willAutoFocus, forwardedRef])
willAutoFocus && maybeFocusRef(forwardedRef)
}, [forwardedRef, willAutoFocus])

const getValue = () => {
if (typeof value === 'undefined') {
Expand Down
12 changes: 8 additions & 4 deletions renderer/components/Form/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import { createSystemInput } from './util'
// Create an html input element that accepts all style props from styled-system.
export const SystemTextArea = createSystemInput('textarea')

const maybeFocusRef = ref => {
if (ref.current && document.activeElement !== ref.current) {
ref.current.focus()
}
}

const TextArea = ({
description,
onChange,
Expand Down Expand Up @@ -42,10 +48,8 @@ const TextArea = ({
const { value, maskedValue, error, asyncError } = fieldState

useLayoutEffect(() => {
if (willAutoFocus) {
forwardedRef.current.focus()
}
}, [forwardedRef, willAutoFocus])
willAutoFocus && maybeFocusRef(forwardedRef)
}, [willAutoFocus, forwardedRef])

const getValue = () => {
if (typeof value === 'undefined') {
Expand Down
20 changes: 5 additions & 15 deletions renderer/components/Request/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Request extends React.Component {
fetchTickers: PropTypes.func.isRequired,
intl: intlShape.isRequired,
invoice: PropTypes.object,
isAnimating: PropTypes.bool,
isProcessing: PropTypes.bool,
payReq: PropTypes.string,
showNotification: PropTypes.func.isRequired,
Expand All @@ -39,12 +40,6 @@ class Request extends React.Component {

amountInput = React.createRef()

componentDidMount() {
const { fetchTickers } = this.props
fetchTickers()
this.focusAmountInput()
}

componentDidUpdate(prevProps) {
const { payReq } = this.props
const { currentStep } = this.state
Expand Down Expand Up @@ -103,15 +98,6 @@ class Request extends React.Component {
this.formApi = formApi
}

/**
* focusAmountInput - Focus the amount input.
*/
focusAmountInput = () => {
if (this.amountInput.current) {
this.amountInput.current.focus()
}
}

renderHelpText = () => {
const { chainName, cryptoUnitName } = this.props
return (
Expand All @@ -125,12 +111,14 @@ class Request extends React.Component {
}

renderAmountFields = () => {
const { isAnimating } = this.props
return (
<CurrencyFieldGroup
formApi={this.formApi}
forwardedRef={this.amountInput}
isRequired
mb={3}
willAutoFocus={!isAnimating}
/>
)
}
Expand Down Expand Up @@ -186,12 +174,14 @@ class Request extends React.Component {
fetchTickers,
intl,
isProcessing,
isAnimating,
invoice,
payReq,
showNotification,
...rest
} = this.props
const { currentStep } = this.state

return (
<Form
css={`
Expand Down
17 changes: 12 additions & 5 deletions renderer/containers/ModalStack.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { animated, Transition } from 'react-spring/renderprops.cjs'
Expand All @@ -21,7 +21,7 @@ const Container = styled(animated.div)`
${ModalOverlayStyles}
`

const ModalContent = ({ type, closeModal }) => {
const ModalContent = ({ type, closeModal, isAnimating }) => {
switch (type) {
case 'SETTINGS':
return (
Expand Down Expand Up @@ -54,7 +54,7 @@ const ModalContent = ({ type, closeModal }) => {
case 'REQUEST_FORM':
return (
<Modal onClose={closeModal} p={4}>
<Request mx="auto" width={9 / 16} />
<Request isAnimating={isAnimating} mx="auto" width={9 / 16} />
</Modal>
)

Expand Down Expand Up @@ -97,11 +97,12 @@ const ModalContent = ({ type, closeModal }) => {

ModalContent.propTypes = {
closeModal: PropTypes.func.isRequired,
isAnimating: PropTypes.bool,
type: PropTypes.string.isRequired,
}

/**
* ModalStack - Render modqals from the modal stack.
* ModalStack - Render modals from the modal stack.
*
* @param {{ modals, closeModal }} props Props
* @returns {Node} Node
Expand All @@ -111,6 +112,10 @@ function ModalStack(props) {
const doCloseModal = () => closeModal()

useOnKeydown('Escape', closeModal)
const [isAnimating, setIsAnimating] = useState(false)

const onStart = () => setIsAnimating(true)
const onRest = () => setIsAnimating(false)

return (
<Transition
Expand All @@ -119,13 +124,15 @@ function ModalStack(props) {
items={modals}
keys={item => item.id}
leave={{ opacity: 0, pointerEvents: 'none' }}
onRest={onRest}
onStart={onStart}
>
{modal =>
modal &&
/* eslint-disable react/display-name */
(styles => (
<Container style={styles}>
<ModalContent closeModal={doCloseModal} type={modal.type} />
<ModalContent closeModal={doCloseModal} isAnimating={isAnimating} type={modal.type} />
</Container>
))
}
Expand Down

0 comments on commit cb9ccb6

Please sign in to comment.