@elementor/editor-controls 0.9.0 → 0.11.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,6 +1,13 @@
1
1
  import * as React from 'react';
2
- import { useState } from 'react';
3
- import { booleanPropTypeUtil, linkPropTypeUtil, type LinkPropValue, numberPropTypeUtil } from '@elementor/editor-props';
2
+ import { useMemo, useState } from 'react';
3
+ import {
4
+ booleanPropTypeUtil,
5
+ linkPropTypeUtil,
6
+ type LinkPropValue,
7
+ numberPropTypeUtil,
8
+ stringPropTypeUtil,
9
+ urlPropTypeUtil,
10
+ } from '@elementor/editor-props';
4
11
  import { type HttpResponse, httpService } from '@elementor/http';
5
12
  import { MinusIcon, PlusIcon } from '@elementor/icons';
6
13
  import { useSessionStorage } from '@elementor/session';
@@ -8,19 +15,20 @@ import { Collapse, Divider, Grid, IconButton, Stack, Switch } from '@elementor/u
8
15
  import { __ } from '@wordpress/i18n';
9
16
 
10
17
  import { PropKeyProvider, PropProvider, useBoundProp } from '../bound-prop-context';
11
- import { ControlLabel } from '../components/control-label';
12
- import { createControl } from '../create-control';
13
18
  import {
14
- AutocompleteControl,
19
+ Autocomplete,
15
20
  type CategorizedOption,
16
21
  findMatchingOption,
17
22
  type FlatOption,
18
23
  isCategorizedOptionPool,
19
- } from './autocomplete-control';
24
+ } from '../components/autocomplete';
25
+ import { ControlLabel } from '../components/control-label';
26
+ import ControlActions from '../control-actions/control-actions';
27
+ import { createControl } from '../create-control';
20
28
 
