Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!