|
1 | | -import type { FC, ReactNode } from 'react'; |
2 | | -import toJsxString from 'react-element-to-jsx-string'; |
3 | | -import { type Block, type Node, Parser, Renderer } from '../../renderer'; |
4 | | -import { getClassNameFactory } from '../../utils'; |
5 | | -import { TargetType } from '../Constants'; |
6 | | -import { ContentScaler } from '../ContentScaler'; |
7 | | -import { Draggable } from '../Draggable'; |
| 1 | +import type { FC } from 'react'; |
| 2 | +import { Text } from '../../preset'; |
| 3 | +import type { Block } from '../../renderer'; |
| 4 | +import { BlockItem } from '../BlockItem'; |
8 | 5 | import styles from './BlockGroup.module.css'; |
9 | 6 |
|
10 | | -const getClassName = getClassNameFactory('BlockGroup', styles); |
11 | | - |
12 | 7 | type Props = { |
13 | 8 | category: string; |
14 | 9 | blocks: Block[]; |
15 | 10 | }; |
16 | 11 |
|
17 | 12 | export const BlockGroup: FC<Props> = ({ category, blocks }) => ( |
18 | | - <div className={getClassName()}> |
19 | | - <h4 className={getClassName('Category')}>{category}</h4> |
20 | | - <div className={getClassName('BlockList')}> |
21 | | - {blocks.map((block) => { |
22 | | - const propertySpecs = block.props ?? {}; |
23 | | - |
24 | | - const defaultProps = Object.entries(propertySpecs).reduce( |
25 | | - (acc, [key, value]) => { |
26 | | - if (value?.hasDefault) { |
27 | | - acc[key] = |
28 | | - propertySpecs[key].type === 'node' |
29 | | - ? Parser.parse(toJsxString(value.default)) |
30 | | - : value.default; |
31 | | - } |
32 | | - |
33 | | - return acc; |
34 | | - }, |
35 | | - {} as Record<string, unknown>, |
36 | | - ); |
37 | | - |
38 | | - const children = [defaultProps.children] |
39 | | - .flat() |
40 | | - .filter((child) => typeof child !== 'undefined') as ReactNode; |
41 | | - |
42 | | - const node = { |
43 | | - __composify__: true, |
44 | | - type: block.name, |
45 | | - props: defaultProps, |
46 | | - children, |
47 | | - } as Node; |
48 | | - |
49 | | - const jsxProps = Object.entries(defaultProps) |
50 | | - .map(([key, value]) => `${key}={${JSON.stringify(value)}}`) |
51 | | - .join(' '); |
52 | | - |
53 | | - return ( |
54 | | - <div key={block.name} className={getClassName('BlockItem')}> |
55 | | - <Draggable |
56 | | - type={TargetType.Library} |
57 | | - item={{ |
58 | | - ...node, |
59 | | - props: defaultProps, |
60 | | - }} |
61 | | - > |
62 | | - <div className={getClassName('BlockItemPreview')}> |
63 | | - <ContentScaler width={1024} height={1024}> |
64 | | - <Renderer source={`<${block.name} ${jsxProps} />`} /> |
65 | | - </ContentScaler> |
66 | | - </div> |
67 | | - </Draggable> |
68 | | - <p className={getClassName('BlockName')}>{block.name}</p> |
69 | | - </div> |
70 | | - ); |
71 | | - })} |
| 13 | + <div className={styles.container}> |
| 14 | + <Text size="xs" weight="medium" color="on-surface" className={styles.category}> |
| 15 | + {category} |
| 16 | + </Text> |
| 17 | + <div className={styles.grid}> |
| 18 | + {blocks.map((block) => ( |
| 19 | + <BlockItem key={block.name} block={block} /> |
| 20 | + ))} |
72 | 21 | </div> |
73 | 22 | </div> |
74 | 23 | ); |
0 commit comments