Skip to content

Commit

Permalink
feat: wip DynamicZoneBlock opened icon, state, FormDynamicZone onToggle
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinLeCaignec committed Aug 20, 2024
1 parent f1f7958 commit ddb325b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useFieldArray, useFormContext, useWatch } from 'react-hook-form';

interface IFormBlock extends IBaseBlock {
fieldName: string;
type: string;
value?: string;
}

Expand All @@ -20,7 +19,7 @@ export function FormDynamicZone(props: IFormDynamicZoneProps): ReactElement {

const { control, register, handleSubmit } =
useFormContext<Record<typeof dynamicZoneName, Omit<IFormBlock, 'id'>[]>>();
const { fields, append, remove, swap } = useFieldArray({
const { fields, append, remove, swap, update } = useFieldArray({
control,
name: dynamicZoneName,
});
Expand All @@ -35,9 +34,14 @@ export function FormDynamicZone(props: IFormDynamicZoneProps): ReactElement {
);
}

function onToggle(block: IFormBlock, index: number, opened: boolean): void {
update(index, { ...block, opened });
}

function onAppend(blockType: string): void {
append({
fieldName: `${blockType}-${fields.length}`,
opened: false,
type: blockType,
value: '',
});
Expand Down Expand Up @@ -68,6 +72,7 @@ export function FormDynamicZone(props: IFormDynamicZoneProps): ReactElement {
blocks={fields as IFormBlock[]}
onAppendBlock={onAppend}
onRenderBlockContent={newBlock}
onToggleBlock={onToggle}
/>
<input type="submit" />
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export const DynamicZone: IStory = {
args: {
blockOptions: ['default'],
blocks: [
{ id: '1', opened: false, value: 'initial' },
{ id: '2', opened: true, value: 'initial' },
{ id: '3', opened: false, value: 'initial' },
{ id: '1', opened: false, type: 'default', value: 'initial' },
{ id: '2', opened: true, type: 'default', value: 'initial' },
{ id: '3', opened: false, type: 'default', value: 'initial' },
],
onAppendBlock: action('onAppendBlock, type'),
onRenderBlockContent: (_b, index) => <input key={index} />,
Expand Down
4 changes: 2 additions & 2 deletions packages/haring-react/src/Form/DynamicZone/DynamicZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface IDynamicZoneProps<Block extends IBaseBlock> {
buttonsGroupProps?: GroupProps;
onAppendBlock: (blockType: string) => void;
onRenderBlockContent: (block: Block, index: number) => ReactElement;
onToggleBlock: (opened: boolean) => void;
onToggleBlock: (block: Block, index: number, opened: boolean) => void;
rootContainerProps?: ContainerProps;
}

Expand Down Expand Up @@ -63,7 +63,7 @@ export function DynamicZone<Block extends IBaseBlock>(
cardSectionProps={blockCardProps?.cardSectionProps}
{...blockCardProps?.cardProps}
key={block.id}
onToggle={onToggleBlock}
onToggle={(opened) => onToggleBlock(block, index, opened)}
opened={block.opened}
>
{onRenderBlockContent(block, index)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

.header {
cursor: pointer;
user-select: none;

&:hover {
filter: contrast(40%);
Expand Down
1 change: 1 addition & 0 deletions packages/haring-react/src/types/dynamic-zone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface IBaseBlock extends Record<string, unknown> {
id: string;
opened: boolean;
type: string;
}

0 comments on commit ddb325b

Please sign in to comment.