I am using Material UI next library and currently I am using List ponent. Since the library is in beta, lot of its parameter names get changed. To solve this I am planning to write a wrapper around the required ponents so that things wont break. My list ponent :
<List dense>
<List className={classes.myListStyles}>
<ListItem disableGutters/>
</List>
</List>
How should I write the wrapper for the List(say myListWrapper) and ListItem so that the wrapper ponent can handle props and pass them to the actual MUI list ponent inside?
I am using Material UI next library and currently I am using List ponent. Since the library is in beta, lot of its parameter names get changed. To solve this I am planning to write a wrapper around the required ponents so that things wont break. My list ponent :
<List dense>
<List className={classes.myListStyles}>
<ListItem disableGutters/>
</List>
</List>
How should I write the wrapper for the List(say myListWrapper) and ListItem so that the wrapper ponent can handle props and pass them to the actual MUI list ponent inside?
Share Improve this question edited Feb 22, 2018 at 7:05 Mayank Shukla 105k19 gold badges162 silver badges145 bronze badges asked Feb 22, 2018 at 6:49 SeaWarrior404SeaWarrior404 4,16816 gold badges47 silver badges66 bronze badges2 Answers
Reset to default 4I had worked on MUI wrappers, writing my own library for a project. The implementation we are focusing, is to pass the props to inner/actual-MUI ponent from the our wrapper ponent. with manipulation. In case of wrapping props for abstraction.
Following is my approach to the solution:
import { List as MaterialList } from 'material-ui/List';
import { React } from 'react';
import { ListItem as MaterialListI } from 'material-ui/ListItem';
class List extends MaterialList {
constructor(props){
const propsToPass = {
prop1 : change(props.prop1),
...props
}
super(propsToPass);
}
};
class ListItem extends MaterialListItem {
const propsToPass = {
prop1 : change(props.prop1),
prop2 : change(props.prop2),
...props
}
super(propsToPass);
}
};
class App extends React.Component {
render () {
return (
<List prop='value' >
<ListItem prop1={somevalue1} prop2={somevalue2} />
<ListItem prop1={somevalue1} prop2={somevalue2} />
<ListItem prop1={somevalue1} prop2={somevalue2} />
</List>
)
}
};
Above code will allow following things to do with your ponent:
- You can use the props with exact names, as used in Material UI.
- You can manipulate/change/transform/reshape you props passed from outside.
- If props to you wrapper ponents are passed with exactly same names as MUI is using, they will directly be sent to the inner ponent. (... operator.)
- You can use Component with exact same name as material is using to avoid confusion.
- Code is written according to advance JSX and JavaScript ES6 standards.
- You have a space to manipulate your props to pass into the MUI Components.
- You can also implement type checking using proptypes.
You can ask for any confusion/query.
You can write it like this:
const MyList = props => (
<List
{/*mention props values here*/}
propA={props.A}
propB={props.B}
>
{props.children}
</List>
)
const MyListItem = props => (
<ListItem
{/*mention props values here*/}
propA={props.A}
propB={props.B}
>
{props.children}
</ListItem>
)
Now you need to use MyList
and MyListItem
, decide the prop names for these ponent (as per your convenient), and inside these ponent map those values to actual Material-UI ponent properties.
Note:
If you are using the same prop names (same name as material-ui ponent expect) for your ponent then you can write like this also:
const MyList = ({children, ...rest}) => <div {...rest}>{children}</div>
const MyListItem = ({children, ...rest}) => <p {...rest}>{children}</p>
Check this example:
const A = props => <div>{props.children}</div>
const B = props => <p>{props.children}</p>
ReactDOM.render(
<A>
<A>
<B>Hello</B>
</A>
</A>,
document.getElementById('app')
)
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app' />