Skip to content

fix: onChange can also update other fields#564

Merged
wojtek-krysiak merged 1 commit intobetafrom
fix/561-inputs-were-not-updated-from-other-inputs
Aug 27, 2020
Merged

fix: onChange can also update other fields#564
wojtek-krysiak merged 1 commit intobetafrom
fix/561-inputs-were-not-updated-from-other-inputs

Conversation

@wojtek-krysiak
Copy link
Copy Markdown
Contributor

@wojtek-krysiak wojtek-krysiak commented Aug 27, 2020

@Co0sh there was an error where users couldn't update properties with onChange.

Baicaly this component didn't work:

import React from 'react'
import { EditPropertyProps } from 'admin-bro'
import { Button, Box } from '@admin-bro/design-system'

const ValueTrigger: React.FC<EditPropertyProps> = (props) => {
  const { onChange, record } = props

  const handleClick = (): void => {
    onChange({
      ...record,
      params: {
        ...record.params,
        name: 'my new name',
      },
    })
  }

  return (
    <Box mb="xxl">
      <Button data-testid="name-button" type="button" onClick={handleClick}>Set Name</Button>
    </Box>
  )
}

export default ValueTrigger

Name property is a string and inside there is an inner state which was only updated from the external params on component mount.

I've fixed that and will merge it, but please verify if this is ok.

const propValue = record.params?.[property.name] ?? ''
const [value, setValue] = useState(propValue)

const previous = usePrevious(propValue)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed usePrevious because the previous state is in value

setValue(propValue)
}
}, [])
}, [propValue])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added that which invalidates when external param was updated

@wojtek-krysiak wojtek-krysiak merged commit 5fc2fe6 into beta Aug 27, 2020
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 3.1.0-beta.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 3.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@wojtek-krysiak wojtek-krysiak deleted the fix/561-inputs-were-not-updated-from-other-inputs branch November 1, 2020 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant