-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `createSpaceBlock` factory to replace `SpaceBlock`. --------- Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
- Loading branch information
1 parent
d6ca50a
commit 90c6f19
Showing
15 changed files
with
238 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@comet/blocks-admin": minor | ||
"@comet/blocks-api": minor | ||
--- | ||
|
||
Deprecate `SpaceBlock` | ||
|
||
It will be replaced by the `createSpaceBlock` factory since it had no real use case. |
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,32 @@ | ||
--- | ||
"@comet/blocks-admin": minor | ||
"@comet/blocks-api": minor | ||
--- | ||
|
||
Add `createSpaceBlock` factory | ||
|
||
Allows selecting a spacing value out of a list of provided options. | ||
|
||
**Example** | ||
|
||
API | ||
|
||
```tsx | ||
enum Spacing { | ||
d150 = "d150", | ||
d200 = "d200", | ||
} | ||
|
||
export const SpaceBlock = createSpaceBlock({ spacing: Spacing }, "DemoSpace"); | ||
``` | ||
|
||
Admin | ||
|
||
```tsx | ||
const options = [ | ||
{ value: "d150", label: "Dynamic 150" }, | ||
{ value: "d200", label: "Dynamic 200" }, | ||
]; | ||
|
||
export const SpaceBlock = createSpaceBlock<string>({ defaultValue: options[0].value, options }); | ||
``` |
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,18 @@ | ||
import { createSpaceBlock } from "@comet/blocks-admin"; | ||
import * as React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
const options: { value: string; label: React.ReactNode }[] = [ | ||
{ value: "d150", label: <FormattedMessage id="spacing.d150" defaultMessage="Dynamic 150" /> }, | ||
{ value: "d200", label: <FormattedMessage id="spacing.d200" defaultMessage="Dynamic 200" /> }, | ||
{ value: "d250", label: <FormattedMessage id="spacing.d250" defaultMessage="Dynamic 250" /> }, | ||
{ value: "d300", label: <FormattedMessage id="spacing.d300" defaultMessage="Dynamic 300" /> }, | ||
{ value: "d350", label: <FormattedMessage id="spacing.d350" defaultMessage="Dynamic 350" /> }, | ||
{ value: "d400", label: <FormattedMessage id="spacing.d400" defaultMessage="Dynamic 400" /> }, | ||
{ value: "d450", label: <FormattedMessage id="spacing.d450" defaultMessage="Dynamic 450" /> }, | ||
{ value: "d500", label: <FormattedMessage id="spacing.d500" defaultMessage="Dynamic 500" /> }, | ||
{ value: "d550", label: <FormattedMessage id="spacing.d550" defaultMessage="Dynamic 550" /> }, | ||
{ value: "d600", label: <FormattedMessage id="spacing.d600" defaultMessage="Dynamic 600" /> }, | ||
]; | ||
|
||
export const SpaceBlock = createSpaceBlock<string>({ defaultValue: options[0].value, options }); |
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
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
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,16 @@ | ||
import { createSpaceBlock } from "@comet/blocks-api"; | ||
|
||
export enum Spacing { | ||
d150 = "d150", | ||
d200 = "d200", | ||
d250 = "d250", | ||
d300 = "d300", | ||
d350 = "d350", | ||
d400 = "d400", | ||
d450 = "d450", | ||
d500 = "d500", | ||
d550 = "d550", | ||
d600 = "d600", | ||
} | ||
|
||
export const SpaceBlock = createSpaceBlock({ spacing: Spacing }, "DemoSpace"); |
6 changes: 3 additions & 3 deletions
6
demo/api/src/db/fixtures/generators/blocks/space.generator.ts
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,4 +1,4 @@ | ||
import { ExtractBlockInputFactoryProps, SpaceBlock } from "@comet/blocks-api"; | ||
import faker from "faker"; | ||
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api"; | ||
import { SpaceBlock, Spacing } from "@src/common/blocks/space.block"; | ||
|
||
export const generateSpaceBlock = (): ExtractBlockInputFactoryProps<typeof SpaceBlock> => ({ height: faker.datatype.number(200) }); | ||
export const generateSpaceBlock = (): ExtractBlockInputFactoryProps<typeof SpaceBlock> => ({ spacing: Spacing.d200 }); |
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
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,9 +1,22 @@ | ||
import { PropsWithData, withPreview } from "@comet/cms-site"; | ||
import { SpaceBlockData } from "@src/blocks.generated"; | ||
import { DemoSpaceBlockData } from "@src/blocks.generated"; | ||
import * as React from "react"; | ||
|
||
const SpaceBlock: React.FunctionComponent<PropsWithData<SpaceBlockData>> = ({ data: { height } }) => { | ||
return <div style={{ height: `${height}px` }} />; | ||
const SpaceMapping: Record<string, number> = { | ||
d150: 10, | ||
d200: 20, | ||
d250: 40, | ||
d300: 60, | ||
d350: 80, | ||
d400: 100, | ||
d450: 150, | ||
d500: 200, | ||
d550: 250, | ||
d600: 300, | ||
}; | ||
|
||
const SpaceBlock: React.FC<PropsWithData<DemoSpaceBlockData>> = ({ data: { spacing } }) => { | ||
return <div style={{ height: `${SpaceMapping[spacing]}px` }} />; | ||
}; | ||
|
||
export default withPreview(SpaceBlock, { label: "Abstand" }); |
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
55 changes: 55 additions & 0 deletions
55
packages/admin/blocks-admin/src/blocks/factories/spaceBlock/createSpaceBlock.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,55 @@ | ||
import { SelectField } from "@comet/admin"; | ||
import { MenuItem } from "@mui/material"; | ||
import * as React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import { BlocksFinalForm } from "../../../form/BlocksFinalForm"; | ||
import { SelectPreviewComponent } from "../../../iframebridge/SelectPreviewComponent"; | ||
import { createBlockSkeleton } from "../../helpers/createBlockSkeleton"; | ||
import { BlockCategory, BlockInterface } from "../../types"; | ||
|
||
export interface SpaceBlockFactoryOptions<T> { | ||
name?: string; | ||
defaultValue: T; | ||
options: { value: T; label: React.ReactNode }[]; | ||
} | ||
|
||
export const createSpaceBlock = <T extends string | number>({ | ||
name = "Space", | ||
defaultValue, | ||
options, | ||
}: SpaceBlockFactoryOptions<T>): BlockInterface => { | ||
const SpaceBlock: BlockInterface<{ spacing: T }, { spacing: T }, { spacing: T }> = { | ||
...createBlockSkeleton(), | ||
|
||
name, | ||
|
||
displayName: <FormattedMessage id="comet.blocks.space" defaultMessage="Space" />, | ||
|
||
category: BlockCategory.Layout, | ||
|
||
defaultValues: () => ({ spacing: defaultValue }), | ||
|
||
AdminComponent: ({ state, updateState }) => { | ||
return ( | ||
<SelectPreviewComponent> | ||
<BlocksFinalForm<{ spacing: T }> onSubmit={updateState} initialValues={state}> | ||
<SelectField name="spacing" fullWidth> | ||
{options.map(({ value, label }) => ( | ||
<MenuItem key={value} value={value}> | ||
{label} | ||
</MenuItem> | ||
))} | ||
</SelectField> | ||
</BlocksFinalForm> | ||
</SelectPreviewComponent> | ||
); | ||
}, | ||
|
||
previewContent: ({ spacing }) => { | ||
return [{ type: "text", content: options.find((option) => option.value === spacing)?.label }]; | ||
}, | ||
}; | ||
|
||
return SpaceBlock; | ||
}; |
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
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
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,39 @@ | ||
import { IsEnum } from "class-validator"; | ||
|
||
import { Block, BlockData, BlockDataInterface, BlockInput, createBlock, inputToData, SimpleBlockInputInterface } from "./block"; | ||
import { BlockField } from "./decorators/field"; | ||
import { BlockFactoryNameOrOptions } from "./factories/types"; | ||
|
||
interface CreateSpaceBlockOptions<SpacingOptions extends string[] | Record<string, string>> { | ||
spacing: SpacingOptions; | ||
} | ||
|
||
type SingleEnumType<SpacingOptions extends string[] | Record<string, string>> = SpacingOptions extends string[] | ||
? SpacingOptions[number] | ||
: keyof SpacingOptions; | ||
|
||
interface SpaceBlockInputInterface<SpacingOptions extends string[] | Record<string, string>> extends SimpleBlockInputInterface { | ||
spacing: SingleEnumType<SpacingOptions>; | ||
} | ||
|
||
export function createSpaceBlock<SpacingOptions extends string[] | Record<string, string>>( | ||
{ spacing: Spacing }: CreateSpaceBlockOptions<SpacingOptions>, | ||
nameOrOptions: BlockFactoryNameOrOptions = "Space", | ||
): Block<BlockDataInterface, SpaceBlockInputInterface<SpacingOptions>> { | ||
class SpaceBlockData extends BlockData { | ||
@BlockField({ type: "enum", enum: Spacing }) | ||
spacing: SingleEnumType<SpacingOptions>; | ||
} | ||
|
||
class SpaceBlockInput extends BlockInput { | ||
@IsEnum(Spacing) | ||
@BlockField({ type: "enum", enum: Spacing }) | ||
spacing: SingleEnumType<SpacingOptions>; | ||
|
||
transformToBlockData(): SpaceBlockData { | ||
return inputToData(SpaceBlockData, this); | ||
} | ||
} | ||
|
||
return createBlock(SpaceBlockData, SpaceBlockInput, nameOrOptions); | ||
} |
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