MUI x-data-grid documentation example never works as expected - javascript

I am implementing the exact same code shown in the material-ui library documentation here
https://mui.com/x/react-data-grid/layout/#flex-layout
I am trying to implement the flex layout,
I just did the exact same steps and the console shows the error all the time:
Sandbox:
https://stackblitz.com/edit/react-8ahs3n?file=src/App.js
Please help, appreciated.

In the documentation example you linked to there is one additional div wrapping what you have in your code: <div style={{ height: 400, width: '100%' }}>. Including that wrapper element (which gives the element an intrinsic height as mentioned in the console warning) gets rid of the console error. Here's a modified version of your stackblitz that does not have the error: https://stackblitz.com/edit/react-m2uoq1?file=src%2FApp.js.
The 400px height in the documentation example can be replaced by whatever height you want -- it just can't be percent-based. If you want the DataGrid to be the full height of the browser, you can use 100vh instead of 100%.
Here's an example:
import * as React from "react";
import { DataGrid } from "#mui/x-data-grid";
import { useDemoData } from "#mui/x-data-grid-generator";
import CssBaseline from "#mui/material/CssBaseline";
export default function FlexLayoutGrid() {
const { data } = useDemoData({
dataSet: "Commodity",
rowLength: 50,
maxColumns: 6
});
return (
<>
<CssBaseline />
<div style={{ height: "100vh", width: "100%" }}>
<div style={{ display: "flex", height: "100%" }}>
<div style={{ flexGrow: 1 }}>
<DataGrid {...data} />
</div>
</div>
</div>
</>
);
}
In my example I included the CssBaseline component in order to get rid of the default 8px margin on the <body> element -- otherwise using 100vh will cause a scroll bar to appear; however the default margin (or a custom margin in your app) can be accounted for in other ways by using calc (e.g. height: "calc(100vh - 16px)").

Use a wrapper component such as Box from Material UI with flex display and height:
<div style={{ height: 400, width: '100%' }}>
<Box style={{
display: "flex",
height:400,
justifyContent: "center",
alignItems: "center"
}}
>
<Datagrid/>
</Box>
</div>

Related

How to make material-ui data-grid Toolbar and table colums header scrollable?

i want to scroll horizontal my Toolbar items when i scroll horizontal my datagrid table items. right now they are scrollable but they are separate. i want if i scroll toolbar items then datagrid items should scroll horizontal. if i scroll datagrid items then toolbar items should scroll too.
please check code sandbox link.. Thanks
https://codesandbox.io/s/69495064-is-there-a-chance-to-change-toolbar-names-material-ui-datagrid-forked-6kk5yh?file=/demo.tsx
i am also pasting the code here too
import * as React from "react";
import { DataGrid } from "#mui/x-data-grid";
import { useDemoData } from "#mui/x-data-grid-generator";
function CustomToolbar() {
return (
<div style={{ display: "flex", overflow: "auto" }}>
<p style={{ minWidth: "30%" }}>Course Name1:</p>
<p style={{ minWidth: "30%" }}>Course Name2:</p>
<p style={{ minWidth: "30%" }}>Course Name3:</p>
<p style={{ minWidth: "30%" }}>Course Name4:</p>
<p style={{ minWidth: "30%" }}>Course Name5:</p>
<p style={{ minWidth: "30%" }}>Course Name6:</p>
</div>
);
}
export default function CustomToolbarGrid() {
const { data } = useDemoData({
dataSet: "Commodity",
rowLength: 10,
maxColumns: 10
});
return (
<div style={{ height: 400, width: "100%" }}>
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar
}}
/>
</div>
);
}
incase if someone face this same issue i fix this without using Toolbar . now i am using renderHeader. so i pass all texts there .

ScrollView: Problems with height and scrolling

I have a <ScrollView />. In the documentation it says
Keep in mind that ScrollViews must have a bounded height in order to work, since they contain unbounded-height children into a bounded container (via a scroll interaction). In order to bound the height of a ScrollView, either set the height of the view directly (discouraged) or make sure all parent views have bounded height.
This is my markup:
<View
style={{
paddingLeft: 15,
paddingRight: 15,
width: '100%',
height: Dimensions.get('window').height,
alignItems: 'center'
}}>
<Foo /> // Custom Component with specific height
<ScrollView
style={{width: '100%'}}
...
{foo.map((obj, index) => {
return <View
key={index}
style={{
display: 'flex',
width: '100%',
paddingTop: 2,
paddingBottom: 2,
}}
...
</View>
})
}
</ScrollView >
</View>
In my markup, I have set the height of the parent <View /> with Dimensions.get.... But the <ScrollView /> is not scrolling all the way to the bottom.
Why is the <ScrollView /> not behaving as expected? I want to be able to scroll all the way down and see all the elements.
make shure ScrollView has flex: 1 set in style props.
Forgetting to transfer {flex: 1} down the view stack can lead to errors here, which the element inspector makes quick to debug.

images always getting smaller

I want to add images to my web site homepage but when i added it always getting smaller how can i cover my page with images?
Here is my code:
const Home = () => {
return (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "90vh",
}}
>
<img src={logo} alt="img" />
</div>
);
};
I tried object-fit:cover and width:100% but it doesnt work.
All images that i add seems like that:
Add style={{display: 'block', height: '100%'}} to the image and it will work.
By default <img> elements have display: inline. It doesn't matter how tall the parent is, it will try to adjust to the width of the parent which, in your case, is shrinked by the centering method.
i think that
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "90vh",
is breaking it, try to add width/height to your image

