Git Product home page Git Product logo

react-selected-items's Introduction

react-selected-items

React component for selected items.

properties:

  static propTypes = {
    className:PropTypes.string,
    type:PropTypes.oneOf([
      'single',
      'multiple',
      'toggle',
      'other'
    ]),
    items:PropTypes.array,
    value:PropTypes.array,
    valueKey:PropTypes.string,
    onChange:PropTypes.func,
  };

usage:

import ReactSelectedItems from './main';
let id=100;
class TestItem extends React.Component{
  render(){
    const {value,text,selected,...props} = this.props;
    console.log(selected);
    return (
      <div {...props} data-kk={`kk-${selected}`} className="test-item">{value}={text}</div>
    );
  }
}

class App extends React.Component{
  state = {
    items1:[
      {
        value:1,
        text:'abc'
      },
      {
        value:2,
        text:'bcd'
      },
      {
        value:3,
        text:'AAA'
      }
    ],
    items2:[
      {
        value:1,
        text:'222-abc'
      },
      {
        value:2,
        text:'222-bcd'
      },
      {
        value:3,
        text:'222-AAA'
      },
      {
        value:4,
        text:'222-BBB'
      },
      {
        value:5,
        text:'222-CCC'
      }
    ],
    items3:[
      {
        value:1,
        text:'333-abc'
      },
      {
        value:2,
        text:'333-bcd'
      },
      {
        value:3,
        text:'333-AAA'
      },
      {
        value:4,
        text:'333-BBB'
      },
      {
        value:5,
        text:'333-CCC'
      }
    ],
    items4:[
      {
        value:1,
        text:'44477787-abc'
      },
      {
        value:2,
        text:'44477787-bcd'
      },
      {
        value:3,
        text:'44477787-AAA'
      },
      {
        value:4,
        text:'44477787-BBB'
      },
      {
        value:5,
        text:'44477787-CCC'
      }
    ],
    d4Value:[3],
    dValue:[1,4],
    dItems:[
      {
        value:1,
        text:'44477787-abc'
      },
      {
        value:2,
        text:'44477787-bcd'
      },
      {
        value:3,
        text:'44477787-AAA'
      },
      {
        value:4,
        text:'44477787-BBB'
      },
      {
        value:5,
        text:'44477787-CCC'
      }
    ]
  };
  _change1(inEvent){
    console.log(inEvent.target.value);
  }

  _change2(inEvent){
    console.log(inEvent.target.value);
  }
  _change3(inEvent){
    console.log(inEvent.target.value);
  }

  //singleToggle:
  _change4(inItem,inSelf){
    console.log('Your own implemnts:->',inItem,inSelf);
  }


  _click1(){
    const {dItems} = this.state;
    dItems.push({
        value:id++,
        text:'DDDYDYD787-CCC'
    });
    this.setState({
      dValue:[2,5],
      dItems:dItems.slice(0)
    })
  }

  _change5(inEvent){
    console.log(inEvent.target.value);
  }

  render(){
    return (
      <div className="hello-react-active-items">
        <h4>Single Active Item</h4>
        <ReactSelectedItems value={[1]} items={this.state.items1} type='single' onChange={this._change1.bind(this)}>
          <TestItem />
        </ReactSelectedItems>
        <h4>Multiple Active Item</h4>
        <ReactSelectedItems value={[1,4]} items={this.state.items2} type='multiple' onChange={this._change2.bind(this)}>
          <TestItem />
        </ReactSelectedItems>
        <h4>Toggle Active Item</h4>
        <ReactSelectedItems value={[2]} items={this.state.items3} type='toggle' onChange={this._change3.bind(this)}>
          <TestItem />
        </ReactSelectedItems>
        <h4>Other Active Item</h4>
        <ReactSelectedItems value={this.state.d4Value} items={this.state.items4} type='other' onChange={this._change4.bind(this)}>
          <TestItem />
        </ReactSelectedItems>

        <h4>Dynamic value/items</h4>
        <button onClick={this._click1.bind(this)}>Dynamic Set value & items</button>
        <ReactSelectedItems value={this.state.dValue} items={this.state.dItems} type='multiple' onChange={this._change5.bind(this)}>
          <TestItem />
        </ReactSelectedItems>
    </div>
    );
  }
}

resource:

react-selected-items's People

Contributors

afeiship avatar

Watchers

 avatar  avatar  avatar

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.