-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update module to use react-select #113
Merged
robbieaverill
merged 10 commits into
silverstripe:master
from
creative-commoners:pulls/master/reactify-tags
Jul 17, 2018
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
77f1096
NEW Converting tagfield to use react components
ScopeyNZ ff3c8eb
Additional changes to convert tagfield to use react-select
ScopeyNZ 70e83c0
Travis fixes
ScopeyNZ cef407d
Adding tests & various feedback updates
ScopeyNZ e67ed8d
Adding NPM tests to travis
ScopeyNZ b60bdab
Simplifying included custom styling - a lot
ScopeyNZ fac6a03
Revert modification of tests to keep B/C
ScopeyNZ 5aed71b
Updating to use injector & removing storybook
ScopeyNZ 3830a74
Updating yarn.lock
ScopeyNZ 2067a3b
Building distribution files
ScopeyNZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
require('legacy/entwine/TagField.js'); |
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,28 @@ | ||
/* global window */ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import TagField from 'components/TagField'; | ||
|
||
window.jQuery.entwine('ss', ($) => { | ||
$('.js-injector-boot .ss-tag-field').entwine({ | ||
onmatch() { | ||
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
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,82 @@ | ||
{ | ||
"name": "silverstripe-tagfield", | ||
"version": "2.2.0", | ||
"description": "Tag field for SilverStripe", | ||
"license": "BSD-3-Clause", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/silverstripe/silverstripe-tagfield.git" | ||
}, | ||
"homepage": "https://github.com/silverstripe/silverstripe-tagfield", | ||
"bugs": { | ||
"url": "https://github.com/silverstripe/silverstripe-tagfield/issues" | ||
}, | ||
"author": "SilverStripe Ltd.", | ||
"engines": { | ||
"node": ">=6.x" | ||
}, | ||
"scripts": { | ||
"build": "yarn && yarn lint && NODE_ENV=production webpack -p --bail --progress", | ||
"dev": "NODE_ENV=development webpack --progress", | ||
"watch": "NODE_ENV=development webpack --watch --progress", | ||
"css": "WEBPACK_CHILD=css npm run build", | ||
"test": "jest", | ||
"coverage": "jest --coverage", | ||
"lock": "npm-shrinkwrap --dev", | ||
"lint": "eslint client/src && sass-lint client/src", | ||
"lint-js": "eslint client/src", | ||
"lint-js-fix": "eslint client/src --fix", | ||
"lint-sass": "sass-lint client/src" | ||
}, | ||
"jest": { | ||
"roots": [ | ||
"client/src" | ||
], | ||
"modulePaths": [ | ||
"client/src", | ||
"../admin/client/src", | ||
"../admin/node_modules", | ||
"vendor/silverstripe/admin/client/src", | ||
"vendor/silverstripe/admin/node_modules" | ||
], | ||
"testMatch": [ | ||
"**/tests/**/*-test.js?(x)" | ||
], | ||
"transform": { | ||
".*": "babel-jest" | ||
} | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"env", | ||
"react" | ||
], | ||
"plugins": [ | ||
"transform-object-rest-spread" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@silverstripe/eslint-config": "^0.0.4", | ||
"@silverstripe/webpack-config": "^0.8.0", | ||
"@storybook/addon-actions": "^3.2.19", | ||
"@storybook/addon-options": "^3.2.19", | ||
"@storybook/react": "^3.2.19", | ||
"babel-jest": "^20.0.3", | ||
"copy-webpack-plugin": "^4.2.0", | ||
"enzyme": "^3.3.0", | ||
"enzyme-adapter-react-15.4": "^1.0.5", | ||
"html-loader": "^0.5.1", | ||
"jest-cli": "^19.0.2", | ||
"webpack": "^2" | ||
}, | ||
"dependencies": { | ||
"babel-polyfill": "6.7.4", | ||
"classnames": "^2.2.5", | ||
"isomorphic-fetch": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz", | ||
"jquery": "^3.1.1", | ||
"react": "15.3.1", | ||
"react-dom": "15.3.1", | ||
"react-select": "^1.0.0-rc.5", | ||
"url": "^0.11.0" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were you going to add a story book definition for it? That'd be cool, but not required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was playing around with that but I didn't get around to it. I can remove that.