Skip to content

Commit

Permalink
fix(widget-string): fix cursor jumping to end of line
Browse files Browse the repository at this point in the history
  • Loading branch information
iansinnott committed Nov 19, 2020
1 parent 8326e8e commit 27657b6
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/netlify-cms-widget-string/src/StringControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ export default class StringControl extends React.Component {
value: '',
};

// The selection to maintain for the input element
_sel = 0;

// The input element ref
_el = null;

// NOTE: This prevents the cursor from jumping to the end of the text for
// nested inputs. In other words, this is not an issue on top-level text
// fields such as the `title` of a collection post. However, it becomes an
// issue on fields nested within other components, namely widgets nested
// within a `markdown` widget. For example, the alt text on a block image
// within markdown.
// SEE: https://github.com/netlify/netlify-cms/issues/4539
// SEE: https://github.com/netlify/netlify-cms/issues/3578
componentDidUpdate() {
if (this._el && this._el.selectionStart !== this._sel) {
this._el.setSelectionRange(this._sel, this._sel);
}
}

handleChange = e => {
this._sel = e.target.selectionStart;
this.props.onChange(e.target.value);
};

render() {
const {
forID,
Expand All @@ -27,11 +52,14 @@ export default class StringControl extends React.Component {

return (
<input
ref={el => {
this._el = el;
}}
type="text"
id={forID}
className={classNameWrapper}
value={value || ''}
onChange={e => onChange(e.target.value)}
onChange={this.handleChange}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
/>
Expand Down

0 comments on commit 27657b6

Please sign in to comment.