1.0.20 • Published 6 years ago

react-ui-set v1.0.20

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

react-ui-set

React UI material

NPM JavaScript Style Guide

Install

npm install --save react-ui-set

Material list

  • DragDropProvider - Demo
  • (Other in progress)

Contents

DragDropProvider

This is a React class that has ready methods for use Drag & Drop.

Standard methods:

  • onDragStart
  • onDrag
  • onDragEnd
  • onDragOver
  • onDragEnter
  • onDragLeave
  • onDrop

Special method

  • onAllEventDragDrop

Usage

Step 1

Create class Extends from DragDropProvider

import React from 'react'
import { DragDropProvider } from 'react-ui-set'

class MyComponent extends DragDropProvider {
 . . .
}

Step 2

Add the required attributes to elements.

         <div
            . . .
            data-dragid={'example-id'}            // required
            . . .
            draggable={true}                      // required

            onDragStart={this.onDragStart}        // required
            onDrag={this.onDrag}
            onDragEnd={this.onDragEnd}
            onDrop={this.onDrop}
            onDragOver={this.onDragOver}
            onDragEnter={this.onDragEnter}
            onDragLeave={this.onDragLeave}
          />

Attributes

Attributes table

Attribute nameInherited methodRequiredTypeExampleDescription
data-dragid-requiredstring"main-id"Unique string
draggable-requiredbooleantrueStandard attribute
onDragStartthis.onDragStartrequiredfuncReact attribute
onDragthis.onDrag*funcReact attribute
onDragEndthis.onDragEnd*funcReact attribute
onDropthis.onDrop*funcReact attribute
onDragOverthis.onDragOver*funcReact attribute
onDragEnterthis.onDragEnter*funcReact attribute
onDragLeavethis.onDragLeave*funcReact attribute

Callback

Callback table

Callback nameDescription
cbDragDropRecommended for all events
cbDragStartNeed attribute onDragStart in HTML element
cbDragNeed attribute onDrag in HTML element
cbDragEndNeed attribute onDragEnd in HTML element
cbDropNeed attribute onDrop in HTML element
cbDragOverNeed attribute onDragOver in HTML element
cbDragEnterNeed attribute onDragEnter in HTML element
cbDragLeaveNeed attribute onDragLeave in HTML element

cbDragDrop (method, eventElementId, e, currentDragElementId)

Recommended for all events

Attribute nametypevalueDescription
methodstring"DragStart", "Drag", "DragEnd", "DragEnter", "DragLeave", "DragOver", "Drop"
eventElementIdstring*
eevent*
currentDragElementIdstring*

cbDragStart (method, currentDragElementId, e)

Attribute nametypevalueDescription
methodstring"DragStart"
currentDragElementIdstring*
eevent*

cbDrag (method, eventElementId, e, currentDragElementId)

onDragEnd (method, eventElementId, e, currentDragElementId)

onDragOver (method, eventElementId, e, currentDragElementId)

onDragEnter (method, eventElementId, e, currentDragElementId)

onDragLeave (method, eventElementId, e, currentDragElementId)

onDrop (method, eventElementId, e, currentDragElementId)

Attribute nametypevalueDescription
methodstring*"Drag" "DragEnd", "DragEnter", "DragLeave", "DragOver", "Drop"
eventElementIdstring*
eevent*
currentDragElementIdstring*

Example 1

Demo

  |-App.js
  |-DragDropElements.js
  |-index.css

DragDropElements.js

import React from 'react'
import { DragDropProvider } from 'react-ui-set'
import PropTypes from 'prop-types'

class DragDropElements extends DragDropProvider {
  render() {
    const {data} = this.props

    return (
      <div>
        {data.map(i => (
          <div
            key={`key-${i.name}`}
            className='bar-el'
            style={{backgroundColor: i.color}}
            data-dragid={i.name}                  // required
            draggable={true}                      // required

            onDragStart={this.onDragStart}        // required
            onDragOver={this.onDragOver}
            onDragLeave={this.onDragLeave}
          />
        ))}
      </div>
    )
  }
}

DragDropElements.propTypes = {
  data: PropTypes.array
}

export default DragDropElements

App.js

import React, { Component } from 'react'
import DragDropElements from './DragDropElements'

const bars = [
  {name: 'green', color: 'green'},
  {name: 'blue', color: 'blue'},
  {name: 'red', color: 'red'},
  {name: 'grey', color: 'grey'},
  {name: 'Cornsilk', color: 'Cornsilk'},
  {name: 'Cyan', color: 'Cyan'},
  {name: 'DarkViolet', color: 'DarkViolet'},
  {name: 'Gold', color: 'Gold'}
]

class App extends Component {
  constructor(props) {
    super(props)

    this.callbackDragDrop = this.callbackDragDrop.bind(this)
    this.state = {
      colorDrag: 'None',
      colorOver: 'None'
    }
  }

  /**
   * Callback drag-and-drop events
   * @param {string} method - Drag&Drop event name
   * @param {string} elementId - Id Over/Enter/Leave element
   * @param {HTMLElement} e - event
   * @param {string} currentDragElementId - id Drag element
   */
  callbackDragDrop(method, elementId, e, currentDragElementId) {
    switch (method) {

      case 'DragStart':
        e.currentTarget.style.border = '5px solid white'
        break

      case 'DragOver':
        this.setState({
          colorDrag: currentDragElementId,
          colorOver: elementId
        })
        e.currentTarget.style.border = `5px solid ${currentDragElementId}`
        break

      case 'DragLeave':
        e.currentTarget.style.border = '5px solid white'
        break

      case 'DragEnd':
      case 'DragEnter':
      case 'Drag':
      case 'Drop':
        // TODO
        break

      default:
        break
    }
  }

  render() {
    const {colorDrag, colorOver} = this.state

    return (
      <div>
        <h1>DragDropProvider</h1>
        <h2>Color Drag: <span style={{color: colorDrag}}>{colorDrag}</span></h2>
        <h2>Color Over: <span style={{color: colorOver}}>{colorOver}</span></h2>
        <p>Drag the square</p>
        <DragDropElements
          cbDragDrop={this.callbackDragDrop}
          data={bars}
        />
      </div>
    )
  }
}

export default App

index.css

body {
  margin: 50px;
  padding: 0;
  font-family: sans-serif;
}

.bar-el {
  position: relative;
  float: left;
  width: 50px;
  height: 50px;
  margin: 10px;
  border: 5px solid white;
}

Changelog

1.0.10

  • Refactoring callback attribute

License

MIT © zhukyuri

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago