react-formio-helper
Version:
Helper library for apps built with react and form.io.
241 lines (211 loc) • 8.42 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _formiojs = require('formiojs');
var _formiojs2 = _interopRequireDefault(_formiojs);
var _formioUtils = require('formio-utils');
var _formioUtils2 = _interopRequireDefault(_formioUtils);
var _reactabular = require('reactabular');
var _Paginator = require('./Paginator');
var _Paginator2 = _interopRequireDefault(_Paginator);
var FormioGrid = (function (_React$Component) {
_inherits(FormioGrid, _React$Component);
function FormioGrid(props) {
_classCallCheck(this, FormioGrid);
_get(Object.getPrototypeOf(FormioGrid.prototype), 'constructor', this).call(this, props);
this.state = {
columns: this.columnsFromForm(props.form),
submissions: this.props.submissions || [],
paginationPage: this.props.paginationPage,
paginationNumPage: this.props.paginationNumPage,
paginationSize: this.props.paginationSize
};
this.onRowClick = this.onRowClick.bind(this);
this.onPageChange = this.onPageChange.bind(this);
this.loadForm = this.loadForm.bind(this);
this.loadSubmissions = this.loadSubmissions.bind(this);
}
_createClass(FormioGrid, [{
key: 'formatCell',
value: function formatCell(value, _ref) {
var column = _ref.column;
if (typeof value !== 'object') {
return value;
}
return '[Object]';
}
}, {
key: 'columnsFromForm',
value: function columnsFromForm(form) {
var _this = this;
var columns = [];
if (form && form.components) {
_formioUtils2['default'].eachComponent(form.components, function (component) {
if (component.input && component.tableView && component.key) {
columns.push({
component: component,
property: 'data.' + component.key,
header: {
label: component.label || component.key,
props: {
style: {
width: 100
}
}
},
cell: {
highlight: true,
format: _this.formatCell
},
visible: true
});
}
});
}
return columns;
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.form !== this.props.form) {
this.setState({
columns: this.columnsFromForm(nextProps.form)
});
}
if (nextProps.submissions !== this.state.submissions) {
this.setState({
submissions: nextProps.submissions
});
}
if (nextProps.paginationPage !== this.state.paginationPage) {
this.setState({
paginationPage: nextProps.paginationPage
});
}
if (nextProps.paginationNumPage !== this.state.paginationNumPage) {
this.setState({
paginationNumPage: nextProps.paginationNumPage
});
}
if (nextProps.paginationSize !== this.state.paginationSize) {
this.setState({
limit: nextProps.paginationSize
});
}
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
if (this.props.src) {
this.formio = new _formiojs2['default'](this.props.src);
this.loadForm();
this.loadSubmissions();
}
}
}, {
key: 'loadForm',
value: function loadForm() {
var _this2 = this;
this.formio.loadForm().then(function (form) {
_this2.setState({
columns: _this2.columnsFromForm(form)
});
});
}
}, {
key: 'loadSubmissions',
value: function loadSubmissions() {
var _this3 = this;
this.formio.loadSubmissions({
params: {
limit: this.state.paginationSize,
skip: (this.state.paginationPage - 1) * this.state.paginationSize
}
}).then(function (submissions) {
_this3.setState({
submissions: submissions,
paginationNumPages: Math.ceil(submissions.serverCount / _this3.state.paginationSize)
});
});
}
}, {
key: 'onRowClick',
value: function onRowClick(row) {
var _this4 = this;
return {
onClick: function onClick() {
if (!_this4.props.buttons && typeof _this4.props.onButtonClick === 'function') {
_this4.props.onButtonClick('row', row._id);
}
}
};
}
}, {
key: 'onPageChange',
value: function onPageChange(page) {
if (typeof this.props.onPageChange === 'function') {
this.props.onPageChange(page);
} else {
this.setState({
paginationPage: page
}, this.loadSubmissions);
}
}
}, {
key: 'render',
value: function render() {
var rows = _reactabular.resolve.resolve({
columns: this.state.columns,
method: _reactabular.resolve.nested
})(this.state.submissions);
return _react2['default'].createElement(
'div',
{ className: 'table-responsive' },
_react2['default'].createElement(
_reactabular.Table.Provider,
{
className: 'table table-striped table-bordered table-hover',
columns: this.state.columns
},
_react2['default'].createElement(_reactabular.Table.Header, null),
_react2['default'].createElement(_reactabular.Table.Body, {
rows: rows,
rowKey: '_id',
onRow: this.onRowClick
})
),
_react2['default'].createElement(
'div',
{ className: 'controls' },
_react2['default'].createElement(_Paginator2['default'], {
paginationPage: this.state.paginationPage,
paginationNumPages: this.state.paginationNumPages,
onSelect: this.onPageChange
})
)
);
}
}]);
return FormioGrid;
})(_react2['default'].Component);
FormioGrid.defaultProps = {
form: {},
submissions: [],
paginationPage: 1,
paginationNumPages: 1,
paginationSizes: [25, 50, 75],
paginationSize: 25
};
FormioGrid.propTypes = {
src: _react2['default'].PropTypes.string
};
exports.FormioGrid = FormioGrid;
/*<SearchColumns query={query} columns={columns} onChange={onSearch} />*/