Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
component input with variable tags and text

I need an input component where I can insert text and when I enter a special character, a window appears with a choice of variables and it will be inserted as a tag, and this variable tag can be inserted anywhere in the text. As far as I understand, this should be done with contentEditable.

all I could do is such an input, where I put the $ symbol only at the end, and not a tag is added, but text: enter image description here

I found many examples where there is just adding tags (https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/), but I didn’t find such a component where tags are mixed with text, can anyone see this?

import { useState } from 'react'

import classNames from 'classnames'
import ContentEditable from 'react-contenteditable'

import { useAppSelector } from '../../app/hooks'
import { selectVariables } from '../../redux/reducers/variables'

import styles from '../../stylesheets/components/common/InputVariable.module.scss'

export const InputVariable = ({ className, label, onChange, value }: any) => {
  const { variables } = useAppSelector(selectVariables)
  const [symbol, symbolSet] = useState(false)

  const handleChange = (e: any) => {
    const str = e.target.value

    onChange(str)

    if (str.charAt(str.length - 1) === '$') {
      symbolSet(true)
    } else {
      symbolSet(false)
    }
  }

  const handleClickVariable = (variable: any) => {
    const newStr = `${value.substring(0, value.length - 1)}{{${variable.id}}}`

    onChange(newStr)
    symbolSet(false)
  }

  const onPaste = (e: any) => {
    e.preventDefault()
    const text = e.clipboardData.getData('text/plain')

    console.log(text)
  }

  return (
    <div className={classNames(styles.inputVariable, className)}>
      <span className={styles.label}>{label}</span>
      <ContentEditable
        className={styles.root}
        html={value}
        disabled={false}
        onChange={handleChange}
        onPaste={onPaste}
      />
      {symbol && (
        <div className={styles.variables}>
          {variables &&
            variables.map((variable: any) => (
              <div key={variable.id} onClick={() => handleClickVariable(variable)} className={styles.variable}>
                {variable.name}
              </div>
            ))}
        </div>
      )}
    </div>
  )
}

export default InputVariable
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda