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

Form fn on update form data #1789

Merged
merged 10 commits into from
Feb 2, 2021
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