GithubHelp home page GithubHelp logo

madox2 / react-tagcloud Goto Github PK

View Code? Open in Web Editor NEW
186.0 4.0 31.0 8.76 MB

Tag/word cloud component for react https://madox2.github.io/react-tagcloud/

License: MIT License

JavaScript 100.00%
react tag-cloud word-cloud

react-tagcloud's Introduction

react-tagcloud

Simple and extensible tag/word cloud React component.

See the demo.

preview tag-cloud preview

Installation

npm install react-tagcloud

# or with yarn
yarn add react-tagcloud

# react < 16.8.0
npm install [email protected]

Basic usage

import { TagCloud } from 'react-tagcloud'

const data = [
  { value: 'JavaScript', count: 38 },
  { value: 'React', count: 30 },
  { value: 'Nodejs', count: 28 },
  { value: 'Express.js', count: 25 },
  { value: 'HTML5', count: 33 },
  { value: 'MongoDB', count: 18 },
  { value: 'CSS3', count: 20 },
]

const SimpleCloud = () => (
  <TagCloud
    minSize={12}
    maxSize={35}
    tags={data}
    onClick={tag => alert(`'${tag.value}' was selected!`)}
  />
)

React Native

In react native projects import tag cloud from the package relative path react-tagcloud/rn.

import React from 'react';

import { Alert } from 'react-native';
import { TagCloud } from 'react-tagcloud/rn'

const data = [
  // ...
]

const SimpleCloud = () => (
  <TagCloud
    minSize={12}
    maxSize={35}
    tags={data}
    onPress={tag => Alert.alert(`'${tag.value}' was selected!`)}
  />
)

API

Options

<TagCloud /> component has props listed below:

