Styled-components: input redux-form?

Created on 8 Dec 2016  路  3Comments  路  Source: styled-components/styled-components

Redux-Form styled.input``?

Most helpful comment

I think you want:

import { Field } from 'redux-form'
import styled from 'styled-components'

const Input = styled(Field)`
  /* You CSS */
`

https://github.com/styled-components/styled-components#third-party-components

All 3 comments

Sorry what's the question here?

I think you want:

import { Field } from 'redux-form'
import styled from 'styled-components'

const Input = styled(Field)`
  /* You CSS */
`

https://github.com/styled-components/styled-components#third-party-components

Solution above doesn't cover cases like styling border when error occurs. You need meta object from Field in your styled component props for that. The good way is to set styled component as Field presential component.

const BasicInput = ({ input, ...props }) => <input {...props} {...input}/>;

export const StyledInput = styled(BasicInput)`
  /* another input styles */
  border: 1px solid ${({ meta: { touched, error } }) => (touched && error ? 'red' : 'grey')};
`;

const Input = ({ name, ...props }) => (
  <Field name={name} component={StyledInput} {...props}/>
);

P.S. Do not render StyledInput inside Field component prop. Fat arrow components have not supported since v6 of redux-form.

Was this page helpful?
0 / 5 - 0 ratings