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

76 lines
2.0 KiB
JavaScript
Raw Normal View History

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.
*/
$('.TestReactFormBuilder').entwine({
onmatch() {
setTimeout(() => this._renderForm(), 100);
// need to force scrollable .css() doesn't work with important which is needed for this case
this[0].style.setProperty('overflow-y', 'scroll', 'important');
},
2016-09-29 02:39:07 +02:00
onunmatch() {
this._clearForm();
this.css('overflow-y', false);
},
2016-09-29 02:39:07 +02:00
open() {
this._renderForm();
},
2016-09-29 02:39:07 +02:00
close() {
this._clearForm();
},
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
identifier="TestReactForm"
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[0]
);
},
2016-09-29 02:39:07 +02:00
_clearForm() {
ReactDOM.unmountComponentAtNode(this[0]);
// this.empty();
},
2016-09-29 02:39:07 +02:00
_handleSubmit(event, fieldValues, submitFn) {
event.preventDefault();
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
});
});