@elementor/editor-controls 0.10.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,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
- }