-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENHANCEMENT Enable UploadField.js within non-react sections (#331)
* ENHANCEMENT Enable UploadField.js within non-react sections * Implement feedback
- Loading branch information
Showing
16 changed files
with
299 additions
and
70 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require('expose?InsertMediaModal!containers/InsertMediaModal/InsertMediaModal'); | ||
|
||
require('boot'); | ||
require('entwine/UploadField/UploadFieldEntwine.js'); | ||
|
||
require('styles/bundle.scss'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import jQuery from 'jQuery'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Provider } from 'react-redux'; | ||
import { schemaMerge } from 'lib/schemaFieldValues'; | ||
import { ConnectedUploadField } from 'components/UploadField/UploadField'; | ||
|
||
/** | ||
* Shiv for inserting react UploadField into entwine forms | ||
*/ | ||
jQuery.entwine('ss', ($) => { | ||
/** | ||
* See boot/index.js for `.react-boot` bootstrap | ||
*/ | ||
$('.js-react-boot input.entwine-uploadfield').entwine({ | ||
|
||
onunmatch() { | ||
this._super(); | ||
// solves errors given by ReactDOM "no matched root found" error. | ||
ReactDOM.unmountComponentAtNode(this[0]); | ||
}, | ||
|
||
onmatch() { | ||
this._super(); | ||
this.refresh(); | ||
}, | ||
|
||
refresh() { | ||
const store = window.ss.store; | ||
const props = this.getAttributes(); | ||
ReactDOM.render( | ||
<Provider store={store}> | ||
<ConnectedUploadField {...props} /> | ||
</Provider>, | ||
this.parent()[0] | ||
); | ||
}, | ||
|
||
/** | ||
* Find the selected node and get attributes associated to attach the data to the form | ||
* | ||
* @returns {Object} | ||
*/ | ||
getAttributes() { | ||
const state = $(this).data('state'); | ||
const schema = $(this).data('schema'); | ||
return schemaMerge(schema, state); | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<input $AttributesHTML <% include SilverStripe/Forms/AriaAttributes %> /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace SilverStripe\AssetAdmin\Tests\Forms; | ||
|
||
use SilverStripe\AssetAdmin\Controller\AssetAdmin; | ||
use SilverStripe\Assets\File; | ||
use SilverStripe\AssetAdmin\Forms\UploadField; | ||
use SilverStripe\Assets\Image; | ||
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore; | ||
use SilverStripe\Control\Controller; | ||
use SilverStripe\Dev\Debug; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\Form; | ||
use SilverStripe\ORM\ArrayList; | ||
|
||
class UploadFieldTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'FileFormBuilderTest.yml'; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
// Set backend and base url | ||
TestAssetStore::activate('FileFormBuilderTest'); | ||
|
||
/** @var File $testfile */ | ||
$testfile = $this->objFromFixture(File::class, 'file1'); | ||
$testfile->setFromLocalFile(__DIR__ . '/fixtures/testfile.txt', 'files/testfile.txt'); | ||
$testfile->write(); | ||
|
||
/** @var Image $testimage */ | ||
$testimage = $this->objFromFixture(Image::class, 'image1'); | ||
$testimage->setFromLocalFile(__DIR__ . '/fixtures/testimage.png', 'files/testimage.png'); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
TestAssetStore::reset(); | ||
parent::tearDown(); | ||
} | ||
|
||
public function testGetAttributes() | ||
{ | ||
$field = UploadField::create('MyField'); | ||
$field->addExtraClass('myfield'); | ||
$field->setIsMultiUpload(false); | ||
$field->setFolderName('/'); | ||
/** @var Image $image */ | ||
$image = $this->objFromFixture(Image::class, 'image1'); | ||
$field->setItems(new ArrayList([$image])); | ||
Form::create(new Controller(), 'MyForm', FieldList::create($field), FieldList::create()); | ||
$admin = new AssetAdmin(); | ||
|
||
$attributes = $field->getAttributes(); | ||
$schema = [ | ||
'name' => 'MyField', | ||
'id' => 'Form_MyForm_MyField', | ||
'type' => 'Custom', | ||
'component' => 'UploadField', | ||
'holderId' => 'Form_MyForm_MyField_Holder', | ||
'title' => 'My Field', | ||
'source' => null, | ||
'extraClass' => 'entwine-uploadfield uploadfield myfield', | ||
'description' => null, | ||
'rightTitle' => null, | ||
'leftTitle' => null, | ||
'readOnly' => false, | ||
'disabled' => false, | ||
'customValidationMessage' => '', | ||
'validation' => [], | ||
'attributes' => [], | ||
'data' => [ | ||
'createFileEndpoint' => [ | ||
'url' => 'admin/assets/api/createFile', | ||
'method' => 'post', | ||
'payloadFormat' => 'urlencoded', | ||
], | ||
'multi' => false, | ||
'parentid' => 0, | ||
], | ||
]; | ||
$state = [ | ||
'name' => 'MyField', | ||
'id' => 'Form_MyForm_MyField', | ||
'value' => [ 'Files' => [$image->ID] ], | ||
'message' => null, | ||
'data' => [ | ||
'files' => [ $admin->getObjectFromData($image) ], | ||
], | ||
]; | ||
$this->assertArraySubset( | ||
[ | ||
'class' => 'entwine-uploadfield uploadfield myfield', | ||
'type' => 'file', | ||
'multiple' => false, | ||
'id' => 'Form_MyForm_MyField' | ||
], | ||
$attributes | ||
); | ||
|
||
// Check schema / state are encoded in this field | ||
$this->assertEquals($schema, json_decode($attributes['data-schema'], true)); | ||
$this->assertEquals($state, json_decode($attributes['data-state'], true)); | ||
} | ||
} |
File renamed without changes.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters