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

153
Visualizações
Select only *this* instance of a styled component

I'm designing an input component, with a border on a div wrapping the input, and a label that is a sibling of the input. I want to have the color of the border and the label change when the input is focused.

The problem is: I have two of my component side by side, and when I focus on one, the color of both labels changes! The borders don't have this problem, by the way, just the labels.


const S = {
  Wrapper: styled.div`
    position: relative;
    border: solid 1px ${theme.colors.lightGrey};

    :hover {
      border-color: ${theme.colors.white};
    }

    :focus-within {
      border-color: ${theme.colors.draftedBlue};
    }
  `,
  Input: styled.input` /* omitted */ `,
  InputLabel: styled.label`
    position: absolute;
    top: -8px;
    left: 8px;

    input:focus + & {
      color: ${theme.colors.draftedBlue};
    }
  `,
};

const SDCurvedInput = ({ ...props }) => (
  <S.Wrapper className={props.className}>
    <S.InputLabel htmlFor={props.label}>{props.label}</S.InputLabel>
    <S.InputWrapper>
      <S.Input {...props} onChange={e => props.onChange(e.target.value)} />
    </S.InputWrapper>
  </S.Wrapper>
);

const Inputs = () => {
  const { entryFee, payout, setEntryFee } = useSlip();

  const clearIfZero = () => {
    if (!parseInt(entryFee)) setEntryFee("");
  };
  const resetToZeroIfBlank = () => {
    if (!entryFee) setEntryFee(0);
  };

  return (
    <S.Wrapper>
      <SDCurvedInput
        label="Entry Fee"
        type="number"
        value={entryFee}
        onChange={v => setEntryFee(parseInt(v))}
        onFocus={clearIfZero}
        onBlur={resetToZeroIfBlank}
      />
      <SDCurvedInput label="Payout" value={payout || 0} readOnly />
    </S.Wrapper>
  );
};

Help!

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