From 87a5c4b2d6b4f0c1e0f70e2a988dd517546457ce Mon Sep 17 00:00:00 2001 From: David Jay Date: Mon, 12 Dec 2016 20:29:54 -0500 Subject: [PATCH] Displaying character count and preventing user from posting when count is too high. --- client/coral-embed-stream/style/default.css | 12 +++++++++++- client/coral-plugin-commentbox/CommentBox.js | 18 +++++++----------- .../coral-plugin-commentbox/translations.json | 6 ++++-- client/coral-ui/components/Button.css | 11 +++++++++++ 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/client/coral-embed-stream/style/default.css b/client/coral-embed-stream/style/default.css index c27baf406..463c8dc68 100644 --- a/client/coral-embed-stream/style/default.css +++ b/client/coral-embed-stream/style/default.css @@ -95,7 +95,7 @@ hr { margin-top: 10px; } -#coralStream .coral-plugin-commentbox-button { +.coral-plugin-commentbox-button { float: right; margin-top: 10px; padding: 5px 10px; @@ -111,6 +111,16 @@ hr { margin-bottom: 5px; } +.coral-plugin-commentbox-char-count { + color: #ccc; + text-align: right; + font-size: 12px; +} + +.coral-plugin-commentbox-char-max { + color: #d50000; +} + /* Comment styles */ .comment { margin-bottom: 10px; diff --git a/client/coral-plugin-commentbox/CommentBox.js b/client/coral-plugin-commentbox/CommentBox.js index 078aba201..d5df14e3d 100644 --- a/client/coral-plugin-commentbox/CommentBox.js +++ b/client/coral-plugin-commentbox/CommentBox.js @@ -53,6 +53,10 @@ class CommentBox extends Component { if (child_id || parent_id) { updateItem(child_id || parent_id, 'showReply', false, 'comments'); } + + if (this.props.charCount && this.state.body.length > this.props.charCount) { + return; + } postItem(comment, 'comments') .then((postedComment) => { const commentId = postedComment.id; @@ -70,17 +74,9 @@ class CommentBox extends Component { this.setState({body: ''}); } - onUpdateComment = (e) => { - const body = e.target.value; - if (!this.props.charCount || body.length > this.props.charCount) { - this.setState({body}); - } - } - render () { const {styles, reply, author, charCount} = this.props; const length = this.state.body.length; - // How to handle language in plugins? Should we have a dependency on our central translation file? return
@@ -99,16 +95,16 @@ class CommentBox extends Component { onChange={(e) => this.setState({body: e.target.value})} rows={3}/>
-
charCount && `${name}-char-max`}`}> +
charCount ? `${name}-char-max` : ''}`}> { charCount && - `${length}/${charCount}` + `${charCount - length} ${lang.t('characters-remaining')}` }
{ author && (