How does Codesandbox implement resizable grid of components?

How does Codesandbox implement resizable windows. I want to implement a user resizable grid in React just like VS-Code or CodeSandbox allows to.
Can someone point me in the right direction?
Edit: I found a solution for this, it was to use the react-simple-resizer npm module
Check out its docs they'll be able to help you.
Here's the example that they have:
import React from 'react';
import { Container, Section, Bar } from 'react-simple-resizer';
export default () => (
<Container style={{ height: '500px' }}>
<Section style={{ background: '#d3d3d3' }} minSize={100}/>
<Bar size={10} style={{ background: '#888888', cursor: 'col-resize' }} />
<Section style={{ background: '#d3d3d3' }} minSize={100} />
</Container>
);

Why does React Native not offer a justify-self

I want to align an item in the primary axis. For example, I want to have a row with a few children all aligned left, and then one child aligned on the right side.
You could achieve that effect with something like "position: absolute; right: 0", but I'm wondering if theres a better way. It seems like there ought to be a justifySelf property, that only affects one child and affects its alignment on the primary axis, in the same way the alignSelf affects one child's alignment on the secondary axis.
Yet no such justifySelf seems to exist. Why is this?
It's similar to this question but not quite: How can you float: right in React Native?
I don't know React Native, but I do know flexbox!
Use the following code as a guide:
<div style="display: flex;">
<div>
I'll be on the left side
</div>
<div>
I'll be hugging the guy on the left side
</div>
<div>
I'll be hugging the guy hugging the guy on the left side
</div>
<div style="margin-left: auto;">
I'll be hugging the right side far away from those other guys
</div>
</div>
The margin set on the last child will push all other children to the left as far as their styles will allow, and push itself as far right as any other styles will allow.
You can test this out by also adding margin-right: auto; to the last child, and you will see the last child centered perfectly in the remaining space of the parent div, after the first three children take up their allotted space. This is because the competing "margin autos" will both share equally whatever space remains, since they can't cancel each other out and won't override each other.
Flex box was designed to handle margin spacing like this, so take advantage of it, as well as the other unique spacing options available under the justify-content property.
Helpful article: https://hackernoon.com/flexbox-s-best-kept-secret-bd3d892826b6
I believe you want to achieve something like this:
You can implement this by nesting views which share the same justifyContent property.
<View style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'space-between',
}}>
<View>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
</View>
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
</View>
A partial answer: there's no justifySelf in React Native because there's no justify-self for flexbox in real CSS. There is a justify-self CSS property, but it doesn't do anything in flexbox layouts. You can see in the spec at https://drafts.csswg.org/css-align-3/#overview that justify-self is defined to apply to:
block-level boxes, absolutely-positioned boxes, and grid items
which notably doesn't include "flex items".
Okay, but why is this the case in CSS? MDN offers an explanation that you may or may not find satisfactory:
There is no justify-self in Flexbox
On the main axis Flexbox deals with our content as a group. The amount of space required to lay out the items is calculated, and the leftover space is then available for distribution. The justify-content property controls how that leftover space is used. Set justify-content: flex-end and the extra space is placed before the items, justify-content: space-around and it is placed either side of the item in that dimension, etc.
This means that a justify-self property does not make sense in Flexbox as we are always dealing with moving the entire group of items around.
On the cross axis align-self makes sense as we potentially have additional space in the flex container in that dimension, in which a single item can be moved to the start and end.
There's clearly some sense to this. It's always meaningful and coherent to put alignSelf: 'center' on a flex item to ask for it to be centered on the cross axis. But what would it mean to, for instance, put justifySelf: 'center' on a flex item, to ask for it to be centered on the main axis? How is that supposed to be handled if previous flex items have already filled more than half of the space along that axis?
Margins provide a fine solution for cases like yours. justifySelf, on the other hand, can't reasonably exist for flexbox, because the justifyContent values specify how to distribute flex items, and a little thought about what it would mean to instead apply them to individually specified items reveals that doing so is basically incoherent.
There is an easy way to do this without absolute positioning that works exactly how you'd expect with all items of varying heights lining up on the y-axis appropriately.
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ backgroundColor: 'green', height: 50, width: 50 }} />
<Text style={{flex: 1}}> text in here because why not!</Text>
<View style={{ backgroundColor: 'yellow', width: 30, height: 30 }} />
</View>
You can take a look at Flex Docs!
Adding flexDirection to a component's style determines the primary axis of its layout.
and then:
Adding alignItems to a component's style determines the alignment of children along the secondary axis (if the primary axis is row, then the secondary is column, and vice versa).
So your desired code will be:
import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native';
export default class AlignItemsBasics extends Component {
render() {
return (
<View style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
}}>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
</View>
);
}
};
// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => AlignItemsBasics);
UPDATE
If you mean something like this image:
Then I'll suggest you this:
import React, { Component } from "react";
import { View, StyleSheet } from "react-native";
class Playground extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.boxes} />
<View style={styles.boxes} />
<View
style={[
styles.boxes,
{
backgroundColor: "crimson",
position: "absolute",
right: 0
}
]}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center"
},
boxes: {
width: 50,
height: 50,
marginLeft: 1, // to separate each box!
backgroundColor: "steelblue"
}
});
export default Playground;
As far as i know with these props, it's the best way!
I really think the best way around this is to wrap whatever you're displaying inside a <View> with position: "absolute", and also add position: "absolute" to the element you want. Then, inside the <View>, add justifyContent: "center", and alignItems: "center". Hopefully this solves the issue.

Categories

Resources