@elementor/editor-props 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,14 @@
1
- import { z, type ZodTypeAny } from '@elementor/schema';
1
+ import { z, type ZodType, type ZodTypeAny } from '@elementor/schema';
2
+
3
+ import { type PropValue } from '../types';
2
4
 
3
5
  type Updater< T > = ( prev?: T ) => T;
4
6
 
7
+ export type CreateOptions = {
8
+ base?: unknown;
9
+ disabled?: boolean;
10
+ };
11
+
5
12
  /**
6
13
  * Usage example:
7
14
  *
@@ -10,32 +17,46 @@ type Updater< T > = ( prev?: T ) => T;
10
17
  *
11
18
  * elementsPropUtils.isValid( element.props?.children );
12
19
  * elementsPropUtils.create( [ 'a', 'b' ] );
13
- * elementsPropUtils.create( ( prev = [] ) => [ ...prev, 'c' ], element.props?.children );
20
+ * elementsPropUtils.create( ( prev = [] ) => [ ...prev, 'c' ], { base: element.props?.children } );
21
+ * elementsPropUtils.create( ( prev = [] ) => [ ...prev, 'c' ], { disabled: true } );
22
+ * elementsPropUtils.extract( element.props?.children );
23
+ *
14
24
  * ```
15
25
  */
16
26
 
27
+ export type PropTypeUtil< TKey extends string, TValue extends PropValue > = ReturnType<
28
+ typeof createPropUtils< TKey, ZodType< TValue > >
29
+ >;
30
+
17
31
  export function createPropUtils< TKey extends string, TValue extends ZodTypeAny >( key: TKey, valueSchema: TValue ) {
18
- const schema = z.object( {
32
+ const schema = z.strictObject( {
19
33
  $$type: z.literal( key ),
20
34
  value: valueSchema,
35
+ disabled: z.boolean().optional(),
21
36
  } );
22
37
 
23
38
  type Prop = z.infer< typeof schema >;
24
39
 
40
+ type TPropValue = z.infer< typeof valueSchema >;
41
+
25
42
  function isValid( prop: unknown ): prop is Prop {
26
43
  return schema.safeParse( prop ).success;
27
44
  }
28
45
 
29
- function create( value: Prop[ 'value' ] ): Prop;
30
- function create( value: Updater< Prop[ 'value' ] >, base: unknown ): Prop;
31
- function create( value: Prop[ 'value' ] | Updater< Prop[ 'value' ] >, base?: unknown ): Prop {
32
- const fn = ( typeof value === 'function' ? value : () => value ) as Updater< Prop[ 'value' ] >;
46
+ function create( value: TPropValue ): Prop;
47
+ function create( value: TPropValue, createOptions?: CreateOptions ): Prop;
48
+ function create( value: Updater< TPropValue >, createOptions: CreateOptions ): Prop;
49
+ function create( value: TPropValue | Updater< TPropValue >, createOptions?: CreateOptions ): Prop {
50
+ const fn = ( typeof value === 'function' ? value : () => value ) as Updater< TPropValue >;
51
+
52
+ const { base, disabled } = createOptions || {};
33
53
 
34
54
  if ( ! base ) {
35
55
  return {
36
56
  $$type: key,
37
57
  value: fn(),
38
- };
58
+ ...( disabled && { disabled } ),
59
+ } as Prop;
39
60
  }
40
61
 
41
62
  if ( ! isValid( base ) ) {
@@ -45,12 +66,23 @@ export function createPropUtils< TKey extends string, TValue extends ZodTypeAny
45
66
  return {
46
67
  $$type: key,
47
68
  value: fn( base.value ),
48
- };
69
+ ...( disabled && { disabled } ),
70
+ } as Prop;
71
+ }
72
+
73
+ function extract( prop: unknown ): TPropValue | null {
74
+ if ( ! isValid( prop ) ) {
75
+ return null;
76
+ }
77
+
78
+ return prop.value;
49
79
  }
50
80
 
51
81
  return {
82
+ extract,
52
83
  isValid,
53
84
  create,
54
- schema,
85
+ // this type fails in build due to zod issue that does not recognize the schema as a ZodType.
86
+ schema: schema as ZodType< Prop >,
55
87
  };
56
88
  }