Coder Social home page Coder Social logo

semantic-ui-redux-form-fields's Introduction

Semantic UI React Redux Form Fields

npm npm wercker status David

This React component library is designed to help you easily integrate Semantic UI React with Redux Form. The components come all pre-hooked-up so you don't have to worry about error reporting or the presentation logic since everything works right out of the box. Included is the big five form components Checkbox Dropdown, Input, Radio, and TextArea as well as fieldEnhance a higher order component that gives you the flexibility to extend and customize.

Install

Package Manager

# yarn
yarn add semantic-ui-redux-form-fields
# npm
npm install semantic-ui-redux-form-fields

Github

In the dist/ directory contains a pre-build Node, Browser, and Browser minified version. However, I strongly recommend you use yarn or npm.

Prerequisites

  1. Install and Config Redux
  2. Install and Config Redux Form
  3. Install and Config React Semantic UI
    • The assumption is you are using Semantic UI. And while you can "technically" use these components as standalones it would not make much sense. Furthermore, the Semantic UI css styles are not included in this package to allow for easier style customizations with Semantic themes and such.

Props

  • All the default Semantic UI props for each component can be passed just like you would expect.
  • currentValue → Is the value for all components. It's critically important that you use currentValue and not value otherwise the component will not work.
  • topLabel → A Semantic UI label that's positioned above the field (top-right).
  • Events → In all likelihood you'll need to implement custom events, and while you can use a non-append prop like onChange you won't have access to the redux-form input methods. However, by using these {*}Custom appended props input is passed as the first argument.
    • onBlurCustom(input, event, newValue, previousValue)
    • onChangeCustom(input, event, newValue, previousValue)
    • onDragStartCustom(input, event)
    • onDropCustom(input, event, newValue, previousValue)
    • onFocusCustom(input, event)

Imports

import {
  Checkbox,
  Dropdown,
  fieldEnhance,
  Input,
  Radio,
  TextArea,
} from 'semantic-ui-redux-form-fields';

Field HOC

All the fields are created through the fieldEnhance higher order component which mentioned above gives you the ability to extend and customize. t handles the Form.Field presentational logic which includes error reporting and a top label. For example, it allows you to easily hook-up and use React TimePicker. Check out __tests__/fieldEnhance.HOC.spec.js for an example how to use fieldEnhance with React TimePicker.

Field Examples

Here's a few examples, and need be there's more examples in __tests__. Also check out the __tests__/fieldEnhance.HOC.spec.js

Checkbox

import React from 'react';
import { Field, reduxForm } from 'redux-form';
import { Checkbox } from 'semantic-ui-redux-form-fields';

const SemanticFormCheckbox = (props) => {
  const { currentValue, handleSubmit, pristine, submitting } = props;
  return (
    <form onSubmit={handleSubmit}>
      <Field
        component={Checkbox}
        currentValue={currentValue}
        name='my-Checkbox'
        placeholder='My Checkbox'
        required={true}
        topLabel='My Checkbox'
      />

      <div>
        <button type='submit' disabled={pristine || submitting}>
          Submit
        </button>
      </div>
    </form>
  );
};

export default reduxForm({
  // a unique identifier for this form
  form: 'Semantic_Checkbox_Form'
})(SemanticFormCheckbox);

Input

import React from 'react';
import { Field, reduxForm } from 'redux-form';
import { Input } from 'semantic-ui-redux-form-fields';


const SemanticFormInput = (props) => {
  const { currentValue, handleSubmit, pristine, submitting } = props;
  return (
    <form onSubmit={handleSubmit}>
      <Field
        component={Input}
        currentValue={currentValue}
        name='my-Input'
        placeholder='My Input'
        required={true}
        topLabel='My Input'
      />

      <div>
        <button type='submit' disabled={pristine || submitting}>
          Submit
        </button>
      </div>
    </form>
  );
};

export default reduxForm({
  // a unique identifier for this form
  form: 'Semantic_Input_Form'
})(SemanticFormInput);

Dropdown

import React from 'react';
import { Field, reduxForm } from 'redux-form';
import { Dropdown } from 'semantic-ui-redux-form-fields';

