Skip to content

Commit

Permalink
refactor: responsive grid rewrite + remove react-grid-system package
Browse files Browse the repository at this point in the history
  • Loading branch information
awesthouse committed Jun 24, 2024
1 parent 1b52856 commit f3d4176
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,45 @@
"type": "CORE:UiRecipe",
"config": {
"type": "PLUGINS:dm-core-plugins/responsive_grid/ResponsiveGridPluginConfig",
"rows": [
"views": [
{
"type": "PLUGINS:dm-core-plugins/responsive_grid/RowItem",
"columns": [
{
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnItem",
"viewConfig": {
"type": "CORE:ReferenceViewConfig",
"recipe": "aRecipeName",
"scope": "aNestedObjectWithCustomUI"
},
"size": {
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnSize",
"sm": 12,
"md": 4
}
},
{
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnItem",
"viewConfig": {
"type": "CORE:ReferenceViewConfig",
"recipe": "aRecipeName",
"scope": "aNestedObjectWithCustomUI"
},
"size": {
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnSize",
"sm": 12,
"md": 4
}
}
]
"type": "PLUGINS:dm-core-plugins/responsive_grid/GridItem",
"viewConfig": {
"type": "CORE:ReferenceViewConfig",
"recipe": "aRecipeName",
"scope": "aNestedObjectWithCustomUI"
},
"size": {
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnSize",
"sm": 12,
"md": 4
}
},
{
"type": "PLUGINS:dm-core-plugins/responsive_grid/RowItem",
"columns": [
{
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnItem",
"viewConfig": {
"type": "CORE:ReferenceViewConfig",
"recipe": "aRecipeName",
"scope": "aNestedObjectWithCustomUI"
},
"size": {
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnSize",
"sm": 12,
"md": 4
}
}
]
"type": "PLUGINS:dm-core-plugins/responsive_grid/GridItem",
"viewConfig": {
"type": "CORE:ReferenceViewConfig",
"recipe": "aRecipeName",
"scope": "aNestedObjectWithCustomUI"
},
"size": {
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnSize",
"sm": 12,
"md": 4
}
},
{
"type": "PLUGINS:dm-core-plugins/responsive_grid/GridItem",
"viewConfig": {
"type": "CORE:ReferenceViewConfig",
"recipe": "aRecipeName",
"scope": "aNestedObjectWithCustomUI"
},
"size": {
"type": "PLUGINS:dm-core-plugins/responsive_grid/ColumnSize",
"sm": 12,
"md": 4
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ColumnItem",
"name": "GridItem",
"type": "dmss://system/SIMOS/Blueprint",
"attributes": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
"attributeType": "string"
},
{
"name": "showItemBorders",
"name": "views",
"type": "CORE:BlueprintAttribute",
"attributeType": "boolean",
"default": false,
"optional": true
"attributeType": "PLUGINS:dm-core-plugins/responsive_grid/GridItem",
"dimensions": "*"
},
{
"name": "rows",
"name": "spacing",
"type": "CORE:BlueprintAttribute",
"attributeType": "PLUGINS:dm-core-plugins/responsive_grid/RowItem",
"dimensions": "*"
"attributeType": "number",
"default": 1,
"optional": true
}
]
}
18 changes: 0 additions & 18 deletions packages/dm-core-plugins/blueprints/responsive_grid/RowItem.json

This file was deleted.

3 changes: 1 addition & 2 deletions packages/dm-core-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"react-hook-form": "^7.48.2",
"react-toastify": "^9.1.3",
"ts-node": "^10.9.1",
"yaml": "^2.3.2",
"react-grid-system": "^8.1.9"
"yaml": "^2.3.2"
},
"devDependencies": {
"@biomejs/biome": "1.4.1",
Expand Down
5 changes: 4 additions & 1 deletion packages/dm-core-plugins/src/grid/GridPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export const GridPlugin = (
}

return (
<Grid className='dm-parent-plugin' {...internalConfig.size}>
<Grid
className='dm-plugin-padding dm-parent-plugin'
{...internalConfig.size}
>
<GridItems
idReference={idReference}
items={internalConfig.items}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
import { IUIPlugin, ViewCreator } from '@development-framework/dm-core'
import { Typography } from '@equinor/eds-core-react'
import React from 'react'
import { Col, Container, Row } from 'react-grid-system'
import { TCol, TGridPluginConfig, TRow } from './types'

const defaultConfig: TGridPluginConfig = {
rows: [],
}
import * as Styled from './styles'
import { TCol, TGridPluginConfig, defaultConfig } from './types'

export const ResponsiveGridPlugin = (
props: IUIPlugin & { config: TGridPluginConfig }
): React.ReactElement => {
const { config, idReference, type, onSubmit, onChange } = props
const { config, idReference, onSubmit, onChange } = props

const internalConfig: TGridPluginConfig = {
...defaultConfig,
...config,
}

return (
<Container style={{ width: '100%' }}>
{internalConfig.rows.map((row: TRow) => {
const columns = row.columns.map((col: TCol) => {
return (
<Col {...col.size}>
<div style={{ marginBottom: '20px' }}>
{col?.title && (
<Typography variant='h4'>{col.title}</Typography>
)}
<ViewCreator
idReference={idReference}
viewConfig={col.viewConfig}
onSubmit={onSubmit}
onChange={onChange}
/>
</div>
</Col>
)
})
return <Row gutterWidth={row.gutterWidth || 30}>{columns}</Row>
<Styled.Grid
className='dm-plugin-padding dm-parent-plugin'
spacing={internalConfig.spacing}
>
{internalConfig.views.map((col: TCol) => {
return (
<Styled.GridItem size={col.size}>
{col?.title && <Typography variant='h4'>{col.title}</Typography>}
<ViewCreator
idReference={idReference}
viewConfig={col.viewConfig}
onSubmit={onSubmit}
onChange={onChange}
/>
</Styled.GridItem>
)
})}
</Container>
</Styled.Grid>
)
}
39 changes: 39 additions & 0 deletions packages/dm-core-plugins/src/responsive_grid/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import styled from 'styled-components'
import { TBreakpoint, TBreakpoints, breakpoints } from './types'

const createSpacingStyles = (spacing: TBreakpoints) =>
Object.entries(spacing)
.filter((bp) => bp[0] !== 'type')
.map(
(bp) => `
@media (min-width: ${breakpoints[bp[0] as TBreakpoint]}px) {
gap: ${bp[1]}rem;
}
`
)
.join('')

const createGridItemColumnSizes = (sizes: TBreakpoints) =>
Object.entries(sizes)
.filter((bp) => bp[0] !== 'type')
.map(
(bp) => `
@media (min-width: ${breakpoints[bp[0] as TBreakpoint]}px) {
grid-column: span ${bp[1]};
}
`
)
.join('')

export const Grid = styled.div<{ spacing?: TBreakpoints }>`
display: grid;
grid-template-columns: repeat(12, 1fr);
width: 100%;
gap: 1rem;
${({ spacing }) => (spacing ? createSpacingStyles(spacing) : '')}
`

export const GridItem = styled.div<{ size: TBreakpoints }>`
grid-column: span 12;
${({ size }) => createGridItemColumnSizes(size)}
`
34 changes: 15 additions & 19 deletions packages/dm-core-plugins/src/responsive_grid/types.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { TViewConfig } from '@development-framework/dm-core'

export type TColSize = {
xs: number
sm: number
md: number
lg: number
xl: number
xxl: number
export type TBreakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'
export type TBreakpoints = Record<TBreakpoint, number>

export const breakpoints: TBreakpoints = {
xs: 576,
sm: 768,
md: 992,
lg: 1200,
xl: 1600,
xxl: 1920,
}

export type TCol = {
viewConfig: TViewConfig
size: TColSize
size: TBreakpoints
title?: string
}

export type TRow = {
columns: TCol[]
gutterWidth: number
}

export type TGridPluginConfig = {
rows: TRow[]
views: TCol[]
spacing?: TBreakpoints
}

export type TItemBorder = {
size: string
style: string
color: string
radius: string
export const defaultConfig: TGridPluginConfig = {
views: [],
}

0 comments on commit f3d4176

Please sign in to comment.