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

Fix select widget loosing focus when the value has changed #4003

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- Improve collapsing of whitespace when pasting to slate text block @tiberiuichim
- Avoid warning for missing value in NumberWidget @tiberiuichim
- Fix crash in Slate link editing in a dexterity field @tiberiuichim
- Fix select widget loosing focus when the value has changed @reebalazs
Copy link
Member

Choose a reason for hiding this comment

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

@reebalazs For future PRs, please note that the changelog now uses towncrier. That will help avoid putting the release note under the wrong release, like here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for that - old habits leave on :)


## 16.1.0 (2022-11-23)

Expand Down
11 changes: 10 additions & 1 deletion src/components/manage/Form/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ class Form extends Component {
selected: selectedBlock,
multiSelected: [],
isClient: false,
// Ensure focus remain in field after change
inFocus: {},
};
this.onChangeField = this.onChangeField.bind(this);
this.onSelectBlock = this.onSelectBlock.bind(this);
Expand Down Expand Up @@ -324,6 +326,13 @@ class Form extends Component {
[id]:
value || (value !== undefined && isBoolean(value)) ? value : null,
},
// Changing the form data re-renders the select widget which causes the
// focus to get lost. To circumvent this, we set the focus back to
// the input.
// This could fix other widgets too but currently targeted
// against the select widget only.
// Ensure field to be in focus after the change
inFocus: { [id]: true },
};
});
}
Expand Down Expand Up @@ -607,7 +616,7 @@ class Form extends Component {
id={field}
fieldSet={item.title.toLowerCase()}
formData={this.state.formData}
focus={false}
focus={this.state.inFocus[field]}
value={this.state.formData?.[field]}
required={schema.required.indexOf(field) !== -1}
onChange={this.onChangeField}
Expand Down
3 changes: 3 additions & 0 deletions src/components/manage/Widgets/ArrayWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ class ArrayWidget extends Component {
isDisabled={this.props.disabled || this.props.isDisabled}
className="react-select-container"
classNamePrefix="react-select"
/* eslint-disable jsx-a11y/no-autofocus */
autoFocus={this.props.focus}
/* eslint-enable jsx-a11y/no-autofocus */
options={
this.props.vocabBaseUrl
? choices
Expand Down