Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add doc structure and example #44

Merged
merged 42 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b2e83a6
added doc structure and example
dtassone Jul 1, 2020
fdec9a5
replace xgrid with grid as not published yet
dtassone Jul 1, 2020
d31d703
WIP working on columns mdx
dtassone Jul 2, 2020
138e920
add columns doc and demos
dtassone Jul 2, 2020
cb5305c
fix column demo doc
dtassone Jul 2, 2020
30e36eb
fix ts code markdown
dtassone Jul 3, 2020
e5f7e0f
add autoheight, rename components Grid to XGrid, add classname prop, …
dtassone Jul 3, 2020
3b81c04
fix grid-root classname, fix import React
dtassone Jul 3, 2020
facaad2
v0.1.43
dtassone Jul 3, 2020
8988297
Update packages/storybook/src/docs/columns.stories.mdx
dtassone Jul 6, 2020
0481c63
Update packages/storybook/src/docs/columns.stories.mdx
dtassone Jul 6, 2020
e2e4f3c
Rewording doc
dtassone Jul 6, 2020
5454c09
Update packages/storybook/src/docs/columns.stories.mdx
dtassone Jul 6, 2020
3c451c4
Update packages/storybook/src/docs/quick-start.stories.mdx
dtassone Jul 6, 2020
6679fc0
Update packages/storybook/src/docs/quick-start.stories.mdx
dtassone Jul 6, 2020
229bcee
Update packages/storybook/src/docs/sorting.stories.mdx
dtassone Jul 6, 2020
92c4fb7
Update packages/storybook/src/docs/columns.stories.mdx
dtassone Jul 6, 2020
f7fe535
Update packages/storybook/src/docs/quick-start.stories.mdx
dtassone Jul 6, 2020
15b3953
Update packages/storybook/src/docs/quick-start.stories.mdx
dtassone Jul 6, 2020
18820a2
Rewording: Apply suggestions from code review
dtassone Jul 6, 2020
94cc8a5
Added 3rd level titles
dtassone Jul 6, 2020
9bd1d71
Merge branch 'mdxDoc' of github.com:mui-org/material-ui-x into mdxDoc
dtassone Jul 6, 2020
4604f06
revert react import change
dtassone Jul 6, 2020
d49f1f3
testing grid with codesandbox
dtassone Jul 6, 2020
e27e804
v0.1.44
dtassone Jul 6, 2020
e53d469
revert react import change
dtassone Jul 6, 2020
8c63b1c
remove terser
dtassone Jul 6, 2020
5fa86c9
v0.1.45
dtassone Jul 6, 2020
e6ca144
refactor rowcells to avoid nested react
dtassone Jul 6, 2020
80af7fb
v0.1.46
dtassone Jul 6, 2020
efe6219
better refactor of rowcells to fix codesandbox
dtassone Jul 6, 2020
e7e5338
upgrade dependencies
dtassone Jul 6, 2020
57078be
v0.1.47
dtassone Jul 6, 2020
83dceac
add tslib devdep
dtassone Jul 6, 2020
0f2063d
v0.1.48
dtassone Jul 6, 2020
1182807
upgrade dependencies
dtassone Jul 6, 2020
402d521
v0.1.49
dtassone Jul 6, 2020
e7cb9e3
move tslib in dependencies :(
dtassone Jul 6, 2020
1d82dbd
v0.1.50
dtassone Jul 6, 2020
0d296dd
split and simplify demos
dtassone Jul 7, 2020
917b57e
Merge branch 'master' into mdxDoc
dtassone Jul 7, 2020
b372f5e
yarn and prettier
dtassone Jul 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/storybook/src/docs/api.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Grid } from '@material-ui/x-grid';

<Meta title="1. Docs/10. Api" component={Grid} />

# Api
# Interface / Api

Generated?

Expand Down
96 changes: 95 additions & 1 deletion packages/storybook/src/docs/columns.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,16 +1,110 @@
import { Grid } from '@material-ui/x-grid';
import {ColumnTypesDemo, HideColumnDemo, ValueGetterDemo, FormattingDemo} from './demos/columns.demo'

<Meta title="1. Docs/2. Columns" component={Grid} />

# Columns

After importing XGrid or DataGrid, you're now ready to use the component in your JSX, and configure your grid columns.
Todo that, you need to define the columns property of the component as below.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
If you use Typescript, you can enforce the typing and define the prop with 'Columns' or 'ColDef[]' type.

