@elementor/editor-components 3.35.0-411 → 3.35.0-413

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.
package/src/types.ts CHANGED
@@ -73,3 +73,8 @@ export type ComponentOverridable = {
73
73
  override_key: string;
74
74
  origin_value: TransformablePropValue< string >;
75
75
  };
76
+
77
+ export type UpdatedComponentName = {
78
+ componentId: number;
79
+ title: string;
80
+ };
@@ -0,0 +1,27 @@
1
+ import { __getState as getState } from '@elementor/store';
2
+
3
+ import { createSubmitComponentSchema } from '../components/create-component-form/utils/component-form-schema';
4
+ import { selectComponents } from '../store/store';
5
+
6
+ type ValidationResult = { isValid: true; errorMessage: null } | { isValid: false; errorMessage: string };
7
+
8
+ export function validateComponentName( label: string ): ValidationResult {
9
+ const existingComponentTitles = selectComponents( getState() )?.map( ( { name } ) => name ) ?? [];
10
+ const schema = createSubmitComponentSchema( existingComponentTitles );
11
+ const result = schema.safeParse( { componentName: label.toLowerCase() } );
12
+
13
+ if ( result.success ) {
14
+ return {
15
+ isValid: true,
16
+ errorMessage: null,
17
+ };
18
+ }
19
+
20
+ const formattedErrors = result.error.format();
21
+ const errorMessage = formattedErrors.componentName?._errors[ 0 ] ?? formattedErrors._errors[ 0 ];
22
+
23
+ return {
24
+ isValid: false,
25
+ errorMessage,
26
+ };
27
+ }