@measured/puck 0.11.0-canary.6145c32 → 0.11.0-canary.7f13efc

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -8,100 +8,142 @@ type ItemSelector = {
8
8
  zone?: string;
9
9
  };
10
10
 
11
- type Adaptor<AdaptorParams = {}> = {
12
- name: string;
13
- fetchList: (adaptorParams?: AdaptorParams) => Promise<Record<string, any>[] | null>;
14
- };
15
- type WithId<T> = T & {
11
+ type WithPuckProps<Props> = Props & {
16
12
  id: string;
17
13
  };
18
- type Field<Props extends {
14
+ type BaseField = {
15
+ label?: string;
16
+ };
17
+ type TextField = BaseField & {
18
+ type: "text" | "number" | "textarea";
19
+ };
20
+ type SelectField = BaseField & {
21
+ type: "select" | "radio";
22
+ options: {
23
+ label: string;
24
+ value: string | number | boolean;
25
+ }[];
26
+ };
27
+ type ArrayField<Props extends {
19
28
  [key: string]: any;
20
29
  } = {
21
30
  [key: string]: any;
22
- }> = {
23
- type: "text" | "textarea" | "number" | "select" | "array" | "external" | "radio" | "custom";
24
- label?: string;
25
- adaptor?: Adaptor;
26
- adaptorParams?: object;
27
- arrayFields?: {
28
- [SubPropName in keyof Props]: Field<Props[SubPropName][0]>;
31
+ }> = BaseField & {
32
+ type: "array";
33
+ arrayFields: {
34
+ [SubPropName in keyof Props[0]]: Field<Props[0][SubPropName]>;
29
35
  };
30
- getItemSummary?: (item: Props, index?: number) => string;
31
- defaultItemProps?: Props;
32
- render?: (props: {
33
- field: Field;
36
+ defaultItemProps?: Props[0];
37
+ getItemSummary?: (item: Props[0], index?: number) => string;
38
+ };
39
+ type ExternalField<Props extends {
40
+ [key: string]: any;
41
+ } = {
42
+ [key: string]: any;
43
+ }> = BaseField & {
44
+ type: "external";
45
+ placeholder?: string;
46
+ fetchList: () => Promise<any[] | null>;
47
+ mapProp?: (value: any) => Props;
48
+ getItemSummary: (item: Props, index?: number) => string;
49
+ };
50
+ type CustomField<Props extends {
51
+ [key: string]: any;
52
+ } = {
53
+ [key: string]: any;
54
+ }> = BaseField & {
55
+ type: "custom";
56
+ render: (props: {
57
+ field: CustomField;
34
58
  name: string;
35
59
  value: any;
36
- onChange: (value: any) => void;
60
+ onChange: (value: Props) => void;
37
61
  readOnly?: boolean;
38
62
  }) => ReactElement;
39
- options?: {
40
- label: string;
41
- value: string | number | boolean;
42
- }[];
43
63
  };
64
+ type Field<Props extends {
65
+ [key: string]: any;
66
+ } = {
67
+ [key: string]: any;
68
+ }> = TextField | SelectField | ArrayField<Props> | ExternalField<Props> | CustomField;
44
69
  type DefaultRootProps = {
45
- children: ReactNode;
46
- title: string;
47
- editMode: boolean;
70
+ title?: string;
48
71
  [key: string]: any;
49
72
  };
73
+ type DefaultRootRenderProps = {
74
+ editMode: boolean;
75
+ } & DefaultRootProps;
50
76
  type DefaultComponentProps = {
51
77
  [key: string]: any;
52
78
  editMode?: boolean;
53
79
  };
54
80
  type Fields<ComponentProps extends DefaultComponentProps = DefaultComponentProps> = {
55
- [PropName in keyof Omit<Required<ComponentProps>, "children" | "editMode">]: Field<ComponentProps[PropName][0]>;
81
+ [PropName in keyof Omit<Required<ComponentProps>, "children" | "editMode">]: Field<ComponentProps[PropName]>;
56
82
  };
57
83
  type Content<Props extends {
58
84
  [key: string]: any;
59
85
  } = {
60
86
  [key: string]: any;
61
- }> = MappedItem<Props>[];
62
- type ComponentConfig<ComponentProps extends DefaultComponentProps = DefaultComponentProps, DefaultProps = ComponentProps> = {
63
- render: (props: WithId<ComponentProps>) => ReactElement;
87
+ }> = ComponentData<Props>[];
88
+ type ComponentConfig<ComponentProps extends DefaultComponentProps = DefaultComponentProps, DefaultProps = ComponentProps, DataShape = ComponentData<ComponentProps>> = {
89
+ render: (props: WithPuckProps<ComponentProps>) => ReactElement;
64
90
  defaultProps?: DefaultProps;
65
91
  fields?: Fields<ComponentProps>;
92
+ resolveData?: (data: DataShape, params: {
93
+ changed: Partial<Record<keyof ComponentProps, boolean>>;
94
+ }) => Promise<Partial<ComponentDataWithOptionalProps<ComponentProps>>> | Partial<ComponentDataWithOptionalProps<ComponentProps>>;
95
+ };
96
+ type Category<ComponentName> = {
97
+ components?: ComponentName[];
98
+ title?: string;
99
+ visible?: boolean;
100
+ defaultExpanded?: boolean;
66
101
  };
67
102
  type Config<Props extends {
68
103
  [key: string]: any;
69
104
  } = {
70
105
  [key: string]: any;
71
- }, RootProps extends DefaultRootProps = DefaultRootProps> = {
106
+ }, RootProps extends DefaultRootProps = DefaultRootProps, CategoryName extends string = string> = {
107
+ categories?: Record<CategoryName, Category<keyof Props>> & {
108
+ other?: Category<Props>;
109
+ };
72
110
  components: {
73
- [ComponentName in keyof Props]: ComponentConfig<Props[ComponentName], Props[ComponentName]>;
111
+ [ComponentName in keyof Props]: Omit<ComponentConfig<Props[ComponentName], Props[ComponentName]>, "type">;
74
112
  };
75
- root?: ComponentConfig<RootProps & {
113
+ root?: Partial<ComponentConfig<RootProps & {
76
114
  children: ReactNode;
77
115
  }, Partial<RootProps & {
78
116
  children: ReactNode;
79
- }>>;
117
+ }>, RootDataWithProps<RootProps>>>;
80
118
  };
81
- type MappedItem<Props extends {
119
+ type BaseData<Props extends {
82
120
  [key: string]: any;
83
121
  } = {
84
122
  [key: string]: any;
85
123
  }> = {
124
+ readOnly?: Partial<Record<keyof Props, boolean>>;
125
+ };
126
+ type ComponentData<Props extends DefaultComponentProps = DefaultComponentProps> = {
86
127
  type: keyof Props;
87
- props: WithId<{
88
- [key: string]: any;
89
- }>;
128
+ props: WithPuckProps<Props>;
129
+ } & BaseData<Props>;
130
+ type RootDataWithProps<Props extends DefaultRootProps = DefaultRootProps> = {
131
+ props: Props;
90
132
  };
91
- type Data<Props extends {
92
- [key: string]: any;
93
- } = {
94
- [key: string]: any;
95
- }, RootProps extends {
96
- title: string;
133
+ type RootDataWithoutProps<Props extends DefaultRootProps = DefaultRootProps> = Props;
134
+ type RootData<Props extends DefaultRootProps = DefaultRootProps> = BaseData<Props> & Partial<RootDataWithProps<Props>> & Partial<RootDataWithoutProps<Props>>;
135
+ type ComponentDataWithOptionalProps<Props extends {
97
136
  [key: string]: any;
98
137
  } = {
99
- title: string;
100
138
  [key: string]: any;
101
- }> = {
102
- root: RootProps;
103
- content: Content<Props>;
104
- zones?: Record<string, Content<Props>>;
139
+ }> = Omit<ComponentData, "props"> & {
140
+ props: Partial<WithPuckProps<Props>>;
141
+ };
142
+ type MappedItem = ComponentData;
143
+ type Data<Props extends DefaultComponentProps = DefaultComponentProps, RootProps extends DefaultRootProps = DefaultRootProps> = {
144
+ root: RootData<RootProps>;
145
+ content: Content<WithPuckProps<Props>>;
146
+ zones?: Record<string, Content<WithPuckProps<Props>>>;
105
147
  };
106
148
  type ItemWithId = {
107
149
  _arrayId: string;
@@ -115,6 +157,12 @@ type UiState = {
115
157
  leftSideBarVisible: boolean;
116
158
  itemSelector: ItemSelector | null;
117
159
  arrayState: Record<string, ArrayState | undefined>;
160
+ componentList: Record<string, {
161
+ components?: string[];
162
+ title?: string;
163
+ visible?: boolean;
164
+ expanded?: boolean;
165
+ }>;
118
166
  };
119
167
  type AppState = {
120
168
  data: Data;
@@ -170,13 +218,13 @@ type RemoveAction = {
170
218
  index: number;
171
219
  zone: string;
172
220
  };
173
- type SetStateAction = {
221
+ type SetUiAction = {
174
222
  type: "setUi";
175
- ui: Partial<UiState>;
223
+ ui: Partial<UiState> | ((previous: UiState) => Partial<UiState>);
176
224
  };
177
225
  type SetDataAction = {
178
226
  type: "setData";
179
- data: Partial<Data>;
227
+ data: Partial<Data> | ((previous: Data) => Partial<Data>);
180
228
  };
181
229
  type SetAction = {
182
230
  type: "set";
@@ -192,7 +240,7 @@ type UnregisterZoneAction = {
192
240
  };
193
241
  type PuckAction = {
194
242
  recordHistory?: boolean;
195
- } & (ReorderAction | InsertAction | MoveAction | ReplaceAction | RemoveAction | DuplicateAction | SetAction | SetDataAction | SetStateAction | RegisterZoneAction | UnregisterZoneAction);
243
+ } & (ReorderAction | InsertAction | MoveAction | ReplaceAction | RemoveAction | DuplicateAction | SetAction | SetDataAction | SetUiAction | RegisterZoneAction | UnregisterZoneAction);
196
244
 
197
245
  type PathData = Record<string, {
198
246
  path: string[];
@@ -201,6 +249,7 @@ type PathData = Record<string, {
201
249
  type DropZoneContext = {
202
250
  data: Data;
203
251
  config: Config;
252
+ componentState?: Record<string, any>;
204
253
  itemSelector?: ItemSelector | null;
205
254
  setItemSelector?: (newIndex: ItemSelector | null) => void;
206
255
  dispatch?: (action: PuckAction) => void;
@@ -265,12 +314,17 @@ type Plugin = {
265
314
  }) => ReactElement<any>;
266
315
  };
267
316
 
268
- declare function Puck({ config, data: initialData, onChange, onPublish, plugins, renderHeader, renderHeaderActions, headerTitle, headerPath, }: {
317
+ declare function Puck({ config, data: initialData, onChange, onPublish, plugins, renderComponentList, renderHeader, renderHeaderActions, headerTitle, headerPath, }: {
269
318
  config: Config;
270
319
  data: Data;
271
320
  onChange?: (data: Data) => void;
272
321
  onPublish: (data: Data) => void;
273
322
  plugins?: Plugin[];
323
+ renderComponentList?: (props: {
324
+ children: ReactNode;
325
+ dispatch: (action: PuckAction) => void;
326
+ state: AppState;
327
+ }) => ReactElement;
274
328
  renderHeader?: (props: {
275
329
  children: ReactNode;
276
330
  dispatch: (action: PuckAction) => void;
@@ -289,10 +343,19 @@ declare function Render({ config, data }: {
289
343
  data: Data;
290
344
  }): react_jsx_runtime.JSX.Element;
291
345
 
292
- declare const FieldLabel: ({ children, icon, label, }: {
346
+ declare const resolveAllData: (data: Data, config: Config, onResolveStart?: ((item: MappedItem) => void) | undefined, onResolveEnd?: ((item: MappedItem) => void) | undefined) => Promise<{
347
+ root: RootDataWithProps<DefaultRootProps> | RootData<DefaultRootProps>;
348
+ content: any[];
349
+ zones: Record<string, MappedItem[]>;
350
+ }>;
351
+
352
+ declare const FieldLabel: ({ children, icon, label, el, readOnly, className, }: {
293
353
  children?: ReactNode;
294
354
  icon?: ReactNode;
295
355
  label: string;
356
+ el?: "div" | "label" | undefined;
357
+ readOnly?: boolean | undefined;
358
+ className?: string | undefined;
296
359
  }) => react_jsx_runtime.JSX.Element;
297
360
 
298
- export { Adaptor, AppState, ArrayState, Button, ComponentConfig, Config, Content, Data, DefaultComponentProps, DefaultRootProps, DropZone, DropZoneProvider, Field, FieldLabel, Fields, IconButton, ItemWithId, MappedItem, Puck, Render, UiState, dropZoneContext };
361
+ export { AppState, ArrayField, ArrayState, BaseData, BaseField, Button, ComponentConfig, ComponentData, Config, Content, CustomField, Data, DefaultComponentProps, DefaultRootProps, DefaultRootRenderProps, DropZone, DropZoneProvider, ExternalField, Field, FieldLabel, Fields, IconButton, ItemWithId, MappedItem, Puck, Render, RootData, RootDataWithProps, RootDataWithoutProps, SelectField, TextField, UiState, dropZoneContext, resolveAllData };