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

135
Visualizações
How to attach close function on dyanmic children button?

I have a Tooltip component which is a wrapper for dynamic content.

I am trying to use it as a popup, which will have Delete and Cancel buttons.

I am passing the delete and cancel button as children prop, the problem is cus Close and Open state is in the Tooltip component.

I need to attach the close function on Cancel button which lives in Children prop.

Need help to find an elegant solution to this.

Tooltip component:


export const Tooltip: FC<TooltipProps> = ({
  content,
  helperText,
  children,
  ...props
}) => {
  const [visible, setVisible] = useState(false);

  const show = () => setVisible(true);
  const hide = () => setVisible(false);

  return (
    <div>
      <Tippy
        content={content}
        visible={visible}
        onClickOutside={hide}
        interactive
        {...props}
      >
        <div onClick={visible ? hide : show}>
        
          // =====>>> **Close button which be in children prop, need to attach hide() function**
          {children}

        </div>
      </Tippy>
    </div>
  );
};

This is Call of Tooltip component and passing buttons as Children:


 

 <Tooltip
     content={
       <div className="popover-buttons">
          
          // Need to attach here **hide()** function from Tooltip coomponent
          <button>
              Cancel
          </button>
          
           <button>
              Delete
           </button>
        </div>
 </Tooltip>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can make the content prop passed to Tippy a component that has hide() function passed to it as a prop

export const Tooltip: FC<TooltipProps> = ({
  content: Content,
  helperText,
  children,
  ...props
}) => {
  const [visible, setVisible] = useState(false);

  const show = () => setVisible(true);
  const hide = () => setVisible(false);

  return (
    <div>
      <Tippy
        content={<Content hide={hide} />}
        visible={visible}
        onClickOutside={hide}
        interactive
        {...props}
      >
        <div onClick={visible ? hide : show}>
        
          // =====>>> **Close button which be in children prop, need to attach hide() function**
          {children}

        </div>
      </Tippy>
    </div>
  );
};

Then you have:

 <Tooltip
     content={ ({ hide }) =>
       <div className="popover-buttons">
          
          // Need to attach here **hide()** function from Tooltip coomponent
          <button onClick={hide}>
              Cancel
          </button>
          
           <button>
              Delete
           </button>
        </div>
 </Tooltip>
about 4 years ago · Juan Pablo Isaza Relatório
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