Skip to content

Json Schema Form rwesponsive layouts #1316

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

Merged
merged 3 commits into from
Nov 18, 2024
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
Original file line number Diff line number Diff line change
@@ -1,60 +1,114 @@
import React from 'react';
import { Button, Row, Col } from 'antd';
import { ArrayFieldTemplateProps } from '@rjsf/utils';
import { ArrayFieldTemplateProps, getUiOptions, RJSFSchema } from '@rjsf/utils';
import { ArrowDownOutlined, ArrowUpOutlined, DeleteOutlined, PlusOutlined } from '@ant-design/icons';
import ObjectFieldTemplate from './ObjectFieldTemplate'; // Ensure this is correctly imported

const DEFAULT_RESPONSIVE_COL_SPAN = {
xs: 24,
sm: 24,
md: 12,
lg: 8,
xl: 6,
};

type UiProps = {
rowGutter?: number;
colSpan?: number | Record<string, number>;
};

const ArrayFieldTemplate = (props: ArrayFieldTemplateProps) => {
const { items, canAdd, onAddClick, title } = props;
const { items, canAdd, onAddClick, title, uiSchema, registry } = props;

return (
<fieldset>
{title && <legend>{title}</legend>}
<Row gutter={[0, 0]}>
{items.map((element: any) => (
<Col key={element.index} span={24} style={{ display: 'flex', alignItems: 'center' }}>
{/* Content container for the array item */}
<div style={{ flexGrow: 1 }}>
{element.children}
</div>
// Get UI schema configuration
const { rowGutter = 8, colSpan = DEFAULT_RESPONSIVE_COL_SPAN } = getUiOptions(uiSchema)?.["ui:props"] as UiProps || {};

{/* Container for the control buttons with vertical alignment */}
<div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'flex-end', paddingTop: "58px" }}>
{/* Move down button */}
{element.hasMoveDown && (
<Button
type="default"
icon={<ArrowDownOutlined />}
onClick={element.onReorderClick(element.index, element.index + 1)}
style={{ marginLeft: '4px' }}
/>
)}
const calculateResponsiveColSpan = (): { span: number } => {
if (typeof colSpan === 'number') {
return { span: colSpan };
} else if (typeof colSpan === 'object') {
// Return span based on screen width
const width = window.innerWidth;
if (width > 1200 && colSpan.xl !== undefined) return { span: colSpan.xl };
if (width > 992 && colSpan.lg !== undefined) return { span: colSpan.lg };
if (width > 768 && colSpan.md !== undefined) return { span: colSpan.md };
if (width > 576 && colSpan.sm !== undefined) return { span: colSpan.sm };
return { span: colSpan.xs || DEFAULT_RESPONSIVE_COL_SPAN.xs };
}
return { span: DEFAULT_RESPONSIVE_COL_SPAN.xs };
};

{/* Move up button */}
{element.hasMoveUp && (
<Button
type="default"
icon={<ArrowUpOutlined />}
onClick={element.onReorderClick(element.index, element.index - 1)}
style={{ marginLeft: '4px' }}
/>
)}
const renderItems = () => {
return items.map((element) => {
const { schema, uiSchema, formData, idSchema, name } = element.children.props;

{/* Remove button */}
{element.hasRemove && (
<Button
type="default"
icon={<DeleteOutlined />}
danger
onClick={element.onDropIndexClick(element.index)}
style={{ marginLeft: '4px' }}
/>
)}
</div>
</Col>
))}
{/* Add button for the array */}
return (
<Col key={element.index} span={24}>
{/* Use ObjectFieldTemplate to render each array item */}
<ObjectFieldTemplate
title=""
description={schema.description}
properties={[
{
content: element.children,
name,
readonly: element.children.props.readonly,
disabled: element.children.props.disabled,
hidden: false
},
]}
schema={schema}
uiSchema={uiSchema}
formData={formData}
idSchema={idSchema}
registry={registry}
readonly={element.children.props.readonly}
disabled={element.children.props.disabled}
onAddClick={function (schema: RJSFSchema): () => void {
throw new Error('Function not implemented.');
} }
/>

{/* Control buttons */}
<div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '8px' }}>
{element.hasMoveDown && (
<Button
type="default"
icon={<ArrowDownOutlined />}
onClick={element.onReorderClick(element.index, element.index + 1)}
style={{ marginLeft: '4px' }}
/>
)}
{element.hasMoveUp && (
<Button
type="default"
icon={<ArrowUpOutlined />}
onClick={element.onReorderClick(element.index, element.index - 1)}
style={{ marginLeft: '4px' }}
/>
)}
{element.hasRemove && (
<Button
type="default"
icon={<DeleteOutlined />}
danger
onClick={element.onDropIndexClick(element.index)}
style={{ marginLeft: '4px' }}
/>
)}
</div>
</Col>
);
});
};

return (
<fieldset>

<Row gutter={rowGutter}>
{renderItems()} {/* Render items */}
{canAdd && (
<Col span={24} style={{ textAlign: 'center' }}>
<Col span={24} style={{ textAlign: 'center', marginTop: '16px' }}>
<Button type="dashed" onClick={onAddClick} icon={<PlusOutlined />}>
Add Item
</Button>
Expand Down
Loading
Loading