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
<Directory /> component refactored #80
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
586c4d8
Make tests code more consistent
isnifer 5de691e
Merge pull request #78 from isnifer/feature/make-tests-code-more-cons…
6c631c9
merged test update
9d2914e
added travis yml
3eb07fd
testing travis
429770c
retesting travis
dc9d1c3
retesting travis
e416738
Merge pull request #83 from JiniHendrix/master
markmarcelo 1f8c659
<Directory /> component refactored
isnifer File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": ["react", "es2015"] | ||
} | ||
"presets": ["react", "es2015", "stage-2"] | ||
} |
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,3 @@ | ||
language: node_js | ||
node_js: | ||
- "6" |
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,76 +1,94 @@ | ||
import React from 'react'; | ||
import React, { Component } from 'react'; | ||
import cx from 'classnames'; | ||
import File from './File.jsx'; | ||
import CreateMenu from './CreateMenu.jsx'; | ||
import CreateForm from './CreateForm.jsx'; | ||
import RenameForm from './RenameForm.jsx'; | ||
|
||
export default class Directory extends React.Component { | ||
constructor() { | ||
super(); | ||
} | ||
export default class Directory extends Component { | ||
handleListItemClick = proxyEvent => ( | ||
this.props.clickHandler( | ||
this.props.id, | ||
this.props.directory.path, | ||
this.props.directory.type, | ||
proxyEvent | ||
) | ||
) | ||
|
||
handlePlusIconClick = proxyEvent => ( | ||
this.props.openCreateMenu( | ||
this.props.id, | ||
this.props.directory.path, | ||
proxyEvent | ||
) | ||
) | ||
|
||
render() { | ||
const arr = []; | ||
let uniqueId; | ||
for (var i = 0; i < this.props.directory.subdirectories.length; i++) { | ||
arr.push( | ||
<Directory | ||
key={this.props.directory.subdirectories[i].id} | ||
id={this.props.directory.subdirectories[i].id} | ||
directory={this.props.directory.subdirectories[i]} | ||
openFile={this.props.openFile} | ||
clickHandler={this.props.clickHandler} | ||
selectedItem={this.props.selectedItem} | ||
openCreateMenu={this.props.openCreateMenu} | ||
openMenuId={this.props.openMenuId} | ||
createMenuInfo={this.props.createMenuInfo} | ||
createForm={this.props.createForm} | ||
createItem={this.props.createItem} | ||
rename={this.props.rename} | ||
renameHandler={this.props.renameHandler} | ||
/>) | ||
} | ||
for (var i = 0; i < this.props.directory.files.length; i++) { | ||
arr.push( | ||
<File | ||
key={this.props.directory.files[i].id} | ||
id={this.props.directory.files[i].id} | ||
file={this.props.directory.files[i]} | ||
openFile={this.props.openFile} | ||
clickHandler={this.props.clickHandler} | ||
selectedItem={this.props.selectedItem} | ||
rename={this.props.rename} | ||
renameHandler={this.props.renameHandler} | ||
/>) | ||
} | ||
let item = ( | ||
<div | ||
className="list-item" | ||
onClick={this.props.clickHandler.bind(null, this.props.id, this.props.directory.path, this.props.directory.type)} | ||
> | ||
const subdirectories = this.props.directory.subdirectories.map(directory => ( | ||
<Directory | ||
key={directory.id} | ||
id={directory.id} | ||
directory={directory} | ||
openFile={this.props.openFile} | ||
clickHandler={this.props.clickHandler} | ||
selectedItem={this.props.selectedItem} | ||
openCreateMenu={this.props.openCreateMenu} | ||
openMenuId={this.props.openMenuId} | ||
createMenuInfo={this.props.createMenuInfo} | ||
createForm={this.props.createForm} | ||
createItem={this.props.createItem} | ||
rename={this.props.rename} | ||
renameHandler={this.props.renameHandler} | ||
/> | ||
)); | ||
const files = this.props.directory.files.map(file => ( | ||
<File | ||
key={file.id} | ||
id={file.id} | ||
file={file} | ||
openFile={this.props.openFile} | ||
clickHandler={this.props.clickHandler} | ||
selectedItem={this.props.selectedItem} | ||
rename={this.props.rename} | ||
renameHandler={this.props.renameHandler} | ||
/> | ||
)); | ||
const directory = subdirectories.concat(files); | ||
|
||
const item = ( | ||
<div className="list-item" onClick={this.handleListItemClick}> | ||
<span className="icon icon-file-directory"> | ||
{this.props.directory.name} | ||
</span> | ||
<span className="plus-icon" onClick={this.props.openCreateMenu.bind(null, this.props.id, this.props.directory.path)}>+</span> | ||
{this.props.openMenuId === this.props.id ? <CreateMenu createForm={this.props.createForm} id={this.props.id} /> : <span />} | ||
{this.props.createMenuInfo.id === this.props.id ? <CreateForm createItem={this.props.createItem} /> : <span />} | ||
</div>) | ||
if (this.props.directory.opened) { | ||
return ( | ||
<li className={this.props.selectedItem.id === this.props.id ? 'list-nested-item selected' : 'list-nested-item'}> | ||
{this.props.rename && this.props.selectedItem.id === this.props.id ? <RenameForm renameHandler={this.props.renameHandler} /> : item} | ||
<span className="plus-icon" onClick={this.handlePlusIconClick}>+</span> | ||
{this.props.openMenuId === this.props.id ? | ||
<CreateMenu createForm={this.props.createForm} id={this.props.id} /> : | ||
<span /> | ||
} | ||
{this.props.createMenuInfo.id === this.props.id ? | ||
<CreateForm createItem={this.props.createItem} /> : | ||
<span /> | ||
} | ||
</div> | ||
); | ||
|
||
const listItemClassName = cx('list-nested-item', { | ||
collapsed: !this.props.directory.opened, | ||
selected: this.props.selectedItem.id === this.props.id | ||
}); | ||
|
||
return ( | ||
<li className={listItemClassName}> | ||
{this.props.rename && this.props.selectedItem.id === this.props.id ? | ||
<RenameForm renameHandler={this.props.renameHandler} /> : | ||
item | ||
} | ||
{this.props.directory.opened && | ||
<ul className="list-tree"> | ||
{arr} | ||
{directory} | ||
</ul> | ||
</li> | ||
) | ||
} else { | ||
return ( | ||
<li | ||
className={this.props.selectedItem.id === this.props.id ? 'list-nested-item collapsed selected' : 'list-nested-item collapsed'} | ||
> | ||
{this.props.rename && this.props.selectedItem.id === this.props.id ? <RenameForm renameHandler={this.props.renameHandler} /> : item} | ||
</li> | ||
) | ||
} | ||
} | ||
</li> | ||
) | ||
} | ||
} | ||
} |
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.
Arrow functions in classes are really hard to read. Maybe you should write them in a classical way like:
That change should also preserve a consistent code style because the handlers in the
App
component were not written as Arrow functions.Also, wouldn't the methods
handlePlusIconClick
andhandleListItemClick
need to be bound to thethis
instance in the Component constructor?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.
You're wrong. Arrow functions are easy to read. They help us to avoid using
bind
inconstructor
.When we are writing arrow functions we do not need
constructor
andbind
, because arrow functions already bound into an instance.Just read - https://medium.com/@housecor/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56
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.
What about code consistency - I've refactored all other files except App.jsx already. It will be next step
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.
That are good points - Thanks, I didn't know that article! When the utilization of arrow functions makes the binding in classes obsolete, then it is a great design decision to use them. Overall I like your changes. You're awesome 😄
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.
Thanks, Calvin! 👌
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.
@isnifer Probably if we can wrap the arrow function with the round parenthesis,
handleListItemClick = (proxyEvent) => {}
it would be more readable in terms of both classical and latest way of JavaScript coding pattern. Let me know your thoughts on this.