-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from creative-commoners/pulls/master/reactify…
…-tags Update module to use react-select
- Loading branch information
Showing
20 changed files
with
8,914 additions
and
55 deletions.
There are no files selected for viewing
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 @@ | ||
module.exports = require('@silverstripe/eslint-config/.eslintrc'); |
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,3 +1,6 @@ | ||
composer.lock | ||
vendor | ||
framework | ||
node_modules/ | ||
/**/*.js.map | ||
/**/*.css.map |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* global window */ | ||
import registerComponents from 'boot/registerComponents'; | ||
|
||
window.document.addEventListener('DOMContentLoaded', () => { | ||
registerComponents(); | ||
}); |
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,8 @@ | ||
import Injector from 'lib/Injector'; | ||
import TagField from 'components/TagField'; | ||
|
||
export default () => { | ||
Injector.component.registerMany({ | ||
TagField, | ||
}); | ||
}; |
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,2 @@ | ||
require('legacy/entwine/TagField.js'); | ||
require('boot'); |
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 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import Select from 'react-select'; | ||
import fetch from 'isomorphic-fetch'; | ||
import url from 'url'; | ||
|
||
class TagField extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
value: props.value, | ||
}; | ||
|
||
this.onChange = this.onChange.bind(this); | ||
this.getOptions = this.getOptions.bind(this); | ||
} | ||
|
||
onChange(value) { | ||
this.setState({ | ||
value | ||
}); | ||
|
||
if (typeof this.props.onChange === 'function') { | ||
this.props.onChange(value); | ||
} | ||
} | ||
|
||
getOptions(input) { | ||
const { lazyLoad, options, optionUrl, labelKey, valueKey } = this.props; | ||
|
||
if (!lazyLoad) { | ||
return Promise.resolve({ options }); | ||
} | ||
|
||
if (!input) { | ||
return Promise.resolve({ options: [] }); | ||
} | ||
|
||
const fetchURL = url.parse(optionUrl, true); | ||
fetchURL.query.term = input; | ||
|
||
return fetch(url.format(fetchURL), { credentials: 'same-origin' }) | ||
.then((response) => response.json()) | ||
.then((json) => ({ | ||
options: json.items.map(item => ({ | ||
[labelKey]: item.id, | ||
[valueKey]: item.text, | ||
})) | ||
})); | ||
} | ||
|
||
render() { | ||
const { | ||
lazyLoad, | ||
options, | ||
creatable, | ||
...passThroughAttributes | ||
} = this.props; | ||
|
||
const optionAttributes = lazyLoad | ||
? { loadOptions: this.getOptions } | ||
: { options }; | ||
|
||
let SelectComponent = Select; | ||
if (lazyLoad && creatable) { | ||
SelectComponent = Select.AsyncCreatable; | ||
} else if (lazyLoad) { | ||
SelectComponent = Select.Async; | ||
} else if (creatable) { | ||
SelectComponent = Select.Creatable; | ||
} | ||
|
||
passThroughAttributes.value = this.state.value; | ||
|
||
return ( | ||
<SelectComponent | ||
{...passThroughAttributes} | ||
onChange={this.onChange} | ||
inputProps={{ className: 'no-change-track' }} | ||
{...optionAttributes} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
TagField.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
labelKey: PropTypes.string.isRequired, | ||
valueKey: PropTypes.string.isRequired, | ||
lazyLoad: PropTypes.bool.isRequired, | ||
creatable: PropTypes.bool.isRequired, | ||
multi: PropTypes.bool.isRequired, | ||
disabled: PropTypes.bool, | ||
options: PropTypes.arrayOf(PropTypes.object), | ||
optionUrl: PropTypes.string, | ||
value: PropTypes.any, | ||
onChange: PropTypes.func, | ||
onBlur: PropTypes.func, | ||
}; | ||
|
||
TagField.defaultProps = { | ||
labelKey: 'Title', | ||
valueKey: 'Value', | ||
disabled: false | ||
}; | ||
|
||
export default TagField; |
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,5 @@ | ||
@import "~react-select/dist/react-select.css"; | ||
|
||
.ss-tag-field .Select--multi .Select-value { | ||
margin-top: 3px; | ||
} |
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,81 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* global jest, describe, beforeEach, it, expect, setTimeout, document */ | ||
|
||
jest.mock('isomorphic-fetch'); | ||
|
||
import React from 'react'; | ||
import Enzyme, { shallow } from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-15.4'; | ||
import TagField from '../TagField'; | ||
import Select from 'react-select'; | ||
import fetch from 'isomorphic-fetch'; | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
describe('TagField', () => { | ||
let props; | ||
|
||
beforeEach(() => { | ||
props = { | ||
name: 'Test', | ||
labelKey: 'label', | ||
valueKey: 'value', | ||
lazyLoad: false, | ||
creatable: false, | ||
multi: true, | ||
}; | ||
}); | ||
|
||
describe('should render a Select component with type', () => { | ||
it('Select', () => { | ||
const wrapper = shallow( | ||
<TagField {...props} /> | ||
); | ||
expect(wrapper.find(Select).length).toBe(1); | ||
}); | ||
it('Select.Creatable with creatable option', () => { | ||
props.creatable = true; | ||
const wrapper = shallow( | ||
<TagField {...props} /> | ||
); | ||
expect(wrapper.find(Select.Creatable).length).toBe(1); | ||
}); | ||
it('Select.Async with lazyLoad option', () => { | ||
props.lazyLoad = true; | ||
const wrapper = shallow( | ||
<TagField {...props} /> | ||
); | ||
expect(wrapper.find(Select.Async).length).toBe(1); | ||
}); | ||
it('Select.AsyncCreatable with both creatable and lazyLoad options', () => { | ||
props.creatable = true; | ||
props.lazyLoad = true; | ||
const wrapper = shallow( | ||
<TagField {...props} /> | ||
); | ||
expect(wrapper.find(Select.AsyncCreatable).length).toBe(1); | ||
}); | ||
}); | ||
|
||
describe('with lazyLoad on and given a URL', () => { | ||
let wrapper; | ||
|
||
beforeEach(() => { | ||
props.lazyLoad = true; | ||
props.optionUrl = 'localhost/some-fetch-url'; | ||
|
||
wrapper = shallow( | ||
<TagField {...props} /> | ||
); | ||
|
||
fetch.mockImplementation(() => Promise.resolve({ | ||
json: () => ({}), | ||
})); | ||
}); | ||
|
||
it('should fetch the URL for results', () => { | ||
wrapper.instance().getOptions('a'); | ||
expect(fetch).toBeCalledWith('localhost/some-fetch-url?term=a', expect.anything()); | ||
}); | ||
}); | ||
}); |
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,33 @@ | ||
/* global window */ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { loadComponent } from 'lib/Injector'; | ||
|
||
window.jQuery.entwine('ss', ($) => { | ||
$('.js-injector-boot .ss-tag-field').entwine({ | ||
onmatch() { | ||
const cmsContent = this.closest('.cms-content').attr('id'); | ||
const context = (cmsContent) | ||
? { context: cmsContent } | ||
: {}; | ||
const TagField = loadComponent('TagField', context); | ||
const dataSchema = { | ||
...this.data('schema'), | ||
onBlur: () => { | ||
this.parents('.cms-edit-form:first').trigger('change'); | ||
} | ||
}; | ||
|
||
ReactDOM.render( | ||
<TagField | ||
{...dataSchema} | ||
/>, | ||
this[0] | ||
); | ||
}, | ||
|
||
onunmatch() { | ||
ReactDOM.unmountComponentAtNode(this[0]); | ||
} | ||
}); | ||
}); |
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,5 @@ | ||
// Import core variables | ||
@import "variables"; | ||
|
||
// Components | ||
@import '../components/TagField'; |
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
Oops, something went wrong.