@elementor/editor-controls 4.5.0-1156 → 4.5.0-1157

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elementor/editor-controls",
3
3
  "description": "This package contains the controls model and utils for the Elementor editor",
4
- "version": "4.5.0-1156",
4
+ "version": "4.5.0-1157",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -40,23 +40,23 @@
40
40
  "dev": "tsup --config=../../tsup.dev.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@elementor/editor-current-user": "4.5.0-1156",
44
- "@elementor/editor-elements": "4.5.0-1156",
45
- "@elementor/editor-props": "4.5.0-1156",
46
- "@elementor/editor-responsive": "4.5.0-1156",
47
- "@elementor/editor-ui": "4.5.0-1156",
48
- "@elementor/editor-v1-adapters": "4.5.0-1156",
49
- "@elementor/env": "4.5.0-1156",
50
- "@elementor/events": "4.5.0-1156",
51
- "@elementor/http-client": "4.5.0-1156",
43
+ "@elementor/editor-current-user": "4.5.0-1157",
44
+ "@elementor/editor-elements": "4.5.0-1157",
45
+ "@elementor/editor-props": "4.5.0-1157",
46
+ "@elementor/editor-responsive": "4.5.0-1157",
47
+ "@elementor/editor-ui": "4.5.0-1157",
48
+ "@elementor/editor-v1-adapters": "4.5.0-1157",
49
+ "@elementor/env": "4.5.0-1157",
50
+ "@elementor/events": "4.5.0-1157",
51
+ "@elementor/http-client": "4.5.0-1157",
52
52
  "@elementor/icons": "~1.78.1",
53
- "@elementor/locations": "4.5.0-1156",
54
- "@elementor/query": "4.5.0-1156",
55
- "@elementor/schema": "4.5.0-1156",
56
- "@elementor/session": "4.5.0-1156",
53
+ "@elementor/locations": "4.5.0-1157",
54
+ "@elementor/query": "4.5.0-1157",
55
+ "@elementor/schema": "4.5.0-1157",
56
+ "@elementor/session": "4.5.0-1157",
57
57
  "@elementor/ui": "1.41.4",
58
- "@elementor/utils": "4.5.0-1156",
59
- "@elementor/wp-media": "4.5.0-1156",
58
+ "@elementor/utils": "4.5.0-1157",
59
+ "@elementor/wp-media": "4.5.0-1157",
60
60
  "@monaco-editor/react": "^4.7.0",
61
61
  "@tanstack/react-virtual": "^3.13.3",
62
62
  "@tiptap/extension-bold": "^3.11.1",