const checkboxOptions = [{
  value: 'one', text: 'one', key: 'one',
}, {
  value: 'two', text: 'two', key: 'two',
}, {
  value: 'three', text: 'three', key: 'three',
}];

const SemanticFormDropdown = (props) => {
  const { currentValue, handleSubmit, pristine, submitting } = props;
  return (
    <form onSubmit={handleSubmit}>
      <Field
        component={Dropdown}
        currentValue={currentValue}
        name='my-Dropdown'
        options={checkboxOptions}
        placeholder='My Dropdown'
        required={true}
        topLabel='My Dropdown'
      />

      <div>
        <button type='submit' disabled={pristine || submitting}>
          Submit
        </button>
      </div>
    </form>
  );
};

export default reduxForm({
  // a unique identifier for this form
  form: 'Semantic_Dropdown_Form'
})(SemanticFormDropdown);

Best, te

semantic-ui-redux-form-fields's People

Contributors

artisin avatar

Stargazers

Roman avatar Georg Bez avatar

Watchers

James Cloos avatar  avatar

Forkers

andrewsantarin

semantic-ui-redux-form-fields's Issues

width prop is lost

I am trying to create a form with a bit of simple layout, like this:

<Form.Group>
  <Field
    component={Dropdown}
    name="location"
    topLabel="Location"
    options={LOCATION_OPTIONS}
    width={6}
  />
  <Field
    component={Dropdown}
    name="status"
    topLabel="Status"
    options={STATUS_OPTIONS}
    width={6}
  />
</Form.Group>

When looking at the rendered DOM the width information gets lost. The React devtools show that width is passed to fieldEnhance(DropdownPure), but that does not pass it on to the Form.Field

Could we not use the package's inline styling, please?

Hi, @artisin. I've tried semantic-ui-redux-form-fields and so far, and it has been a great little utility belt, but I have difficulties working with the inline styling.

In some cases, I do not want formFieldStyles and the inline styling from ErrorMsg to take effect. I'd rather have those inline styles kept as a custom CSS somewhere in this repo, and then we decide whether we want to use it or not.

I could say creating an overriding CSS will help work, but I'm afraid I'll override other CSS classes, too, which means I'll be fighting the inline CSS all the time. Or I could try to do something like removing the inline style as soon as the component mounts:

Input.jsx

import React, { Component } from 'react';
import { Input as InputComponent } from 'semantic-ui-react';
import { fieldEnhance } from 'semantic-ui-redux-form-fields';

export class Input extends Component {
  componentDidMount() {
    /**
     * @HACK: I had to! The parent <div /> element can't be accessed by other means!
     */
    document.getElementById(this.props.input.name)
      .closest('.Semantic-redux-form-field')
      .removeAttribute("style");
  }

  render() {
    const {
      input: {
        onDragStart,
        onDrop,
        ...input
      },

      ...props
    } = this.props;

    return (
      <InputComponent
        {...input}
        {...props}
        /**
         * NOTE: What if I don't want to have an `id` for this element?
         */
        id={input.name}
      />
    );
  }
}

// Decorate <Input /> with semantic-ui-redux-form-fields's fieldEnhance(ComposedComponent).
export default fieldEnhance(Input);

But really, why would I want to do that?

Warning: Failed prop type: Invalid prop `label` of type `object` supplied to `fieldEnhance(pure(InputPure)`, expected `string`.

Environment:
react 15.6.2
redux 3.7.2
semantic-ui-react 0.74.2
semantic-ui-redux-form-fields 1.0.0
redux-form 7.1.0

Expected behavior
No warnings and the input renders like here https://react.semantic-ui.com/elements/input#input-example-right-labeled-basic

Actual behavior
I get the correctly rendered component but with the following warning:

Warning: Failed prop type: Invalid prop label of type object supplied to fieldEnhance(pure(InputPure), expected string.
in fieldEnhance(pure(InputPure) (created by Input)
in Input (created by ConnectedField)
in ConnectedField (created by Connect(ConnectedField))
in Connect(ConnectedField) (created by Field)
in Field (at EquipmentFormPart.jsx:35)
in div (at EquipmentFormPart.jsx:34)
in div (at EquipmentFormPart.jsx:33)
in div (at EquipmentFormPart.jsx:32)
in div (at EquipmentFormPart.jsx:14)
in div (at EquipmentFormPart.jsx:13)
in div (at EquipmentFormPart.jsx:11)
in EquipmentFormPart (created by Connect(EquipmentFormPart))
in Connect(EquipmentFormPart) (at NewBuyOrderForm.jsx:24)
in div (at NewBuyOrderForm.jsx:21)
in div (at NewBuyOrderForm.jsx:20)
in div (at NewBuyOrderForm.jsx:16)
in form (at NewBuyOrderForm.jsx:15)
in NewBuyOrderForm (created by Form(NewBuyOrderForm))
in Form(NewBuyOrderForm) (created by Connect(Form(NewBuyOrderForm)))
in Connect(Form(NewBuyOrderForm)) (created by ReduxForm)
in ReduxForm (at NewBuyOrderFormContainer.jsx:22)
in div (at NewBuyOrderFormContainer.jsx:21)
in NewBuyOrderFormContainer (created by Connect(NewBuyOrderFormContainer))
in Connect(NewBuyOrderFormContainer) (at Wizard.jsx:21)
in div (at Tabs.jsx:62)
in div (at Tabs.jsx:49)
in Tabs (at Wizard.jsx:9)
in Unknown (at NewBuyOrderView.jsx:30)
in div (at HeaderPanel.jsx:6)
in Unknown (at NewBuyOrderView.jsx:29)
in div (at NewBuyOrderView.jsx:14)
in NewBuyOrderView (created by Connect(NewBuyOrderView))
in Connect(NewBuyOrderView) (at WorkView.jsx:33)
in div (at WorkView.jsx:29)
in div (at WorkView.jsx:21)
in WorkView (created by Connect(WorkView))
in Connect(WorkView) (at PrivateRoute.jsx:10)
in Route (at PrivateRoute.jsx:6)
in PrivateRoute (created by Connect(PrivateRoute))
in Connect(PrivateRoute) (created by Route)
in Route (created by withRouter(Connect(PrivateRoute)))
in withRouter(Connect(PrivateRoute)) (at HomeView.jsx:28)
in Switch (at HomeView.jsx:21)
in div (at HomeView.jsx:19)
in HomeView (created by Connect(HomeView))
in Connect(HomeView) (at PrivateRoute.jsx:10)
in Route (at PrivateRoute.jsx:6)
in PrivateRoute (created by Connect(PrivateRoute))
in Connect(PrivateRoute) (created by Route)
in Route (created by withRouter(Connect(PrivateRoute)))
in withRouter(Connect(PrivateRoute)) (at App.jsx:19)
in Switch (at App.jsx:17)
in div (at App.jsx:16)
in App (created by Connect(App))
in Connect(App) (created by Route)
in Route (created by withRouter(Connect(App)))
in withRouter(Connect(App)) (at index.js:14)
in div (at index.js:13)
in Router (created by ConnectedRouter)
in ConnectedRouter (at index.js:12)
in Provider (at index.js:11)

Steps
The component is used like this:
<Field component={Input} name="equipment[temperature]" label={{ basic: true, content: 'F' }} labelPosition="right" placeholder="25&deg;" type="number" className="order-form-group__text-input" disabled={!selectedEquipment || !selectedEquipment.hasRefrigerator} />

How to get the selected value for drop down option?

I was able to pass the value to the Dropdown option but cannot get the selected value when in other page. May I know do you know what is the problem with it? Below is the code:

const categoryRouteOptions = [{
  value: '1', text: 'Others', key: '1',
}, {
  value: '2', text: 'Air Con', key: '2',
}];

....

CategoryRouteFormField = props => {
    const { currentValue } = props;
      return(
      <Form.Field>
       <Dropdown selection {...props.input}
                 value={props.input.value}
                 options={categoryRouteOptions}
                 onChange={(param,data) => props.input.onChange(data.value)}
                 placeholder={props.label} 
                 currentValue={currentValue}
        />
      </Form.Field>
      )
  }

....

render() {
return (
<Field name="category_route" component={this.CategoryRouteFormField} label="Category Route"/>
...
)

Any help will be appreciated. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.