```typescript jsx
dtassone marked this conversation as resolved.
Show resolved Hide resolved
const columns: Columns = [
{
field: 'columnId', headerName: 'My Column Id', description: 'Something about this grid is amazing!', width: 100, sortable: true, type: 'number'
}
]
```
In the code above we define a column with some of the basic attributes.
- `field` is a mandatory field and is used to identify the corresponding value on rows. It's similar to an id, but has to be a string.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
- `headerName` is the title visible in the grid column header. If not set, `field` will be used in the header.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `headerName` is the title visible in the grid column header. If not set, `field` will be used in the header.
- `headerName` is the title visible in the grid column header. If not set, `field` will be used in the header.

If it's a title, should it be headerTitle instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe or just Title (remind me of tooltip title) ?
I put the same prop as AgGrid to be backward compatible, and easily try MUI grid where agGrid is already implemented 🤔
Not sure how realisable that will stand...

- `description` is the description of the column, that will be displayed in a tooltip if the column header is too small to render the full column title.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
- `width` is the width the column will be when the component has mounted
dtassone marked this conversation as resolved.
Show resolved Hide resolved
- `sortable` is an optional boolean that allows to make the column sortable.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
- `type` is the type of the column, *more on this one below*.
dtassone marked this conversation as resolved.
Show resolved Hide resolved

The full list of column option is defined in the `ColDef` interface and can be found here (TODO add link to ColDef generated doc)
dtassone marked this conversation as resolved.
Show resolved Hide resolved

## Column types

To facilitate column configuration, we've defined some column types which act as a preset of column definition.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
By default, columns are considered string and thus the default column string type will be applied.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
That means, column sorting will use the string comparator, the column content will be aligned to the left side of the cell...
dtassone marked this conversation as resolved.
Show resolved Hide resolved

We have built-in the following types
dtassone marked this conversation as resolved.
Show resolved Hide resolved
- string
- number
- date
- dateTime

To apply a column type, you just need to define the `type` property in your column definition.
```typescript jsx
const columns: Columns = [
{ field: 'name', type: 'string' },
dtassone marked this conversation as resolved.
Show resolved Hide resolved
{ field: 'age', type: 'number' },
{ field: 'dateCreated', type: 'date' },
{ field: 'lastLogin', type: 'dateTime' },
]
```
In the code above, we are defining 4 columns, and just by applying a type, each column will be sortable with the correct comparator for its type.
We also added locale formatting for date, dateTime, and number Columns.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We also added locale formatting for date, dateTime, and number Columns.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should remove this one?


<ColumnTypesDemo />
<br />

### Extending col type? Adding new col type?

## Hiding
Hiding a column is pretty straightforward, you just need to toggle the `hide` attribute of the column definition.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Hiding a column is pretty straightforward, you just need to toggle the `hide` attribute of the column definition.
Hiding a column is pretty straightforward, you just need to toggle the `hide` attribute of the column definition.

Wondering if this should be hidden?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as headerName.
That being said, usually we use show/hide

```typescript jsx
const columns: Columns = [
{ field: 'hiddenCol', hide: true },
]
```
<HideColumnDemo />

## Sizing

To define the size of a column, you can use the `width` attribute available in ColDef. You can also resize the column, using the column resize separator icon, available on the column header. If you would like to block the resizing of a column, you can do by setting the resizable prop to false.
dtassone marked this conversation as resolved.
Show resolved Hide resolved

```typescript jsx
const columns: Columns = [
{ field: 'largeCol', width: 200 },
{ field: 'unresizableCol', width: 200, resizable: false }
]
```
## Getting values
Sometimes a column might not have a corresponding value and you just want to render a combination of different field.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
To do that, you can set the `valueGetter` attribute of `ColDef` as in the example below.

```typescript jsx
const columns: Columns = [
{ field: 'firstName'},
{ field: 'lastName' },
{ field: 'fullName', valueGetter: ({data})=> `${data.firstName} ${data.lastName}` }
dtassone marked this conversation as resolved.
Show resolved Hide resolved
]
```
- `ValueGetter` is a function of type `(params: ValueGetterParams) => CellValue;`, therefore you can destructure `params` to get all the row data as in the example above.
dtassone marked this conversation as resolved.
Show resolved Hide resolved
TODO Api ref the valueGetter interface here!

<ValueGetterDemo />

## Formatting
Formatting data before rendering is a common task, it is used in our built in `date` or `dateTime` column types, to apply local formatting to dates.
dtassone marked this conversation as resolved.
Show resolved Hide resolved

