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

GH-27 [FIX] Card Layout #37

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion src/cells/AntdCellCardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import './cell.css';

export const AntdCellCardLayout = (props: any): JSX.Element => {
const { viewKind, viewKindElement, viewDescr, viewDescrElement, schema, data, id } = props;
const { style } = viewKindElement?.options;
const createCardChilds = () =>
viewKindElement.elements
? viewKindElement.elements.map((e: IViewKindElement, idx: number) => {
Expand All @@ -38,7 +39,11 @@ export const AntdCellCardLayout = (props: any): JSX.Element => {
);
})
: null;
return <Card hoverable>{createCardChilds()}</Card>;
return (
<Card style={style} hoverable>
{createCardChilds()}
</Card>
);
};

/**
Expand Down
23 changes: 19 additions & 4 deletions src/cells/AntdSimpleCells.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import React from 'react';
import React, { useEffect, useState, useRef } from 'react';
import { Button, Image, Rate } from 'antd';

import {
Expand Down Expand Up @@ -46,10 +46,25 @@ export const AntdBooleanCellWithStore = withStoreToCellProps(AntdBooleanCell);
*/
export const AntdButtonCell = (props: any): JSX.Element => {
const options = props.uiOptions;
const [fontSize, setFontSize] = useState<number>();
const cellRef = useRef<any>(null);

useEffect(() => {
const resizeObserver = new ResizeObserver((entries) => {
setFontSize(entries[0].contentRect.width * options.relativeFont);
});

if (options.relativeFont) {
const antRow = cellRef.current.parentNode.parentNode;
resizeObserver.observe(antRow);
}

return () => resizeObserver.disconnect();
}, []);
return (
<Button size={'small'} style={options.style}>
<button ref={cellRef} style={{ fontSize, verticalAlign: 'middle', ...options.style }}>
{options.label}
</Button>
</button>
);
};
export const antdButtonCellTester: RankedTester = rankWith(2, uiTypeIs('aldkg:Button'));
Expand Down Expand Up @@ -80,7 +95,7 @@ export const AntdNumberCellWithStore = withStoreToCellProps(AntdNumberCell);
*/
export const AntdImageCell = (props: any): JSX.Element => {
const { data } = props;
return <Image style={{ height: '100%', width: '100%' }} src={data[0]} />;
return <Image width={'100%'} src={data[0]} />;
};
export const antdImageCellTester: RankedTester = rankWith(2, uiTypeIs('aldkg:ImageCell'));
export const AntdImageCellWithStore = withStoreToCellProps(AntdImageCell);
Expand Down
25 changes: 24 additions & 1 deletion src/cells/CellRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const CellRenderer: React.FC<ControlComponent & WithCell> = ({
}) => {
const [editing, setEditing] = useState(false);
const [currentData, setCurrentData] = useState(props.data);
const [fontSize, setFontSize] = useState<number>();
const inputRef = useRef<any>(null);
const cellRef = useRef<any>(null);
const formatterId = uiOptions.formatter || 'base';
const query = uiOptions.query;
let width = 'auto';
Expand Down Expand Up @@ -64,9 +66,30 @@ export const CellRenderer: React.FC<ControlComponent & WithCell> = ({
specialProps[key] = rowData[formatterProps[key]];
}
}
useEffect(() => {
const resizeObserver = new ResizeObserver((entries) => {
setFontSize(entries[0].contentRect.width * uiOptions.relativeFont);
});

if (uiOptions.relativeFont) {
const antRow = cellRef.current.parentNode.parentNode;
resizeObserver.observe(antRow);
}

return () => resizeObserver.disconnect();
}, []);
//console.log('RENDER', props.data);
return (
<div style={{ margin: '5px', display: 'block', alignSelf: 'start', width: width, ...uiOptions.style }}>
<div
ref={cellRef}
style={{
margin: '5px',
display: 'block',
alignSelf: 'start',
width: width,
fontSize,
...uiOptions.style,
}}>
{editable ? (
editing ? (
<Cell inputRef={inputRef} value={currentData} handleChange={save} {...props} />
Expand Down
9 changes: 8 additions & 1 deletion src/data-controls/GridRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const GridRenderer: React.FC<any> = (props) => {
const { viewKind, viewKindElement, viewDescr, viewDescrElement, child, schema } = props;
const grid = viewKindElement?.options?.grid || { gutter: 16, column: 4 };
const template = viewKindElement?.options?.elementTemplate || null;
const style = viewKindElement?.options?.style;

const createCell = (data: any, id: string | number) =>
template ? (
template.map((e: IViewKindElement, idx: number) => (
Expand All @@ -38,6 +40,11 @@ export const GridRenderer: React.FC<any> = (props) => {
<span key={id}>{data['@id']}</span>
);
return (
<List grid={grid} dataSource={child} renderItem={(item, idx) => <List.Item>{createCell(item, idx)}</List.Item>} />
<List
grid={grid}
style={style}
dataSource={child}
renderItem={(item, idx) => <List.Item>{createCell(item, idx)}</List.Item>}
/>
);
};
27 changes: 25 additions & 2 deletions stories/CardsGridList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,19 @@ const viewKinds = [
xl: 4,
xxl: 7,
},
style: {
minWidth: 460,
},
elementTemplate: [
{
'@id': 'mktp:_94hfT67',
'@type': 'aldkg:CardLayout',
options: {
style: {
maxWidth: 340,
minWidth: 220,
},
},
elements: [
{
'@id': 'mktp:_kje733js',
Expand Down Expand Up @@ -119,6 +128,9 @@ const viewKinds = [
textAlign: 'left',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -134,6 +146,9 @@ const viewKinds = [
textAlign: 'right',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -154,6 +169,9 @@ const viewKinds = [
textAlign: 'left',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -172,6 +190,9 @@ const viewKinds = [
textAlign: 'left',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -193,16 +214,17 @@ const viewKinds = [
options: {
style: {
border: '1.5px solid black',
padding: '0 3px 0 3px',
borderRadius: '2px',
height: '2em',
textAlign: 'center',
fontWeight: 500,
width: '90px',
color: 'black',
},
specialImage: 'https://www.meme-arsenal.com/memes/f8e9bfb9fdf368272b21a5dac8f01ec1.jpg',
editable: false,
formatter: 'link',
relativeFont: 0.06,
dataToFormatter: {
link: '@id',
},
Expand All @@ -214,11 +236,12 @@ const viewKinds = [
'@type': 'aldkg:Button',
options: {
label: 'Добавить',
relativeFont: 0.06,
style: {
border: '1.5px solid black',
borderRadius: '2px',
width: '90px',
fontWeight: 500,
backgroundColor: 'white',
color: 'black',
},
},
Expand Down
31 changes: 28 additions & 3 deletions stories/CardsHorizontalScroller.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ const viewKinds = [
elements: [
{
'@id': 'mktp:_29kFg89',
'@type': 'aldkg:VerticalLayout',
'@type': 'aldkg:PanelLayout',
options: {
style: {
width: '100%',
},
},
elements: [
{
'@id': 'mktp:_24Hdr78',
Expand All @@ -69,6 +74,12 @@ const viewKinds = [
{
'@id': 'mktp:_94hfT67',
'@type': 'aldkg:CardLayout',
options: {
style: {
maxWidth: 340,
minWidth: 220,
},
},
elements: [
{
'@id': 'mktp:_kje733js',
Expand Down Expand Up @@ -119,6 +130,9 @@ const viewKinds = [
textAlign: 'left',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -134,6 +148,9 @@ const viewKinds = [
textAlign: 'right',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -154,6 +171,9 @@ const viewKinds = [
textAlign: 'left',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -172,6 +192,9 @@ const viewKinds = [
textAlign: 'left',
fontFamily: 'Lato,Tahoma,sans-serif',
color: 'gray',
height: '1rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
},
},
Expand All @@ -189,16 +212,17 @@ const viewKinds = [
options: {
style: {
border: '1.5px solid black',
padding: '0 3px 0 3px',
borderRadius: '2px',
height: '2em',
textAlign: 'center',
fontWeight: 500,
width: '90px',
color: 'black',
},
specialImage: 'https://www.meme-arsenal.com/memes/f8e9bfb9fdf368272b21a5dac8f01ec1.jpg',
editable: false,
formatter: 'link',
relativeFont: 0.06,
dataToFormatter: {
link: '@id',
},
Expand All @@ -210,11 +234,12 @@ const viewKinds = [
'@type': 'aldkg:Button',
options: {
label: 'Добавить',
relativeFont: 0.06,
style: {
border: '1.5px solid black',
borderRadius: '2px',
width: '90px',
fontWeight: 500,
backgroundColor: 'white',
color: 'black',
},
},
Expand Down