-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
175 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"name": "data-grid", | ||
"version": "0.4.3", | ||
"description": "Grid for displaying datasets" | ||
"description": "Grid for displaying datasets", | ||
"luna": { | ||
"react": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { FC, useEffect, useRef } from 'react' | ||
import DataGrid, { IColumn } from './index' | ||
import types from 'licia/types' | ||
import each from 'licia/each' | ||
|
||
interface IDataGridProps { | ||
columns: IColumn[] | ||
data?: any[] | ||
height?: number | ||
maxHeight?: number | ||
minHeight?: number | ||
filter?: string | RegExp | types.AnyFn | ||
} | ||
|
||
const LunaDataGrid: FC<IDataGridProps> = (props) => { | ||
const dataGridRef = useRef<HTMLDivElement>(null) | ||
const dataGrid = useRef<DataGrid>() | ||
|
||
useEffect(() => { | ||
dataGrid.current = new DataGrid(dataGridRef.current!, { | ||
columns: props.columns, | ||
height: props.height, | ||
maxHeight: props.maxHeight, | ||
minHeight: props.minHeight, | ||
filter: props.filter, | ||
}) | ||
setData(dataGrid, props.data) | ||
}, []) | ||
|
||
useEffect(() => setData(dataGrid, props.data), [props.data]) | ||
useEffect(() => setOption(dataGrid, 'height', props.height), [props.height]) | ||
useEffect( | ||
() => setOption(dataGrid, 'maxHeight', props.maxHeight), | ||
[props.maxHeight] | ||
) | ||
useEffect( | ||
() => setOption(dataGrid, 'minHeight', props.minHeight), | ||
[props.minHeight] | ||
) | ||
useEffect(() => setOption(dataGrid, 'filter', props.filter), [props.filter]) | ||
|
||
return <div ref={dataGridRef}></div> | ||
} | ||
|
||
function setData( | ||
dataGrid: React.MutableRefObject<DataGrid | undefined>, | ||
data: any | ||
) { | ||
if (dataGrid.current) { | ||
dataGrid.current.clear() | ||
each(data, (item: any) => | ||
dataGrid.current?.append(item, { selectable: true }) | ||
) | ||
} | ||
} | ||
|
||
function setOption( | ||
dataGrid: React.MutableRefObject<DataGrid | undefined>, | ||
name: string, | ||
val: any | ||
) { | ||
if (dataGrid.current) { | ||
dataGrid.current.setOption(name, val) | ||
} | ||
} | ||
|
||
export default LunaDataGrid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters