antd的Input.TextArea换行并插入文字

1、表单

<Form.Item label={<div><span style={{ color: 'red' }}>*</span>标题</div>}>
          <div style={{ marginBottom: '4px' }}><Button type='primary' onClick={() => this.setState({ wordsVisible: true })}>插入动态词包</Button></div>
          <Form.Item name='title' rules={[{ validator: this.titleCheck }]}>
            <Input.TextArea ref={input => this.contentProps = input} autoSize={{ minRows: 8 }} placeholder='标题长度范围为6-30个字,添加多个标题请换行' />
          </Form.Item>
        </Form.Item>

{
        wordsVisible && <Modal visible={wordsVisible} title='公共词包' onOk={this.insert} onCancel={() => this.setState({ wordsVisible: false })} />
      }

2、插入判断

insert = () => {
    // 获取光标所在位置
    let index = this.contentProps.resizableTextArea.textArea.selectionStart
    const content = this.form.getFieldValue('title')
    const str = '我是插入语'
    const value = content ? content.slice(0, index) + str + content.slice(index) : str
    this.form.setFieldsValue({ title: value })
    this.timer = setTimeout(() => {
      // 光标移动到插入次的后面并进行form校验
      this.contentProps.focus()
      this.contentProps.resizableTextArea.textArea.setSelectionRange(index + str.length, index + str.length)
      this.setState({ wordsVisible: false })
      this.form.validateFields()
    }, 0)
  }

3、表单校验

titleCheck = (_, value) => {
    const arr = value.split('\n')
    return new Promise((resolve, reject) => {
      if (value) {
        const index = arr.findIndex(i => i?.length < 6 || i.length > 30)
        if (arr && index >= 0) {
          return reject(`第${index + 1}行的标题字数需要在6~30字之间`)
        }
        resolve()
      } else {
        reject('请输入标题')
      }
    })
  }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值