-
Notifications
You must be signed in to change notification settings - Fork 0
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
Quentin Le Caignec
committed
Aug 20, 2024
1 parent
8bfefc0
commit f5777e1
Showing
6 changed files
with
119 additions
and
24 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
12 changes: 8 additions & 4 deletions
12
packages/haring-react/src/Form/DynamicZone/DynamicZone.module.css
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,6 +1,10 @@ | ||
.container { | ||
display: flex; | ||
gap: 10px; | ||
margin-top: 20px; | ||
flex-wrap: wrap; | ||
/* WIP */ | ||
border: 1px dashed black; | ||
} | ||
|
||
.blocksContainer { | ||
} | ||
|
||
.buttonsContainer { | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/haring-react/src/Form/DynamicZone/DynamicZone.stories.tsx
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,23 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { DynamicZone as Cmp } from './DynamicZone'; | ||
|
||
const meta = { | ||
component: Cmp, | ||
tags: ['autodocs'], | ||
title: '3-custom/Form/DynamicZone', | ||
} satisfies Meta<typeof Cmp>; | ||
|
||
export default meta; | ||
type IStory = StoryObj<typeof meta>; | ||
|
||
export const DynamicZone: IStory = { | ||
args: { | ||
blocks: [ | ||
{ id: '1', value: 'initial' }, | ||
{ id: '2', value: 'initial' }, | ||
{ id: '3', value: 'initial' }, | ||
], | ||
onCreatingBlock: (_b, index) => <input key={index} />, | ||
}, | ||
}; |
56 changes: 44 additions & 12 deletions
56
packages/haring-react/src/Form/DynamicZone/DynamicZone.tsx
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,24 +1,56 @@ | ||
import type { ContainerProps, GroupProps, StackProps } from '@mantine/core'; | ||
import type { ReactElement } from 'react'; | ||
|
||
import { Stack } from '@mantine/core'; | ||
import { Container, Group, Stack } from '@mantine/core'; | ||
|
||
import classes from './DynamicZone.module.css'; | ||
import { DynamicZoneBlock } from './DynamicZoneBlock/DynamicZoneBlock'; | ||
|
||
export interface IDynamicZoneProps<Field> { | ||
fields: Field[]; | ||
onCreatingField: (field: Field, index: number) => ReactElement; | ||
export interface IBaseBlock extends Record<string, unknown> { | ||
readonly id: string; | ||
} | ||
|
||
export function DynamicZone<Field>( | ||
props: IDynamicZoneProps<Field>, | ||
export interface IDynamicZoneProps<Block extends IBaseBlock> { | ||
blocks: Block[]; | ||
blocksStackProps?: StackProps; | ||
buttonsGroupProps?: GroupProps; | ||
onCreatingBlock: (block: Block, index: number) => ReactElement; | ||
rootContainerProps?: ContainerProps; | ||
} | ||
|
||
export function DynamicZone<Block extends IBaseBlock>( | ||
props: IDynamicZoneProps<Block>, | ||
): ReactElement { | ||
const { fields, onCreatingField } = props; | ||
const { | ||
blocks, | ||
blocksStackProps, | ||
buttonsGroupProps, | ||
onCreatingBlock, | ||
rootContainerProps, | ||
} = props; | ||
return ( | ||
<div className={classes.container}> | ||
wip | ||
<Stack> | ||
{fields.map((field, index) => onCreatingField(field, index))} | ||
<Container | ||
className={classes.container} | ||
fluid | ||
p={10} | ||
{...rootContainerProps} | ||
> | ||
<Stack className={classes.blocksContainer} {...blocksStackProps}> | ||
{blocks.map((block, index) => ( | ||
<DynamicZoneBlock key={block.id}> | ||
{onCreatingBlock(block, index)} | ||
</DynamicZoneBlock> | ||
))} | ||
</Stack> | ||
</div> | ||
<Group | ||
className={classes.buttonsContainer} | ||
mt={20} | ||
{...buttonsGroupProps} | ||
> | ||
<button type="button">A</button> | ||
<button type="button">B</button> | ||
<button type="button">C</button> | ||
</Group> | ||
</Container> | ||
); | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/haring-react/src/Form/DynamicZone/DynamicZoneBlock/DynamicZoneBlock.module.css
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,4 @@ | ||
.container { | ||
/* WIP */ | ||
border: 1px red dashed; | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/haring-react/src/Form/DynamicZone/DynamicZoneBlock/DynamicZoneBlock.tsx
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,26 @@ | ||
import type { ContainerProps } from '@mantine/core'; | ||
import type { ReactElement, ReactNode } from 'react'; | ||
|
||
import { Container } from '@mantine/core'; | ||
|
||
import classes from './DynamicZoneBlock.module.css'; | ||
|
||
export interface IDynamicZoneBlockProps { | ||
children: ReactNode; | ||
containerProps?: ContainerProps; | ||
} | ||
|
||
export function DynamicZoneBlock(props: IDynamicZoneBlockProps): ReactElement { | ||
const { children, containerProps } = props; | ||
return ( | ||
<Container | ||
className={classes.container} | ||
fluid | ||
m={0} | ||
p={10} | ||
{...containerProps} | ||
> | ||
{children} | ||
</Container> | ||
); | ||
} |