11import { $t } from '../../localization' ;
22import { dropdownProperty } from '../../properties/helpers' ;
33import { UnlayerProperty } from '../../types' ;
4- import { CustomField , SmartFormAction } from './types' ;
4+ import { CustomField , SmartFormAction , UnlayerField } from './types' ;
55
66export const behaviorListProperty : ( ) => UnlayerProperty < string > = ( ) =>
77 dropdownProperty ( {
@@ -69,19 +69,20 @@ const optionToString = (options: string[] | undefined) => {
6969 ?. slice ( 0 , - 1 ) ;
7070} ;
7171
72- export const availableFields = userData . fields ?. map ( ( field : CustomField ) => {
73- return {
74- meta_data : {
75- ...field ,
76- } ,
77- name : field . name ,
78- type : getFieldCompatibleType ( field . type ) ,
79- label : field . name ,
80- required : field . required ,
81- show_label : true ,
82- options : optionToString ( field . allowedValues ) ,
83- } ;
84- } ) ;
72+ export const availableFields : UnlayerField [ ] =
73+ userData . fields ?. map ( ( field : CustomField ) => {
74+ return {
75+ meta_data : {
76+ ...field ,
77+ } ,
78+ name : field . name ,
79+ type : getFieldCompatibleType ( field . type ) ,
80+ label : field . name ,
81+ required : field . required ,
82+ show_label : true ,
83+ options : optionToString ( field . allowedValues ) ,
84+ } ;
85+ } ) ?? [ ] ;
8586
8687export const isValidUrl = ( url : string ) => {
8788 /* eslint-disable no-useless-escape */
@@ -90,3 +91,53 @@ export const isValidUrl = (url: string) => {
9091 ) ;
9192 return URL_VALID_REGEX . test ( url ) ;
9293} ;
94+
95+ export const applyFieldsWidgetModalRestrictions = ( ) => {
96+ window . requestAnimationFrame ( ( ) => {
97+ const modal = document . querySelector ( '.blockbuilder-fields-widget-modal' ) ;
98+ if ( ! ( modal instanceof HTMLElement ) ) {
99+ return ;
100+ }
101+
102+ const modalBody = modal . querySelector ( '.modal-content' ) ;
103+ const modalFooter = modal . querySelector ( '.modal-footer' ) ;
104+ if (
105+ ! ( modalBody instanceof HTMLElement ) ||
106+ ! ( modalFooter instanceof HTMLElement )
107+ ) {
108+ return ;
109+ }
110+
111+ const typeButton = modalBody . querySelector ( 'button.dropdown-button' ) ;
112+ const fieldNameInput = modalBody . querySelector ( 'input[name="name"]' ) ;
113+ const inputOption = modalBody . querySelector ( 'textarea' ) ;
114+ const fieldName =
115+ fieldNameInput instanceof HTMLInputElement
116+ ? fieldNameInput . value
117+ : undefined ;
118+ const currentField = availableFields . find (
119+ ( field : UnlayerField ) => field . name === fieldName ,
120+ ) ;
121+
122+ if ( typeButton instanceof HTMLButtonElement && currentField ) {
123+ typeButton . disabled = currentField . type !== 'text' ;
124+ }
125+
126+ if ( inputOption instanceof HTMLTextAreaElement && currentField ) {
127+ inputOption . disabled = currentField . type !== 'text' ;
128+ }
129+
130+ const footerButtons = modalFooter . querySelectorAll ( 'button' ) ;
131+ const buttonEliminate = footerButtons [ 1 ] ;
132+ if (
133+ buttonEliminate instanceof HTMLButtonElement &&
134+ fieldNameInput instanceof HTMLInputElement
135+ ) {
136+ buttonEliminate . disabled = fieldNameInput . value === 'EMAIL' ;
137+ }
138+
139+ if ( fieldNameInput instanceof HTMLInputElement ) {
140+ fieldNameInput . closest ( 'form' ) ?. remove ( ) ;
141+ }
142+ } ) ;
143+ } ;
0 commit comments