Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: inputs in Modals lose focus every key press #1622

Open
AshotN opened this issue Feb 12, 2024 · 5 comments
Open

[Bug]: inputs in Modals lose focus every key press #1622

AshotN opened this issue Feb 12, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@AshotN
Copy link
Contributor

AshotN commented Feb 12, 2024

Contact Details

No response

What happened?

I am using a custom component that is a modal with an input field in it. Every onChange event causes a re-render and the input loses focus.

Bug prevalence

Often

AdminJS dependencies version

 "@adminjs/design-system": "4.0.3",
 "@adminjs/koa": "4.1.0",
 "adminjs": "7.5.10",

What browsers do you see the problem on?

Firefox

Relevant log output

No response

Relevant code that's giving you issues

import React, { useState } from 'react'
import { Button, FormGroup, Input, InputGroup, Modal } from '@adminjs/design-system'
import { ApiClient, BasePropertyProps } from 'adminjs'

const ModalExample = (props: BasePropertyProps) => {
  const { record, resource } = props
  if (!record) throw new Error('Missing record')
  const { params } = record

  const [amount, setAmount] = useState(0)

  const api = new ApiClient()

  return (
    <Modal>
      <form
        onSubmit={(e) => {
          e.preventDefault()
          api.recordAction({
            actionName: 'asdf',
            resourceId: resource.id,
            recordId: record.id,
            data: {
              amount
            }
          })
        }}
      >
        <FormGroup>
          <div
            style={{ display: 'flex', flexDirection: 'column', gap: '10px', justifyContent: 'space-between' }}
          >
            <InputGroup>
              <label>
                Amount:{' '}
                <Input
                  placeholder="Base APY"
                  type="number"
                  value={amount}
                  onChange={(e: React.ChangeEvent<HTMLInputElement>) => setAmount(parseFloat(e.target.value))}
                />
              </label>
            </InputGroup>
            <Button variant="success" type="submit">
              Submit
            </Button>
          </div>
        </FormGroup>
      </form>
    </Modal>
  )
}

export default ModalExample
@AshotN AshotN added the bug Something isn't working label Feb 12, 2024
@eakenbor
Copy link

@AshotN did you find a solution to this?

@eakenbor
Copy link

@dziraf please can you have a look at this?

@AshotN
Copy link
Contributor Author

AshotN commented Apr 15, 2024

No, I haven't found a solution. It's still a problem for me.

@eakenbor
Copy link

@AshotN I managed to get around it using useRef by doing the following:

<Modal>
        <Input
                    ref={amountRef}
                    required
                    type='number'
                    value={amount}
                    onChange={event => {
                      setAmount(parseFloat(event.target.value))
                      setTimeout(() => {
                        amountRef.current.focus()
                      }, 0)
                    }}
                    disabled={type == 'SERVICE'}
                  ></Input>
</Modal>

However, this hack does not work for CurrencyInput.

@dziraf do you intend to solve this bug anytime soon? CurrencyInput is very important to us.

@AshotN
Copy link
Contributor Author

AshotN commented Apr 24, 2024

Ya that's a bit hacky, but probably a good stop gap. Hoping there is a proper fix soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants