Skip to content

Styled input loose focus on change #540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
const-g opened this issue Mar 1, 2017 · 3 comments
Closed

Styled input loose focus on change #540

const-g opened this issue Mar 1, 2017 · 3 comments

Comments

@const-g
Copy link

const-g commented Mar 1, 2017

Version

1.4.3

Steps to reproduce

  1. Create Form component.
  2. Within it, call a TextField Component, which is an inpunt within a div
  3. Style the div around the input component with
    MaterialFormGroup = styled.div` ... `;

Expected Behavior

the input only is refreshed on typing (using onChange)

Actual Behavior

When looking at the real-DOM, the form is refreshed, and so, the input loose focus at each key press

@k15a
Copy link
Member

k15a commented Mar 1, 2017

Could you post a more detailed reproduction please?

@theocar
Copy link

theocar commented Mar 2, 2017

Hi there, i have the same issue.
This is a Pure React Component with a controlled styled input.
For each keystroke, focus is lost...

import React from 'react';
import styled from 'styled-components';

import Button from './Button';

export default function SearchBar(props) {

  const CustomInput = styled.input`
    font-size: 1em;
    text-align: left;
    border: 1px solid #EFEFEF;
    padding: 0.5em;
    width: 100%;
    border-radius: 10px;
  `;

  const SearchBarWrapper = styled.div`
    display: flex;
    align-items: stretch;
    width: 280px;
    animation: ${props.animation}
  `

  const PrimaryColumn = styled.div`
    flex: 2
  `
  const SecondaryColumn = styled.div`
    flex: 1
  `

  return (
    <SearchBarWrapper>
      <PrimaryColumn>
        <CustomInput
          type="text"
          placeholder="City"
          onChange={props.onChange}
          value={props.value}
        />
      </PrimaryColumn>
      <SecondaryColumn>
        <Button label="GO" primary/>
      </SecondaryColumn>
    </SearchBarWrapper>
);
}

bug-styled-input

@k15a
Copy link
Member

k15a commented Mar 2, 2017

@theocar If you move the styled components out of the SearchBar Component it should work.

import React from 'react';
import styled from 'styled-components';

import Button from './Button';


const CustomInput = styled.input`
    font-size: 1em;
    text-align: left;
    border: 1px solid #EFEFEF;
    padding: 0.5em;
    width: 100%;
    border-radius: 10px;
`;

const SearchBarWrapper = styled.div`
    display: flex;
    align-items: stretch;
    width: 280px;
    animation: ${props.animation}
`

const PrimaryColumn = styled.div`
    flex: 2
`
const SecondaryColumn = styled.div`
    flex: 1
`


export default function SearchBar(props) {
  return (
    <SearchBarWrapper>
      <PrimaryColumn>
        <CustomInput
          type="text"
          placeholder="City"
          onChange={props.onChange}
          value={props.value}
        />
      </PrimaryColumn>
      <SecondaryColumn>
        <Button label="GO" primary/>
      </SecondaryColumn>
    </SearchBarWrapper>
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants