Skip to content
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

Fix word count and paragraph count in Filler/index.js #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/components/Filler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ export class Filler extends React.PureComponent {
super(props);
this.state = {
paragraphCount: 3,
paragraphText: "3",
wordcount: 500,
wordText: "500",
copied: false
};
this.textareaRef = React.createRef();
this.updateWordcount = this.updateWordcount.bind(this);
this.updateParagraphcount = this.updateParagraphcount.bind(this);
}

generateText() {
Expand Down Expand Up @@ -44,7 +47,24 @@ export class Filler extends React.PureComponent {
}

updateWordcount(wordcount) {
this.setState({ wordcount: wordcount > 99 ? parseInt(wordcount) : 500, copied: true }, () => {
wordcount = wordcount?.toString();
this.setState({
wordText: wordcount && /^\d+$/.test(wordcount) && wordcount.charAt(0) !== "0" ? wordcount : "",
wordcount: wordcount && /^\d+$/.test(wordcount) && wordcount >= 0 ? parseInt(wordcount) : 0, // check for integer values >= 0
copied: true
}, () => {
this.textareaRef.current.select();
document.execCommand("copy");
this.textareaRef.current.focus();
})
}

updateParagraphcount(paragraphCount) {
this.setState({
paragraphText: paragraphCount && /^\d+$/.test(paragraphCount) && paragraphCount.charAt(0) !== "0" ? paragraphCount : "",
paragraphCount: paragraphCount && /^\d+$/.test(paragraphCount) && paragraphCount >= 1 ? parseInt(paragraphCount) : 1, // check for integer values >= 1
copied: true
}, () => {
this.textareaRef.current.select();
document.execCommand("copy");
this.textareaRef.current.focus();
Expand All @@ -59,17 +79,13 @@ export class Filler extends React.PureComponent {
<div className="flex-item">
<div>
<label htmlFor="wordcount">Word count:</label>
<input onChange={e => this.updateWordcount(e.target.value)} value={this.state.wordcount} step="100" type="number" name="wordcount" min="0" />
<input onChange={e => this.updateWordcount(e.target.value)} value={this.state.wordText} step="100" type="number" name="wordcount" min="0" />
<span className="semibold pointer" onClick={() => this.updateWordcount(Math.ceil((this.state.wordcount + 99) / 100) * 100)}>&#8593;</span>
<span className="semibold pointer" onClick={() => this.updateWordcount(Math.round((this.state.wordcount - 99) / 100) * 100)}>&#8595;</span>
</div>
<div>
<label htmlFor="paragraphs">Paragraphs:</label>
<input onChange={e => this.setState({ paragraphCount: parseInt(e.target.value), copied: true }, () => {
this.textareaRef.current.select();
document.execCommand("copy");
this.textareaRef.current.focus();
})} value={this.state.paragraphCount} type="number" name="paragraphs" min="1" />
<input onChange={e => this.updateParagraphcount(e.target.value)} value={this.state.paragraphText} type="number" name="paragraphs" min="1" />
</div>
{this.state.copied && <Copied />}
</div>
Expand Down