-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[#1526] Add ellipsis to sidebar file names #1527
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
Changes from all commits
937421e
21761a6
65bd4b2
3f30968
223d716
a56740c
90e81d8
763ee14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,61 @@ import FolderRightIcon from '../../../images/triangle-arrow-right.svg'; | |
import FolderDownIcon from '../../../images/triangle-arrow-down.svg'; | ||
import FileIcon from '../../../images/file.svg'; | ||
|
||
function parseFileName(name) { | ||
const nameArray = name.split('.'); | ||
if (nameArray.length > 1) { | ||
const extension = `.${nameArray[nameArray.length - 1]}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use nameArray.slice(-1) here, if that's more readable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I always forget that I can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'm actually going to change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, so apparently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure to check/test what happens if you pass an empty string to it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind, you're catching this on line 15. My bad! |
||
const baseName = nameArray.slice(0, -1).join(''); | ||
const firstLetter = baseName[0]; | ||
const lastLetter = baseName[baseName.length - 1]; | ||
const middleText = baseName.slice(1, -1); | ||
return { | ||
baseName, | ||
firstLetter, | ||
lastLetter, | ||
middleText, | ||
extension | ||
}; | ||
} | ||
const firstLetter = name[0]; | ||
const lastLetter = name[name.length - 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
const middleText = name.slice(1, -1); | ||
return { | ||
baseName: name, | ||
firstLetter, | ||
lastLetter, | ||
middleText | ||
}; | ||
} | ||
|
||
function FileName({ name }) { | ||
const { | ||
baseName, | ||
firstLetter, | ||
lastLetter, | ||
middleText, | ||
extension | ||
} = parseFileName(name); | ||
return ( | ||
<span className="sidebar__file-item-name-text"> | ||
<span>{firstLetter}</span> | ||
{baseName.length > 2 && | ||
<span className="sidebar__file-item-name--ellipsis">{middleText}</span> | ||
} | ||
{baseName.length > 1 && | ||
<span>{lastLetter}</span> | ||
} | ||
{extension && | ||
<span>{extension}</span> | ||
} | ||
</span> | ||
); | ||
} | ||
|
||
FileName.propTypes = { | ||
name: PropTypes.string.isRequired | ||
}; | ||
|
||
export class FileNode extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
@@ -206,11 +261,12 @@ export class FileNode extends React.Component { | |
</div> | ||
} | ||
<button | ||
aria-label="Name" | ||
aria-label={this.state.updatedName} | ||
className="sidebar__file-item-name" | ||
onClick={this.handleFileClick} | ||
data-testid="file-name" | ||
> | ||
{this.state.updatedName} | ||
<FileName name={this.state.updatedName} /> | ||
</button> | ||
<input | ||
data-testid="input" | ||
|
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.
It's a good idea to catch
if (!name) return;
at the beginning of the function, otherwise.split
can explodeThere 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.
name
is a required string proptype, which means that the component itself will complain if it's used without this prop, so is this necessary?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.
True, in this case it's kind of pointless