21
29
  type Props = {
22
30
  queryOptions: {
23
- requestParams: object;
31
+ requestParams: Record< string, unknown >;
24
32
  endpoint: string;
25
33
  };
26
34
  allowCustomValues?: boolean;
@@ -29,7 +37,7 @@ type Props = {
29
37
  };
30
38
 
31
39
  type LinkSessionValue = {
32
- value?: LinkPropValue[ 'value' ];
40
+ value?: LinkPropValue[ 'value' ] | null;
33
41
  meta?: {
34
42
  isEnabled?: boolean;
35
43
  };
@@ -42,6 +50,7 @@ const SIZE = 'tiny';
42
50
  export const LinkControl = createControl( ( props: Props ) => {
43
51
  const { value, path, setValue, ...propContext } = useBoundProp( linkPropTypeUtil );
44
52
  const [ linkSessionValue, setLinkSessionValue ] = useSessionStorage< LinkSessionValue >( path.join( '/' ) );
53
+ const [ isEnabled, setIsEnabled ] = useState( !! value );
45
54
 
46
55
  const {
47
56
  allowCustomValues,
@@ -55,38 +64,39 @@ export const LinkControl = createControl( ( props: Props ) => {
55
64
  );
56
65
 
57
66
  const onEnabledChange = () => {
58
- const { meta } = linkSessionValue ?? {};
59
- const { isEnabled } = meta ?? {};
60
-
61
- setValue( isEnabled ? null : linkSessionValue?.value ?? { destination: null } );
67
+ setIsEnabled( ( prevState ) => ! prevState );
68
+ setValue( isEnabled ? null : linkSessionValue?.value ?? null );
62
69
  setLinkSessionValue( { value, meta: { isEnabled: ! isEnabled } } );
63
70
  };
64
71
 
65
72
  const onOptionChange = ( newValue: number | null ) => {
66
- const valueToSave = {
67
- ...value,
68
- destination: { $$type: 'number', value: newValue },
69
- label: {
70
- $$type: 'string',
71
- value: findMatchingOption( options, newValue?.toString() )?.label || '',
72
- },
73
- };
73
+ const valueToSave: LinkPropValue[ 'value' ] | null = newValue
74
+ ? {
75
+ ...value,
76
+ destination: numberPropTypeUtil.create( newValue ),
77
+ label: stringPropTypeUtil.create( findMatchingOption( options, newValue )?.label || null ),
78
+ }
79
+ : null;
74
80
 
75
81
  onSaveNewValue( valueToSave );
76
82
  };
77
83
 
78
84
  const onTextChange = ( newValue: string | null ) => {
79
- const valueToSave = {
80
- ...value,
81
- destination: { $$type: 'url', value: newValue },
82
- label: null,
83
- };
85
+ newValue = newValue?.trim() || '';
86
+
87
+ const valueToSave: LinkPropValue[ 'value' ] | null = newValue
88
+ ? {
89
+ ...value,
90
+ destination: urlPropTypeUtil.create( newValue ),
91
+ label: null,
92
+ }
93
+ : null;
84
94
 
85
95
  onSaveNewValue( valueToSave );
86
96
  updateOptions( newValue );
87
97
  };
88
98
 
89
- const onSaveNewValue = ( newValue: typeof value ) => {
99
+ const onSaveNewValue = ( newValue: LinkPropValue[ 'value' ] | null ) => {
90
100
  setValue( newValue );
91
101
  setLinkSessionValue( { ...linkSessionValue, value: newValue } );
92
102
  };
@@ -98,17 +108,20 @@ export const LinkControl = createControl( ( props: Props ) => {
98
108
  return;
99
109
  }
100
110
 
101
- debounceFetch( newValue )();
111
+ debounceFetch( { ...requestParams, term: newValue } );
102
112
  };
103
113
 
104
- const debounceFetch = ( newValue: string ) =>
105
- debounce(
106
- () =>
107
- fetchOptions( endpoint, { ...requestParams, term: newValue } ).then( ( newOptions ) => {
108
- setOptions( formatOptions( newOptions ) );
109
- } ),
110
- 400
111
- );
114
+ const debounceFetch = useMemo(
115
+ () =>
116
+ debounce(
117
+ ( params: FetchOptionsParams ) =>
118
+ fetchOptions( endpoint, params ).then( ( newOptions ) => {
119
+ setOptions( formatOptions( newOptions ) );
120
+ } ),
121
+ 400
122
+ ),
123
+ [ endpoint ]
124
+ );
112
125
 
113
126
  return (
114
127
  <PropProvider { ...propContext } value={ value } setValue={ setValue }>
@@ -123,24 +136,25 @@ export const LinkControl = createControl( ( props: Props ) => {
123
136
  >
124
137
  <ControlLabel>{ __( 'Link', 'elementor' ) }</ControlLabel>
125
138
  <ToggleIconControl
126
- enabled={ linkSessionValue?.meta?.isEnabled ?? false }
139
+ enabled={ isEnabled }
127
140
  onIconClick={ onEnabledChange }
128
- label={ __( 'Toggle Link', 'elementor' ) }
141
+ label={ __( 'Toggle link', 'elementor' ) }
129
142
  />
130
143
  </Stack>
131
- <Collapse in={ linkSessionValue?.meta?.isEnabled } timeout="auto" unmountOnExit>
144
+ <Collapse in={ isEnabled } timeout="auto" unmountOnExit>
132
145
  <Stack gap={ 1.5 }>
133
146
  <PropKeyProvider bind={ 'destination' }>
134
- <AutocompleteControl
135
- options={ options }
136
- allowCustomValues={ allowCustomValues }
137
- placeholder={ placeholder }
138
- optionRestrictedPropTypeUtil={ numberPropTypeUtil }
139
- onOptionChangeCallback={ onOptionChange }
140
- onTextChangeCallback={ onTextChange }
141
- minInputLength={ minInputLength }
142
- customValue={ value?.destination?.value?.toString() }
143
- />
147
+ <ControlActions>
148
+ <Autocomplete
149
+ options={ options }
150
+ allowCustomValues={ allowCustomValues }
151
+ placeholder={ placeholder }
152
+ value={ value?.destination?.value }
153
+ onOptionChange={ onOptionChange }
154
+ onTextChange={ onTextChange }
155
+ minInputLength={ minInputLength }
156
+ />
157
+ </ControlActions>
144
158
  </PropKeyProvider>
145
159
  <PropKeyProvider bind={ 'isTargetBlank' }>
146
160
  <SwitchControl />
@@ -186,7 +200,9 @@ const SwitchControl = () => {
186
200
  );
187
201
  };
188
202
 
189
- async function fetchOptions( ajaxUrl: string, params: object ) {
203
+ type FetchOptionsParams = Record< string, unknown > & { term: string };
204
+
205
+ async function fetchOptions( ajaxUrl: string, params: FetchOptionsParams ) {
190
206
  if ( ! params || ! ajaxUrl ) {
191
207
  return [];
192
208
  }
@@ -38,7 +38,7 @@ export const LinkedDimensionsControl = createControl( ( { label }: { label: stri
38
38
  <Stack direction="row" gap={ 2 } flexWrap="nowrap">
39
39
  <ControlLabel>{ label }</ControlLabel>
40
40
  <ToggleButton
41
- aria-label={ __( 'Link Inputs', 'elementor' ) }
41
+ aria-label={ __( 'Link inputs', 'elementor' ) }
42
42
  size={ 'tiny' }
43
43
  value={ 'check' }
44
44
  selected={ isLinked }
@@ -23,7 +23,14 @@ export const SelectControl = createControl( ( { options, onChange }: Props ) =>
23
23
 
24
24
  return (
25
25
  <ControlActions>
26
- <Select displayEmpty size="tiny" value={ value ?? '' } onChange={ handleChange } fullWidth>
26
+ <Select
27
+ sx={ { overflow: 'hidden' } }
28
+ displayEmpty
29
+ size="tiny"
30
+ value={ value ?? '' }
31
+ onChange={ handleChange }
32
+ fullWidth
33
+ >
27
34
  { options.map( ( { label, ...props } ) => (
28
35
  <MenuItem key={ props.value } { ...props } value={ props.value ?? '' }>
29
36
  { label }
@@ -23,10 +23,10 @@ export const StrokeControl = createControl( () => {
23
23
  return (
24
24
  <PropProvider { ...propContext }>
25
25
  <Stack gap={ 1.5 }>
26
- <Control bind="width" label={ __( 'Stroke Width', 'elementor' ) }>
26
+ <Control bind="width" label={ __( 'Stroke width', 'elementor' ) }>
27
27
  <SizeControl units={ units } />
28
28
  </Control>
29
- <Control bind="color" label={ __( 'Stroke Color', 'elementor' ) }>
29
+ <Control bind="color" label={ __( 'Stroke color', 'elementor' ) }>
30
30
  <ColorControl />
31
31
  </Control>
32
32
  </Stack>
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  // control types
2
- export { AutocompleteControl } from './controls/autocomplete-control';
3
2
  export { ImageControl } from './controls/image-control';
4
3
  export { TextControl } from './controls/text-control';
5
4
  export { TextAreaControl } from './controls/text-area-control';
@@ -1,195 +0,0 @@
1
- import * as React from 'react';
2
- import { type PropTypeUtil } from '@elementor/editor-props';
3
- import { XIcon } from '@elementor/icons';
4
- import {
5
- Autocomplete,
6
- type AutocompleteRenderInputParams,
7
- Box,
8
- IconButton,
9
- InputAdornment,
10
- TextField,
11
- } from '@elementor/ui';
12
-
13
- import { useBoundProp } from '../bound-prop-context';
14
- import { createControl } from '../create-control';
15
-
16
- export type FlatOption = {
17
- id: string;
18
- label: string;
19
- groupLabel?: never;
20
- };
21
-
22
- export type CategorizedOption = {
23
- id: string;
24
- label: string;
25
- groupLabel: string;
26
- };
27
-
28
- export type Props< TOptionKey extends string, TCustomKey extends string = '' > = {
29
- options: FlatOption[] | CategorizedOption[];
30
- optionRestrictedPropTypeUtil: PropTypeUtil< TOptionKey, number | null >;
31
- onOptionChangeCallback?: ( newValue: number | null ) => void;
32
- onTextChangeCallback?: ( newValue: string | null ) => void;
33
- allowCustomValues?: boolean;
34
- placeholder?: string;
35
- minInputLength?: number;
36
- customValue?: TCustomKey;
37
- };
38
-
39
- export const AutocompleteControl = createControl(
40
- < TOptionKey extends string, TCustomKey extends string >( props: Props< TOptionKey, TCustomKey > ) => {
41
- const {
42
- options,
43
- optionRestrictedPropTypeUtil,
44
- onOptionChangeCallback,
45
- onTextChangeCallback,
46
- allowCustomValues = false,
47
- placeholder = '',
48
- minInputLength = 2,
49
- customValue,
50
- } = props;
51
-
52
- const { value: selectableValue, setValue: setSelectableValue } = useBoundProp( optionRestrictedPropTypeUtil );
53
-
54
- const value = selectableValue || customValue || '';
55
-
56
- const optionKeys = _factoryFilter( selectableValue || customValue || null, options, minInputLength ).map(
57
- ( { id } ) => id
58
- );
59
- const allowClear = !! value;
60
-
61
- // Prevents MUI warning when freeSolo/allowCustomValues is false
62
- const muiWarningPreventer = allowCustomValues || !! value?.toString()?.length;
63
- const isOptionEqualToValue = () => {
64
- return muiWarningPreventer ? undefined : () => true;
65
- };
66
- const hasSelectedValue = !! findMatchingOption( options, selectableValue?.toString() );
67
-
68
- const onOptionChange = ( newValue: number | null ) => {
69
- setSelectableValue( newValue );
70
- onOptionChangeCallback?.( newValue );
71
- };
72
-
73
- const onTextChange = ( newValue: string | null ) => {
74
- onTextChangeCallback?.( newValue );
75
- };
76
-
77
- return (
78
- <Autocomplete
79
- forcePopupIcon={ false }
80
- disableClearable={ true } // Disabled component's auto clear icon to use our custom one instead
81
- freeSolo={ allowCustomValues }
82
- value={ value?.toString() || '' }
83
- size={ 'tiny' }
84
- onChange={ ( _, newValue ) => onOptionChange( Number( newValue ) ) }
85
- readOnly={ hasSelectedValue }
86
- options={ optionKeys }
87
- getOptionKey={ ( optionId ) => findMatchingOption( options, optionId )?.id || optionId }
88
- getOptionLabel={ ( optionId ) => findMatchingOption( options, optionId )?.label || optionId.toString() }
89
- groupBy={
90
- isCategorizedOptionPool( options )
91
- ? ( optionId: string ) => findMatchingOption( options, optionId )?.groupLabel || optionId
92
- : undefined
93
- }
94
- isOptionEqualToValue={ isOptionEqualToValue() }
95
- filterOptions={ () => optionKeys }
96
- renderOption={ ( optionProps, optionId ) => (
97
- <Box component="li" { ...optionProps } key={ optionProps.id }>
98
- { findMatchingOption( options, optionId )?.label ?? optionId }
99
- </Box>
100
- ) }
101
- renderInput={ ( params ) => (
102
- <TextInput
103
- params={ params }
104
- handleChange={ onTextChange }
105
- allowClear={ allowClear }
106
- placeholder={ placeholder }
107
- hasSelectedValue={ hasSelectedValue }
108
- />
109
- ) }
110
- />
111
- );
112
- }
113
- );
114
-
115
- const TextInput = ( {
116
- params,
117
- allowClear,
118
- placeholder,
119
- handleChange,
120
- hasSelectedValue,
121
- }: {
122
- params: AutocompleteRenderInputParams;
123
- allowClear: boolean;
124
- handleChange: ( newValue: string | null ) => void;
125
- placeholder: string;
126
- hasSelectedValue: boolean;
127
- } ) => {
128
- const onChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
129
- handleChange( event.target.value );
130
- };
131
-
132
- return (
133
- <TextField
134
- { ...params }
135
- placeholder={ placeholder }
136
- onChange={ onChange }
137
- sx={ {
138
- '& .MuiInputBase-input': {
139
- cursor: hasSelectedValue ? 'default' : undefined,
140
- },
141
- } }
142
- InputProps={ {
143
- ...params.InputProps,
144
- endAdornment: <ClearButton params={ params } allowClear={ allowClear } handleChange={ handleChange } />,
145
- } }
146
- />
147
- );
148
- };
149
-
150
- const ClearButton = ( {
151
- allowClear,
152
- handleChange,
153
- params,
154
- }: {
155
- params: AutocompleteRenderInputParams;
156
- allowClear: boolean;
157
- handleChange: ( newValue: string | null ) => void;
158
- } ) => (
159
- <InputAdornment position="end">
160
- { allowClear && (
161
- <IconButton size={ params.size } onClick={ () => handleChange( null ) } sx={ { cursor: 'pointer' } }>
162
- <XIcon fontSize={ params.size } />
163
- </IconButton>
164
- ) }
165
- </InputAdornment>
166
- );
167
-
168
- export function findMatchingOption( options: FlatOption[] | CategorizedOption[], optionId: string | null = null ) {
169
- return options.find( ( { id } ) => optionId?.toString() === id.toString() );
170
- }
171
-
172
- export function isCategorizedOptionPool( options: FlatOption[] | CategorizedOption[] ): options is CategorizedOption[] {
173
- return options.every( ( option ) => 'groupLabel' in option );
174
- }
175
- function _factoryFilter< T extends FlatOption[] | CategorizedOption[] >(
176
- newValue: string | number | null,
177
- options: T,
178
- minInputLength: number
179
- ): T {
180
- if ( null === newValue ) {
181
- return options;
182
- }
183
-
184
- const formattedValue = String( newValue || '' )?.toLowerCase();
185
-
186
- if ( formattedValue.length < minInputLength ) {
187
- return new Array( 0 ) as T;
188
- }
189
-
190
- return options.filter(
191
- ( option ) =>
192
- String( option.id ).toLowerCase().includes( formattedValue ) ||
193
- option.label.toLowerCase().includes( formattedValue )
194
- ) as T;
195
- }