npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-commentbox

v3.0.1

Published

A generic comment box made in React

Downloads

46

Readme

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: React.Component, // what to show when the user isn't logged in or there's no author info to use
    normalizeComment: (comment) => { // 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         
        };
    },
    getComments: () => new Promise(), // replace with a function call that fetches comments in oldest to newest order
    comment: (body, parentCommentId = null) => new Promise(), // 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 supplied normalizeComment 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 and disabledComponent 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';

class MyCommentBox extends React.Component {

    state = { authorName: '', authorNameIsSet: false };

    onChangeAuthorName = (e) => this.setState({ authorName: e.currentTarget.value });

    onSubmitAuthorName = (e) => {

        e.preventDefault();
        this.setState({ authorNameIsSet: true });
    };

    // fetch our comments from Contentful
    getComments = () => {

        return this.props.contentfulClient.getEntries({
            'order': 'sys.createdAt', // important for determining nested comments
            'content_type': 'comment',
            'fields.subject': this.props.subjectId,
        }).then( response => {

            return response.items;

        }).catch(console.error);
    };

    // transform Contentful entries to objects that react-commentbox expects.
    normalizeComment = (comment) => {

        const { id, createdAt } = comment.sys;
        const { body, author, parentComment } = comment.fields;

        return {
            id,
            bodyDisplay: body,
            userNameDisplay: author,
            timestampDisplay: createdAt.split('T')[0],
            belongsToAuthor: false,
            parentCommentId: parentComment ? parentComment.sys.id : null
        };
    };

    // make an API call to post a comment
    comment = (body, parentCommentId) => {

        return this.props.postData('/create-comment', {
            body,
            parentCommentId,
            authorName: this.state.authorName,
            subjectId: this.props.subjectId
        });
    };

    // will be shown when the comment box is disabled
    disabledComponent = (props) => {

        return (
            <form onSubmit={ this.onSubmitAuthorName }>
                <input
                    type="text"
                    placeholder="Enter your name to post a comment"
                    value={ this.state.authorName }
                    onChange={ this.onChangeAuthorName }
                />
                <button type="submit">Submit</button>
            </form>
        );
    };

    render() {

        return (
            <div>
                <h2>Comments</h2>
                <CommentBox
                    disabled={ !this.state.authorNameIsSet }
                    getComments={ this.getComments }
                    normalizeComment={ this.normalizeComment }
                    comment={ this.comment }
                    disabledComponent={ this.disabledComponent }
                />
            </div>
        );
    };
}