@@ -0,0 +1,104 @@
1
+ import * as React from 'react';
2
+ import { type SyntheticEvent, useMemo } from 'react';
3
+ import { numberPropTypeUtil, queryPropTypeUtil, stringPropTypeUtil } from '@elementor/editor-props';
4
+ import { Autocomplete, TextField } from '@elementor/ui';
5
+ import { __ } from '@wordpress/i18n';
6
+
7
+ import { useBoundProp } from '../bound-prop-context';
8
+ import { type FlatOption } from '../components/autocomplete';
9
+ import ControlActions from '../control-actions/control-actions';
10
+ import { createControl } from '../create-control';
11
+ import { extractFlatOptionFromQueryValue, useQueryAutocomplete } from '../hooks/use-query-autocomplete';
12
+
13
+ type QuerySelectControlProps = {
14
+ queryOptions: {
15
+ params: Record< string, unknown >;
16
+ url: string;
17
+ };
18
+ placeholder?: string;
19
+ minInputLength?: number;
20
+ ariaLabel?: string;
21
+ };
22
+
23
+ const SIZE = 'tiny';
24
+
25
+ export const QuerySelectControl = createControl( ( props: QuerySelectControlProps ) => {
26
+ const { queryOptions, placeholder, minInputLength = 0, ariaLabel } = props;
27
+ const { value, setValue, disabled } = useBoundProp( queryPropTypeUtil );
28
+
29
+ const selectedOption = useMemo( () => extractFlatOptionFromQueryValue( value ), [ value ] );
30
+
31
+ const { options, updateOptions } = useQueryAutocomplete( {
32
+ url: queryOptions.url,
33
+ params: queryOptions.params,
34
+ minInputLength,
35
+ initialQueryValue: value,
36
+ } );
37
+
38
+ const optionsWithSelected = useMemo(
39
+ () => withSelectedOption( options as FlatOption[], selectedOption ),
40
+ [ options, selectedOption ]
41
+ );
42
+
43
+ const handleChange = ( _: SyntheticEvent, newValue: FlatOption | null ) => {
44
+ if ( ! newValue ) {
45
+ setValue( null );
46
+
47
+ return;
48
+ }
49
+
50
+ setValue( {
51
+ id: numberPropTypeUtil.create( Number( newValue.id ) ),
52
+ label: stringPropTypeUtil.create( newValue.label ),
53
+ } );
54
+ };
55
+
56
+ const handleInputChange = ( _: SyntheticEvent, term: string, reason: string ) => {
57
+ if ( 'reset' === reason || 'clear' === reason ) {
58
+ updateOptions( null );
59
+ return;
60
+ }
61
+
62
+ if ( 'input' !== reason ) {
63
+ return;
64
+ }
65
+
66
+ updateOptions( term || null );
67
+ };
68
+
69
+ return (
70
+ <ControlActions>
71
+ <Autocomplete
72
+ fullWidth
73
+ openOnFocus
74
+ disabled={ disabled }
75
+ size={ SIZE }
76
+ value={ selectedOption }
77
+ options={ optionsWithSelected }
78
+ onChange={ handleChange }
79
+ onInputChange={ handleInputChange }
80
+ getOptionLabel={ ( option ) => option.label }
81
+ isOptionEqualToValue={ ( option, val ) => option.id === val.id }
82
+ filterOptions={ ( opts ) => opts }
83
+ renderInput={ ( params ) => (
84
+ <TextField
85
+ { ...params }
86
+ placeholder={ placeholder ?? __( 'Search', 'elementor' ) }
87
+ inputProps={ {
88
+ ...params.inputProps,
89
+ ...( ariaLabel ? { 'aria-label': ariaLabel } : {} ),
90
+ } }
91
+ />
92
+ ) }
93
+ />
94
+ </ControlActions>
95
+ );
96
+ } );
97
+
98
+ function withSelectedOption( options: FlatOption[], selectedOption: FlatOption | null ): FlatOption[] {
99
+ if ( ! selectedOption || options.some( ( option ) => option.id === selectedOption.id ) ) {
100
+ return options;
101
+ }
102
+
103
+ return [ selectedOption, ...options ];
104
+ }
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo, useState } from 'react';
1
+ import { useEffect, useMemo, useRef, useState } from 'react';
2
2
  import { numberPropTypeUtil, type QueryPropValue, stringPropTypeUtil } from '@elementor/editor-props';
3
3
  import { type HttpResponse, httpService } from '@elementor/http-client';
4
4
  import { debounce } from '@elementor/utils';
@@ -47,13 +47,17 @@ export function useQueryAutocomplete( {
47
47
  [ url, excludeIdSet ]
48
48
  );
49
49
 
50
+ const paramsKey = useMemo( () => JSON.stringify( params ), [ params ] );
51
+ const paramsRef = useRef( params );
52
+ paramsRef.current = params;
53
+
50
54
  useEffect( () => {
51
55
  if ( minInputLength === 0 && url ) {
52
- fetchOptions( url, { ...params, term: '' } ).then( ( newOptions ) => {
56
+ fetchOptions( url, { ...paramsRef.current, term: '' } ).then( ( newOptions ) => {
53
57
  setOptions( formatOptions( filterExcludedOptions( newOptions, excludeIdSet ) ) );
54
58
  } );
55
59
  }
56
- }, [ url, minInputLength, excludeIdSet, params ] );
60
+ }, [ url, minInputLength, excludeIdSet, paramsKey ] );
57
61
 
58
62
  const updateOptions = ( term: string | null ) => {
59
63
  const termStr = term ?? '';
package/src/index.ts CHANGED
@@ -23,6 +23,7 @@ export { HtmlTagControl } from './controls/html-tag-control';
23
23
  export { QueryChipsControl } from './controls/query-chips-control';
24
24
  export { QueryControl } from './controls/query-control';
25
25
  export { QueryFilterRepeaterControl } from './controls/query-filter-repeater-control';
26
+ export { QuerySelectControl } from './controls/query-select-control';
26
27
  export { GapControl } from './controls/gap-control';
27
28
  export { AspectRatioControl } from './controls/aspect-ratio-control';
28
29
  export { SvgMediaControl } from './controls/svg-media-control';