Skip to content

Commit

Permalink
Form fn on update form data (#1789)
Browse files Browse the repository at this point in the history
* added onChangeFormData prop to Form component

* added preventDefault on addBlock button to prevent form submit if blocksForm is used inside a <form> tag

* added preventDefault on button click if blocks are reused in form, and make componentDidUpdate async in form

* updated changelog

* fixed updating formData

* removed updating state from props because usless

Co-authored-by: Giulia Ghisini <giulia.ghisini@redturtle.it>
Co-authored-by: Víctor Fernández de Alba <sneridagh@gmail.com>
  • Loading branch information
3 people authored Feb 2, 2021
1 parent ca220f8 commit b131a18
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Feature

- Added onChangeFormData prop to Form component @giuliaghisini
- Internationalization story for add-ons @sneridagh

### Bugfix
Expand Down
1 change: 1 addition & 0 deletions src/components/manage/Blocks/Image/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ class Edit extends Component {
icon
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
this.props.openObjectBrowser();
}}
>
Expand Down
4 changes: 3 additions & 1 deletion src/components/manage/Blocks/Text/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ class Edit extends Component {
this.setState({ editorState });
}

toggleAddNewBlock = () =>
toggleAddNewBlock = (e) => {
e.preventDefault();
this.setState((state) => ({ addNewBlockOpened: !state.addNewBlockOpened }));
};

handleClickOutside = (e) => {
if (
Expand Down
12 changes: 11 additions & 1 deletion src/components/manage/Form/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Form extends Component {
description: PropTypes.string,
visual: PropTypes.bool,
blocks: PropTypes.arrayOf(PropTypes.object),
onChangeFormData: PropTypes.func,
requestError: PropTypes.string,
allowedBlocks: PropTypes.arrayOf(PropTypes.string),
showRestricted: PropTypes.bool,
Expand Down Expand Up @@ -202,7 +203,7 @@ class Form extends Component {
* also the first Tab to have any errors will be selected
* @param {Object} prevProps
*/
componentDidUpdate(prevProps) {
async componentDidUpdate(prevProps, prevState) {
let { requestError } = this.props;
let errors = {};
let activeIndex = 0;
Expand All @@ -221,6 +222,15 @@ class Form extends Component {
activeIndex,
});
}

if (this.props.onChangeFormData) {
if (
JSON.stringify(prevState?.formData) !==
JSON.stringify(this.state.formData)
) {
this.props.onChangeFormData(this.state.formData);
}
}
}

/**
Expand Down

0 comments on commit b131a18

Please sign in to comment.