react-commentbox
A generic comment box made in React
Installation
npm install react-commentbox --save
Usage
This component has the following default props:
const defaultProps = classPrefix: 'cb-' // prefixes every css class with this className: 'commentbox' // the class name of the top-level element disabled: true // disabled when there's no user/author name present usersHaveAvatars: false // if true, your comments must have a userAvatarUrl field levelPadding: 25 // how much to indent each nested comment textareaRows: 7 // number of rows initially visible in the textarea loadingContent: 'Loading...' // can be a string or instance of a component expandButtonContent: '[+]' // can be a string or instance of a component contractButtonContent: '[-]' // can be a string or instance of a component showReplyButtonContent: 'reply' // can be a string or instance of a component hideReplyButtonContent: 'cancel' // can be a string or instance of a component postReplyButtonContent: 'Post Reply' // can be a string or instance of a component postCommentButtonContent: 'Post Comment' // can be a string or instance of a component postButtonExtraContent: null // placed next to the comment and reply buttons, can be a string or instance of a component disabledComponent: ReactComponent // what to show when the user isn't logged in or there's no author info to use { // turns your comment object into an object the component expects return id // a unique id for this comment. used as the "key" prop bodyDisplay // a string or component to be displayed as the comment body userNameDisplay // a string or component to be displayed as the user name timestampDisplay // a string or component to be displayed as the timestamp belongsToAuthor // does this comment belong to the currently logged in author? parentCommentId // if this comment is a reply to another comment, set it's id here className // if this comment should have a special css class on it ; } // replace with a function call that fetches comments in oldest to newest order // replace with a function call that creates a comment;
- The
getComments
function should resolve to an array of comments. Each element in that array is run through the suppliednormalizeComment
function. Comments must be in order of oldest to newest in order to properly determine comment nesting. - the
comment
function should be replaced with your ajax call to create the comment. It doesn't need to resolve to anything. - comments eventually need an author to attribute themselves to. You manage that on your own via the
disabled
anddisabledComponent
props.
Example
The below example is an "anonymous" comment implementation, where you just need to type in an author name at the time of posting. You'll usually want to replace that logic with a login implementation.
This example assumes your comments are coming from Contentful, thus the normalizeComment
is catered to that use case.
It also assumes that the /create-comment
endpoint exists. You can find a full, working example
of how to implement these endpoints in Express.js in the commentbox/api
directory.
import React from 'react';import CommentBox from 'react-commentbox'; Component state = authorName: '' authorNameIsSet: false ; this; { e; this; }; // fetch our comments from Contentful { return thispropscontentfulClient; }; // transform Contentful entries to objects that react-commentbox expects. { const id createdAt = commentsys; const body author parentComment = commentfields; return id bodyDisplay: body userNameDisplay: author timestampDisplay: createdAt0 belongsToAuthor: false parentCommentId: parentComment ? parentCommentsysid : null ; }; // make an API call to post a comment { return thisprops; }; // will be shown when the comment box is disabled { return <form => <input ="text" ="Enter your name to post a comment" = = /> <button ="submit">Submit</button> </form> ; }; { return <div> <h2>Comments</h2> <CommentBox = = = = = /> </div> ; };