Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main id="app"></main>
              
            
!

CSS

              
                html, body {
  height: 100%;
}
  
body {
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: Helvetica Neue;
}

.container {
  width: 500px;
}

.form-control.active {
  box-shadow: 0 0 5px 1px green;
  
  &.invalid {
    box-shadow: 0 0 5px 1px red;
  }
}

.errors {
  color: red;
  font-size: 12px;
  margin: 5px;
  
  > span {
    display: block;
  }
}

.note {
  color: gray;
  margin: 8px 0;
}
              
            
!

JS

              
                const { Component } = React;
const { Provider, connect } = ReactRedux;
const {
  createStore,
  applyMiddleware
} = Redux;
const {
  Control,
  Errors,
  Form,
  combineForms,
  actions
} = ReactReduxForm;
const thunk = ReduxThunk.default;

const initialUserState = {
  username: 'jasonmalfatto@moduscreate.com',
  password: '',
  passwordConfirm: ''
};

const store = createStore(combineForms({
  user: initialUserState,
}), applyMiddleware(thunk, reduxLogger()));

class UserForm extends Component {
  constructor(props) {  
    super(props);
    
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  
  handleSubmit() {
    const { dispatch } = store;
    const user = store.getState().user;
        
    console.log(user);
  }
  
  render() {
    const getUserClassName = field => {
      const userForm = store.getState().forms.user;
      const isTouched = userForm[field].touched;
      const isValid = userForm[field].valid;
            
      return `form-control${ isTouched || this.props.submitFailed ? ' active' : '' }${ !isValid ? ' invalid' : '' }`;
    };
    
    const MyTextInput = props => {
      const [, name] = props.name.split('.');
      const className = getUserClassName(name);
      
      return <input
               className={className}
               autoComplete="off"
               {...props} />;
    };
    
    const showErrors = field => {      
      const form = store.getState().forms.user.$form;
            
      return !field.pristine || form.submitFailed;
    };
    
    return <Form model="user">
        <div className="form-group">
          <label>Username</label>
          <Control.text 
            model=".username"
            component={MyTextInput}
            validators={{
              required: val => val && val.length,
              validEmail: validator.isEmail
            }}
          />
          <Errors
            className="errors"
            model="user.username"
            show={showErrors}
            messages={{
              required: 'Username is required',
              validEmail: 'Username should be a valid email address',
            }}
          />
        </div>
        <div className="form-group">
          <label>Password</label>
          <Control.text 
            model=".password"
            component={MyTextInput}
            validators={{
              required: val => val && val.length,
              length: val => val.length == 0 || val.length > 4
            }}
          />
          <Errors
            className="errors"
            model="user.password"
            show={showErrors}
            messages={{
              required: 'Password is required',
              length: 'Password should be longer than 4 chars',
            }}
          />
        </div>
        <div className="form-group">
          <label>Confirm Password</label>
          <Control.text 
            model=".passwordConfirm"
            component={MyTextInput}
            validators={{
              required: val => val && val.length,
              matches: val => val === store.getState().user.password
            }}
          />
          <Errors
            className="errors"
            model="user.passwordConfirm"
            show={showErrors}
            messages={{
              required: 'Confirm Password is required',
              matches: 'Passwords do not match'
            }}
          />
        </div>
        <button onClick={this.handleSubmit} className="btn btn-primary">
          Submit
        </button>
    </Form>;
  }
}

const BasicForm = connect(state => {
  const { submitFailed } = state.forms.user.$form;

  return { submitFailed };
})(UserForm);

class App extends React.Component {   
  render() {
    return (
      <div className="container">
        <Provider store={store}>
          <BasicForm />
        </Provider>
        <p className="note">Note: see browser console for redux logging</p>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('app'));
              
            
!
999px

Console