Joost Döbken
Joost Döbken

Reputation: 4007

Autoform with Meteor React and Simple-Schema

Is there any possibility to make meteor-autoform work with meteor-collection2-core and react-meteor?

MWE

Preferably I would like to have something like this.

./imports/api/Books.js

import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
const Books = new Mongo.Collection("books");
Books.attachSchema(new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 200
    },
        author: {
        type: String,
        label: "Author"
    },
}));
if (Meteor.isServer) {
    Meteor.publish('allBooks', function () {
        return Books.find({}, );
    });
};
export default Books;

./imports/client/NewBooks.js

import React, { Component, PropTypes } from 'react';
import { createContainer } from 'meteor/react-meteor-data';
import { quickForm } from 'meteor-autoform';
import Books from '../api/Books';
class NewBooks extends Component {
  constructor(props) {
    super(props)
    this.state = {}
  }

  render() {
    return (
      <div className="container">
        <quickForm
            collection={Books}
            id="insertBookForm"
            type="insert">
        </quickForm>
      </div>
    )
  }
};
export default createContainer(() => {
  Meteor.subscribe('allBooks');
  return {
    books: Books.find().fetch()
  }
}, NewBooks);

Upvotes: 3

Views: 1652

Answers (2)

Joost D&#246;bken
Joost D&#246;bken

Reputation: 4007

The npm package Uniforms worked super easy with Bootstrap.

Addition to ./imports/client/NewBooks.js

import AutoForm from 'uniforms-unstyled/AutoForm';
...
<AutoForm
  schema={Books._collection.simpleSchema()}
  onSubmit={doc => console.log(doc)}
/>

Upvotes: 4

Julian K
Julian K

Reputation: 2001

To my knowledge, Autoform depends heavily on Blaze, so, you could either use blaze autoform components in react (see here), or you can use a different library for this. I used this in a recent project: github.com/nicolaslopezj/simple-react-form. It's powerful, but much more 'hands-on' than the magical Autoform (you have to write your own form and field components).

Upvotes: 2

Related Questions