```typescript jsx
const columns: Columns = [
{ field: 'date', headerName: 'Year' valueFormatter: ({value})=> value.getFullYear() }
]
```
- `valueFormatter` is a function of type `(params: ValueFormatterParams) => CellValue;`, therefore you can destructure `params` to get the column value as in the example above.
TODO Api ref the valueFormatter interface here!
dtassone marked this conversation as resolved.
Show resolved Hide resolved

<FormattingDemo />

## Styling

## Hiding

## Options

Expand Down
206 changes: 206 additions & 0 deletions packages/storybook/src/docs/demos/columns.demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import * as React from 'react';
import { Columns, Grid, RowsProp } from '@material-ui/x-grid';
import { useCallback, useMemo, useState } from 'react';
import { randomCreatedDate, randomUpdatedDate } from '@material-ui/x-grid-data-generator';
import { Button } from '@material-ui/core';
import {CellValue} from "@material-ui/x-grid-modules/dist/src";

export const ColumnTypesDemo = () => {
dtassone marked this conversation as resolved.
Show resolved Hide resolved
const columns: Columns = useMemo(
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
() => [
{ field: 'id', hide: true },
{ field: 'name', type: 'string' },
{ field: 'age', type: 'number' },
{ field: 'dateCreated', type: 'date', width: 180 },
{ field: 'lastLogin', type: 'dateTime', width: 180 },
],
[],
);

const rows: RowsProp = useMemo(
() => [
{
id: 1,
name: 'Damien',
age: 25,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 2,
name: 'Nicolas',
age: 36,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 3,
name: 'Kate',
age: 19,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 4,
name: 'Sebastien',
age: 28,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 5,
name: 'Louise',
age: 23,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
],
[],
);

//TODO use XGrid when published
return (
<div style={{ width: 800, height: 500 }}>
<Grid rows={rows} columns={columns} options={{ hideFooter: true }} />
</div>
);
};

export const HideColumnDemo = () => {
const [columns, setColumns] = useState<Columns>([
{ field: 'id', hide: true },
{ field: 'name', type: 'string' },
{ field: 'age', type: 'number' },
{ field: 'dateCreated', type: 'date', width: 180 },
{ field: 'lastLogin', type: 'dateTime', width: 180 },
]);

const rows: RowsProp = useMemo(
() => [
{
id: 1,
name: 'Damien',
age: 25,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 2,
name: 'Nicolas',
age: 36,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 3,
name: 'Kate',
age: 19,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 4,
name: 'Sebastien',
age: 28,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 5,
name: 'Louise',
age: 23,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
],
[],
);

const toggleLastLogin = useCallback(() => {
setColumns(cols =>
cols.map(col => {
if (col.field === 'lastLogin') {
col.hide = !col.hide;
}
return col;
}),
);
}, [setColumns]);

//TODO use XGrid when published
return (
<>
<Button onClick={toggleLastLogin} color={'primary'} variant={"contained"} size={"small"}>
Toggle Last Login Column
</Button>
<div style={{ width: 800, height: 500, padding:'10px 0' }}>
<Grid rows={rows} columns={columns} options={{ hideFooter: true }} />
</div>
</>
);
};

export const ValueGetterDemo = () => {
const columns = useMemo<Columns>(()=> [
{ field: 'firstName'},
{ field: 'lastName' },
{ field: 'fullName', width: 200, valueGetter: ({data})=> `${data.firstName} ${data.lastName}` }
], []);

const rows: RowsProp = useMemo(
() => [
{
id: 1,
firstName: 'Paul',
lastName: 'Kenton',
},
{
id: 2,
firstName: 'Jack',
lastName: 'Kilby',
},
{
id: 3,
firstName: 'John',
lastName: 'Napier',
},
],
[],
);

return (
<div style={{ width: 800, height: 500, padding:'10px 0' }}>
<Grid rows={rows} columns={columns} options={{ hideFooter: true }} />
</div>
);
};

export const FormattingDemo = () => {
dtassone marked this conversation as resolved.
Show resolved Hide resolved
const columns = useMemo<Columns>(()=> [
{ field: 'date', headerName: 'Year', valueFormatter: ({value}: {value: CellValue})=> (value as Date).getFullYear() }
], []);

const rows: RowsProp = useMemo(
dtassone marked this conversation as resolved.
Show resolved Hide resolved
() => [
{
id: 1,
date: new Date(1979, 0, 1),
},
{
id: 2,
date: new Date(1984, 1, 1)
},
{
id: 3,
date: new Date(1992, 2, 1)
},
],
[],
);

return (
<div style={{ width: 800, height: 500, padding:'10px 0' }}>
<Grid rows={rows} columns={columns} options={{ hideFooter: true }} />
</div>
);
};
Loading