silverstripe-frameworktest/client/src/legacy/TestReactFormBuilder.js

81 lines
2.2 KiB
JavaScript
Raw Normal View History

/* global window */
2017-08-16 05:37:11 +02:00
import jQuery from 'jquery';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
2017-08-15 02:10:31 +02:00
import { provideInjector } from 'lib/Injector';
2017-08-16 05:37:11 +02:00
import FormBuilderLoader from 'containers/FormBuilderLoader/FormBuilderLoader';
2017-04-20 03:14:45 +02:00
const sectionConfigKey = 'TestReactFormBuilder';
2017-08-15 02:10:31 +02:00
const InjectedFormBuilderLoader = provideInjector(FormBuilderLoader);
2017-08-16 05:37:11 +02:00
jQuery.entwine('ss', ($) => {
2016-09-29 02:39:07 +02:00
/**
* Kick off Test React FormBuilder admin section.
* Uses React to rebuild the list of fields from FrameworkTest's TestPages.
*/
$('.js-injector-boot .TestReactFormBuilder').entwine({
2016-09-29 02:39:07 +02:00
onmatch() {
this._renderForm();
2016-09-29 02:39:07 +02:00
},
2016-09-29 02:39:07 +02:00
onunmatch() {
this._clearForm();
},
container() {
let container = this.find('.frameworktest-react-container');
if (!container.length) {
container = $('<div class="frameworktest-react-container panel panel--padded" style="overflow-y: auto; max-height: 100%;"></div>');
this.append(container);
}
container.siblings('form').hide();
return container[0];
2016-09-29 02:39:07 +02:00
},
2016-09-29 02:39:07 +02:00
_renderForm() {
const store = window.ss.store;
const sectionConfig = store.getState()
2017-04-20 03:14:45 +02:00
.config.sections.find((section) => section.name === sectionConfigKey);
2016-09-29 02:39:07 +02:00
const schemaUrl = sectionConfig.form.TestEditForm.schemaUrl;
2016-09-29 02:39:07 +02:00
ReactDOM.render(
<Provider store={store}>
2017-08-15 02:10:31 +02:00
<InjectedFormBuilderLoader
2016-09-29 02:39:07 +02:00
schemaUrl={schemaUrl}
handleSubmit={(...args) => this._handleSubmit(...args)}
2017-08-16 05:37:11 +02:00
identifier="FrameworkTest.ReactSection"
2016-09-29 02:39:07 +02:00
/>
</Provider>,
this.container()
2016-09-29 02:39:07 +02:00
);
},
2016-09-29 02:39:07 +02:00
_clearForm() {
ReactDOM.unmountComponentAtNode(this.container());
2016-09-29 02:39:07 +02:00
// this.empty();
},
2016-09-29 02:39:07 +02:00
_handleSubmit(event, fieldValues, submitFn) {
event.preventDefault();
// eslint-disable-next-line no-console
console.log(fieldValues);
2016-09-29 02:39:07 +02:00
return submitFn();
},
2016-09-29 02:39:07 +02:00
});
2016-09-29 02:39:07 +02:00
$('.TestReactFormBuilder .nav-link').entwine({
2017-08-16 05:37:11 +02:00
onclick(e) {
2016-09-29 02:39:07 +02:00
// this is required because the React version of e.preventDefault() doesn't work
// this is to stop React Tabs from navigating the page
e.preventDefault();
2017-08-16 05:37:11 +02:00
},
2016-09-29 02:39:07 +02:00
});
});