多行文本指定位置添加内容

This commit is contained in:
liyongshun 2022-05-06 12:25:32 +08:00
parent b573e6edf6
commit d03b16dd76
1 changed files with 26 additions and 3 deletions

View File

@ -127,6 +127,30 @@ export default class FormalFormModal extends React.Component {
})
}
/**
* name: 获取文本框光标位置
* param {*} obj
* param {*} str
* return {*}
*/
insertText = (obj,str) => {
if (document.selection) {
let sel = document.selection.createRange();
sel.text = str;
} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
let startPos = obj.selectionStart,
endPos = obj.selectionEnd,
cursorPos = startPos,
tmpStr = obj.value;
obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
cursorPos += str.length;
obj.selectionStart = obj.selectionEnd = cursorPos;
obj.focus();
} else {
obj.value += str;
}
}
// 字段点击回调
handleFieldClick(item) {
this.field = item;
@ -141,9 +165,8 @@ export default class FormalFormModal extends React.Component {
orderIndex: this.parameters.length
}
this.parameters.push(parameterItem)
this.setState({
value: this.state.value + fieldName
})
let propsTextarea = this.contentProps.refs.textareaNormal.refs.input.refs.input; // 获取dom节点实例
let position = this.insertText(propsTextarea,fieldName); // 光标的位置
}
render() {