Revert string control to class component
This commit is contained in:
parent
b3c44a0d89
commit
51c3e0f6b5
@ -2,15 +2,10 @@ import PropTypes from 'prop-types';
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
import { Map, List } from 'immutable';
|
import { Map, List } from 'immutable';
|
||||||
import { oneLine } from 'common-tags';
|
|
||||||
|
|
||||||
import { getRemarkPlugins } from '../../../lib/registry';
|
import { getRemarkPlugins } from '../../../lib/registry';
|
||||||
import ValidationErrorTypes from '../../../constants/validationErrorTypes';
|
import ValidationErrorTypes from '../../../constants/validationErrorTypes';
|
||||||
|
|
||||||
function truthy() {
|
|
||||||
return { error: false };
|
|
||||||
}
|
|
||||||
|
|
||||||
function isEmpty(value) {
|
function isEmpty(value) {
|
||||||
return (
|
return (
|
||||||
value === null ||
|
value === null ||
|
||||||
@ -23,7 +18,7 @@ function isEmpty(value) {
|
|||||||
|
|
||||||
export default class Widget extends Component {
|
export default class Widget extends Component {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
controlComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired,
|
controlComponent: PropTypes.func.isRequired,
|
||||||
validator: PropTypes.func,
|
validator: PropTypes.func,
|
||||||
field: ImmutablePropTypes.map.isRequired,
|
field: ImmutablePropTypes.map.isRequired,
|
||||||
hasActiveStyle: PropTypes.bool,
|
hasActiveStyle: PropTypes.bool,
|
||||||
@ -308,6 +303,7 @@ export default class Widget extends Component {
|
|||||||
onRemoveInsertedMedia,
|
onRemoveInsertedMedia,
|
||||||
getAsset,
|
getAsset,
|
||||||
forID: uniqueFieldId,
|
forID: uniqueFieldId,
|
||||||
|
ref: this.processInnerControlRef,
|
||||||
validate: this.validate,
|
validate: this.validate,
|
||||||
classNameWrapper,
|
classNameWrapper,
|
||||||
classNameWidget,
|
classNameWidget,
|
||||||
|
@ -1,52 +1,50 @@
|
|||||||
import React, { forwardRef, useCallback, useEffect, useState } from 'react';
|
import React from 'react';
|
||||||
import { CmsWidgetControlProps } from '../../interface';
|
|
||||||
|
|
||||||
const StringControl = forwardRef<any, CmsWidgetControlProps<string>>(
|
import type { ChangeEvent } from 'react';
|
||||||
({
|
import type { CmsWidgetControlProps } from '../../interface';
|
||||||
onChange,
|
|
||||||
forID,
|
|
||||||
value = '',
|
|
||||||
classNameWrapper,
|
|
||||||
setActiveStyle,
|
|
||||||
setInactiveStyle,
|
|
||||||
}: CmsWidgetControlProps<string>, _ref) => {
|
|
||||||
const [element, setElement] = useState<HTMLInputElement | null>(null);
|
|
||||||
const [selection, setSelection] = useState<number | null>(null);
|
|
||||||
|
|
||||||
const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
export default class StringControl extends React.Component<CmsWidgetControlProps<string>> {
|
||||||
setSelection(e.target.selectionStart);
|
// The selection to maintain for the input element
|
||||||
onChange(e.target.value);
|
private _sel: number | null = 0;
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleSetElement = useCallback((el: HTMLInputElement) => {
|
// The input element ref
|
||||||
setElement(el);
|
private _el: HTMLInputElement | null = null;
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
// NOTE: This prevents the cursor from jumping to the end of the text for
|
||||||
if (!element) {
|
// nested inputs. In other words, this is not an issue on top-level text
|
||||||
return;
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (element.selectionStart !== selection) {
|
handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
element.setSelectionRange(selection, selection);
|
this._sel = e.target.selectionStart;
|
||||||
}
|
this.props.onChange(e.target.value);
|
||||||
}, [element, element?.selectionStart, selection]);
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { forID, value, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
ref={handleSetElement}
|
ref={el => {
|
||||||
|
this._el = el;
|
||||||
|
}}
|
||||||
type="text"
|
type="text"
|
||||||
id={forID}
|
id={forID}
|
||||||
className={classNameWrapper}
|
className={classNameWrapper}
|
||||||
value={value || ''}
|
value={value || ''}
|
||||||
onChange={handleChange}
|
onChange={this.handleChange}
|
||||||
onFocus={setActiveStyle}
|
onFocus={setActiveStyle}
|
||||||
onBlur={setInactiveStyle}
|
onBlur={setInactiveStyle}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
);
|
}
|
||||||
|
|
||||||
StringControl.displayName = 'StringControl';
|
|
||||||
|
|
||||||
export default StringControl;
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user