1、工具类
export function insertAfterText(textDomId: string, input: string) {
const textDom: any = document.getElementById(textDomId);
if (
textDom.selectionStart ||
textDom.selectionStart === '0' ||
textDom.selectionStart === 0
) {
const startPos = textDom.selectionStart;
const endPos = textDom.selectionEnd;
const { scrollTop, value } = textDom;
textDom.value = `${value.substring(0, startPos)}${input}${value.substring(
endPos,
value.length
)}`;
textDom.focus();
textDom.selectionStart = startPos + input.length;
textDom.selectionEnd = startPos + input.length;
textDom.scrollTop = scrollTop;
} else {
textDom.value += input;
textDom.focus();
}
return textDom.value;
}
2、使用方式
两个参数,第一个为id,第二个为在焦点后添加的内容
var insteadText = insertAfterText("codeRuleExpression", selectedRowsState[0].functionName)
form.setFieldsValue({
"codeRuleExpression": insteadText
})
actionRef.current?.clearSelected()
setCodeFunctionModalVisable(false)