Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use semantic-ui-react with redux-form?

Right now I use ReduxForm's Field component and apply raw Semantic UI classes. But I then came across Semantic UI React, which makes things a lot easier -- you can just use React components that have the semantic ui style.

How would you go about integrating ReduxForm with SemanticUIReact?

For example, I currently have something like:

<Field name="gender" component="select" className="ui fluid dropdown">
  {genderOptions}
</Field>

But then, I would like to connect Semantic UI React components like the one below to redux-form:

<Form.Field control={Select} label='Gender' options={genderOptions} placeholder='Gender' />

! Note Field is from redux-form and Form.Field is from semantic-ui-react

like image 936
nbkhope Avatar asked Feb 03 '17 20:02

nbkhope


People also ask

How does semantic UI Integrate With react?

Option 1: Package Managerx require Semantic UI React >=0.81. 0. The Semantic UI CSS package is automatically synced with the main Semantic UI repository to provide a lightweight CSS only version of Semantic UI. If you are using TypeScript, you don't need to install anything, typings are included with the package.

How do I use material UI with redux?

To run this example locally on your machine clone the redux-form repository, then cd redux-form to change to the repo directory, and run npm install . Then open localhost:3030 in your browser to view the example running locally on your machine.

How does redux handle form?

Redux Form is very easy to use and implement, we just have to wrap our form component with the HOC provided by Redux Form and we are good to go. Applying validation to the form is very easy in Redux Form, we can apply validation to all the fields as well as validations for individual fields.

Should I use semantic UI with react?

Semantic UI is a great framework that helps to build web applications. We use components provided by Semantic UI React to build a React application. With a theming support provided by Semantic UI, we can easily change the style of components.


1 Answers

Create a component like this:

import React, { PropTypes } from 'react'
import { Input } from 'semantic-ui-react'

export default function SemanticReduxFormField ({ input, label, meta: { touched, error, warning }, as: As = Input, ...props }) {
  function handleChange (e, { value }) {
    return input.onChange(value)
  }
  return (
    <div>
      <As {...input} value={input.value} {...props} onChange={handleChange} error={touched && error} />
      {touched && (warning && <span>{warning}</span>)}
    </div>
  )
}

SemanticReduxFormField.propTypes = {
  as: PropTypes.any,
  input: PropTypes.any,
  label: PropTypes.any,
  meta: PropTypes.any
}

Then in your component call your field like this:

import { Form } from 'semantic-ui-react'
import { reduxForm, Field } from 'redux-form'

class MyComponent extends Component {
  render () {
    return (
      <Form>
        <Field component={SemanticUiField} as={Form.Select} name='firstname' multiple options={options} placeholder='Select...' />
      </Form>
    )
  }
}

export default reduxForm({...})(MyComponent)
like image 174
Thomas J. Avatar answered Oct 06 '22 01:10

Thomas J.