Option Type Required Note
tags Array true Array of objects that represent tags (see Tag object)
maxSize Number true Maximal font size (in px) used in cloud
minSize Number true Minimal font size (in px) used in cloud
shuffle Boolean false If true, tags are shuffled. When tags are modified, cloud is re-shuffled. Default: true
colorOptions Object false Random color options (see randomColor#options)
disableRandomColor Boolean false If true, random color is not used
randomSeed Number false Random seed used to shuffle tags and generate color
renderer Function false Function used to render tag
randomNumberGenerator Function false DEPRECATED, use randomSeed instead. Specifies a custom random number generator that is being used by shuffle algorithm. Default: Math.random

Note: Furthermore you can pass any other prop and it will be forwarded to the wrapping <div /> component (e.g. style, className).

Tag object

Each tag is represented by object literal having following properties:

Property Type Required Note
value String true String value to be displayed
count Number true Represents frequency of the tag that is used to calculate tag size
key String false Tag element key. If it is not provided, value property will be used instead (however it can fail if you don't have unique tag values. It is highly recommeded to use key property)
color String false Represents color of the tag. If it is not provided, random color will be used instead
props Object false Props to be passed to a particular tag component

Events

Event handlers can be passed to the <TagCloud /> props. Each handler has two arguments: the first is related tag object and the second is DOM event object.

Currently supported events: onClick, onDoubleClick, onMouseMove

Note: Feel free to open issue if any other event is needed.

Styles

Default class names are tag-cloud for the wrapping container, and tag-cloud-tag for a particular tag. Styles passed to <TagCloud /> props will be applied to the wrapping container.

Renderer

Rendering of tags can be fully customized by providing custom render function and passing it to the renderer prop. By default is used defaultRenderer. Render function has three arguments - tag, size and color. For example:

import { TagCloud } from 'react-tagcloud'

const customRenderer = (tag, size, color) => {
  return (
    <span key={tag.value} style={{ color }} className={`tag-${size}`}>
      {tag.value}
    </span>
  )
}

const CustomizedCloud = () => (
  <TagCloud tags={data} minSize={1} maxSize={5} renderer={customRenderer} />
)

More examples

Testing

Install dev modules:

yarn install

Run unit tests

yarn test

Run examples

cd examples
yarn install
yarn start

and open browser at http://localhost:3000

License

MIT License

react-tagcloud's People

Contributors

dependabot[bot] avatar madox2 avatar ownadi avatar rahavlussato avatar udovenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-tagcloud's Issues

Mouse hover event

Request - Add onMouseOver and onMouseOut events

Sample API
const onMouseOver = tag => console.log("this is value and count", tag.value, tag.count)

<TagCloud onMouseOver={onMouseOver} onMouseOut={onMouseOut} ... />

Also, I think it'd be great to offer a tooltip render option upon hover.
Thank you for the clean abstractions!

TypeError: dispatcher is null

Using react 17.0.2 and nextjs 12.0.8

I simply import tagcloud and place the tag in my html return and get>

Unhandled Runtime Error

TypeError: dispatcher is null
Call Stack
useState
../node_modules/react/cjs/react.development.js (1622:0)
TagCloud
../node_modules/react-tagcloud/lib/TagCloud.js (135:0)
renderWithHooks
node_modules/react-dom/cjs/react-dom.development.js (14985:0)
mountIndeterminateComponent
node_modules/react-dom/cjs/react-dom.development.js (17811:0)
beginWork
node_modules/react-dom/cjs/react-dom.development.js (19049:0)
callCallback
node_modules/react-dom/cjs/react-dom.development.js (3945:0)

I also tried to force client side rendering just in case this was the culprit:

{ process.browser && <TagCloud
                minSize={5}
                maxSize={35}
                tags={this.state.wordcloud_data}
            />    }

But unfortunately still the same error.
The line referenced in the error message is the 2nd one in this snippet:

function TagCloud(props) {
  var _useState = (0, _react.useState)([]),
      _useState2 = _slicedToArray(_useState, 2),

I initialize the data in the component constructor so wordcloud_data should never be null:

this.state = {
            campaign_name: "", 
            tracker: "", 
            talent_table_content: [],
            wordcloud_data: [
                {value: "test1", count: 12}, 
                {value: "test2", count: 25}, 
                {value: "test3", count: 39}, 
                {value: "test4", count: 2}, 
                {value: "test5", count: 40}
            ]
        }

Error while using in nextjs

image

All the classes and tags are made that can be seen in inspect element but all the spans are empty, its like it is not picking the values from data.

Getting warning: React does not recognize the `containerComponent` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `containercomponent` instead. If you accidentally passed it from a parent component, remove it from the DOM element.

I can see in the library code the use of const Container = props.containerComponent, line 102, TagCloud.js.

React warnings about that:
https://reactjs.org/warnings/unknown-prop.html

Any way to fix that warning?

Class components compatibility

Is usage supported in react class components?
I am using [email protected]

error reported in console

react.development.js:1622  Uncaught TypeError: Cannot read properties of null (reading 'useState')
    at useState (react.development.js:1622:1)
    at TagCloud (TagCloud.js:161:1)
    at renderWithHooks (react-dom.development.js:14803:1)
    at mountIndeterminateComponent (react-dom.development.js:17482:1)
    at beginWork (react-dom.development.js:18596:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:188:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:237:1)
    at invokeGuardedCallback (react-dom.development.js:292:1)

thanks

`defaultProps` console warning

Currently using v2.3.1, and I see a console error stating TagCloud: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.

Everything seems to be working fine, just wanted to call it out before it potentially breaks down the road.

Update to support react 16

Currently this package has react ^15.5.0 as a peerDependency.

Since react 16 is out, this package should be updated to support it.

Support React Native by allow customize the default container

react-tagcloud already support custom renderer,
so we can use custom renderer to render Text in react native.
But React Native use <View> instead of <div> as the default container.

What's missing is the way to customize the default container, ex:

import { Text, View } from 'react-native'
import { TagCloud } from "react-tagcloud"

// PROPOSED one
const customContainer(props) => {
  <View>
    {props.children}
  </View>
}

const customRenderer = (tag, size, color) => {
      return (
        <Text
          key={tag.value}
          style={{
             color: 'xxx',
             fontSize: 'xxx',
          }}
        >
          {tag.value}
        </Text>
      )
    };

const data = [
  { value: "JavaScript", count: 38 },
  { value: "React", count: 30 },
  { value: "Nodejs", count: 28 },
  { value: "Express.js", count: 25 },
  { value: "HTML5", count: 33 },
  { value: "MongoDB", count: 18 },
  { value: "CSS3", count: 20 }
];

return(
  <TagCloud
    tags={data}
    renderer={customRenderer}
    // PROPOSED one
    container={customContainer}
    style={{flexDirection:'row', flexWrap:'wrap', alignItems: 'center', 
     justifyContent:'center'}}
  />
)

@madox2 how do you think?

Support for React v17

Due to the peerDependency's current value, this library can not be installed alongside React v17.

It would be cool to add support for this version of React!

Let me know if you need any help with this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.