0

Are there any accordion and modal dialog components available and actually work that are compatible with my environment of:

Google's Material Design Lite version 1.06
Facebook's ReactJS 0.14.2
Microsoft's TypeScript and Visual Studio 2015 (for typescript bundling)

I am trying to avoid JavaScript library bloat and Material Design Lite is missing these essential widgets. I'm not using Node.js since I'm on a Windows platform so Material-UI is not an option. MaterializeCSS bloats my Visual Studio project and makes it really slow and unusable.

Lambert
  • 2,233
  • 5
  • 29
  • 44
  • 1
    Since you want to avoid bloat, have you thought to write your own accordion components, and use state and styles to get proper functionality? You could certainly then incorporate any ui styles you like. I did this recently. I used a component's state to keep track of opened/closed accordions. And css class names to provide shown/hidden (collapse/open) behavior. – Michael Ramos Jan 25 '16 at 17:39
  • Michael - Can you post the accordion code? I'm pretty green at this. – Lambert Jan 25 '16 at 17:45

1 Answers1

4

Update Sep 28,2016

It looks like there is now an open-source library for doing just this: https://github.com/fiffty/react-treeview-mui


Self Implementation

This answer serves as an example for an Accordion dropdown built using React, though not styled as Material Design. You would need to do that yourself.

This setup requires a hierarchy object of parent > child objects/arrays. This base class should be able to handle very deep depths just fine. It uses recursion for its structure setup. I'll also be using ES6 syntax for preference, as it helps setup the recursion easier for me.

Accordion Class:

// Accordian Class
// Dynamic/Recursive
// a parent>child>child... relationship is required
// the whole object can be named however you like,
// but each child object needs to be identified as "children"
class Accordian extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      openLevelRow: "", // this is the current open level row in an accordian (starts out with none being open)
      selfLevelObject: props.newLevel // the current level object containing all rows and their data/children
    };
  }

  // This is our toggle open/close method
  // if row is already open, close it
  // uniqueSelector is unique per row, and also a key
  // in the selfLevelObject (could be a name, id)
  toggleOpenClose(uniqueSelector) {
    // simple ternary assignment
    this.setState({
      openLevelRow: this.state.openLevelRow != uniqueSelector ? uniqueSelector : ""
    });
  }

  render () {  
    // deconstruct assignment from state
    const { selfLevelObject, openLevelRow } = this.state

    return (
      <div>
          {selfLevelObject.map((row, i) =>
              {/* Collectively where all children of the same hierchy level are listed*/}
              <div className="accordian-hold-self-level" key={i} >
                {/* This is an individual collapsable Row */}
                <div onClick={this.toggleOpenClose.bind(this, row.uniqueSelector)} className="accordian-title-row">
                  <p className='accordian-title'> {row.title}</p>
                </div>
                {/* 
                    When iterating the list, find out if a row has been opened
                */}
                {this.state.openLevelRow != row.uniqueSelector ? <span></span> : 
                    /* 
                      This code block is called if the current row is opened
                      now we to need to find out if there are children,
                      if not, then we are at the bottom, do what ever 
                      you'd like with the bottom row
                    */
                    selfLevelObject[uniqueSelector].children != undefined ? 
                      <Accordian newLevel={selfLevelObject[uniqueSelector].children} /> 
                      : // else
                    // TODO - whatever you want with bottom row
                }
              </div> 
          )}
        </div>
    );
  }
}

Accordian.propTypes = {
    newLevel: React.PropTypes.object
}
Michael Ramos
  • 5,523
  • 8
  • 36
  • 51
  • Michael - Thanks for the sample code. I'll have to learn ReactJS first before I attempt to convert it to TypeScript and fully test it. – Lambert Jan 27 '16 at 16:22
  • For the missing Dialog component, I decided to use the "Cards" component as a base for creating a custom dialog with ReactJS. I also believe I can use the "Menus" component and/or a standard HTML "select" tag for drop down menus. I assume that Material Design Lite version 2.0 will hopefully include these components. – Lambert Jan 28 '16 at 21:19
  • Hey yea awesome, and always remember that you could find non-React components/html, and wrap them in React yourself. I might implement a solid version of the accordion this weekend. Not sure – Michael Ramos Jan 29 '16 at 12:40
  • @Lambert, I know this is late, but it looks like someone just implemented exactly what you wanted: https://github.com/fiffty/react-treeview-mui – Michael Ramos Sep 28 '16 at 15:19