0
votes

Im trying to use a Selectable list of items in my Drawer Component. Im using the sample code define in the Material-ui documentation (Material-UI List Documentation), but things are not working as expected. Can anyone here helps me with a basic sample code using Selectable list or point me any good documentation or tutorial.

import React from 'react';
import Component from 'react';
import PropTypes from 'react';
import MobileTearSheet from '../../../MobileTearSheet';
import {List, ListItem, MakeSelectable} from 'material-ui/List';
import Avatar from 'material-ui/Avatar';
import Subheader from 'material-ui/Subheader';

let SelectableList = MakeSelectable(List);

function wrapState(ComposedComponent) {
    return class SelectableList extends Component {
        static propTypes = {
            children: PropTypes.node.isRequired,
            defaultValue: PropTypes.number.isRequired,
        };

        componentWillMount() {
            this.setState({
                selectedIndex: this.props.defaultValue,
            });
        }

        handleRequestChange = (event, index) => {
            this.setState({
                selectedIndex: index,
            });
        };

        render() {
            return (
                <ComposedComponent value={this.state.selectedIndex} onChange={this.handleRequestChange}>
                    {this.props.children}
                </ComposedComponent>
            );
        }
    };
}

SelectableList = wrapState(SelectableList);

const ListExampleSelectable = () => (
    <MobileTearSheet>
        <SelectableList defaultValue={3}>
            <Subheader>Selectable Contacts</Subheader>
            <ListItem
                value={1}
                primaryText="Brendan Lim"
                leftAvatar={<Avatar src="/images/obenbasic.png" />}
                nestedItems={[
          <ListItem
            value={2}
            primaryText="Grace Ng"
            leftAvatar={<Avatar src="/images/obenbasic.png" />}
          />,
        ]}
            />
            <ListItem
                value={3}
                primaryText="Kerem Suer"
                leftAvatar={<Avatar src="/images/obenbasic.png" />}
            />
            <ListItem
                value={4}
                primaryText="Eric Hoffman"
                leftAvatar={<Avatar src="/images/obenbasic.png" />}
            />
            <ListItem
                value={5}
                primaryText="Raquel Parrado"
                leftAvatar={<Avatar src="/images/obenbasic.png" />}
            />
        </SelectableList>
    </MobileTearSheet>
);

export default ListExampleSelectable;

and I use the Selectable List like this:

<Drawer open={false} width="180px" >
   <MenuItem primaryText="MENU ITEM" />
   <ListExampleSelectable />
</Drawer>

I hope this will give you an idea of what i'm doing...

1
Can you provide details of what you mean by "not working as expected"? Or if you're only looking for a resource to help you learn then I think that's outside of the scope of Stack Overflow.Ben Hare
Well, im not allowed, to provide my source here, if that's the point of your comment.kprim
Can you provide even the error message or what's happening if you can't provide the source code? No one will be able to help you without at least a bit of specificity.Ben Hare
Let's assume i have the entire sample code of selectable list in a jsx file, when i do an import of the 'ListExampleSelectable' component in my Drawer i have the following error in the jsx file (Module build failed: SyntaxError: Unexpected token (13:25)).kprim
Im just trying to figure out how to use the selectable list component from material-ui, because obviously im doing something wrong.kprim

1 Answers

0
votes

I finally realised why it was not working. More details on this post : SelectableList does not display Selected item when selection change