@elementor/editor-controls 0.1.0 → 0.1.1

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.
@@ -8,24 +8,48 @@ import { createControl } from '../create-control';
8
8
  const isEmptyOrNaN = ( value?: string | number ) =>
9
9
  value === undefined || value === '' || Number.isNaN( Number( value ) );
10
10
 
11
- export const NumberControl = createControl( ( { placeholder }: { placeholder?: string } ) => {
12
- const { value, setValue } = useBoundProp< number | undefined >();
13
-
14
- const handleChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
15
- const eventValue: string = event.target.value;
16
- setValue( isEmptyOrNaN( eventValue ) ? undefined : Number( eventValue ) );
17
- };
18
-
19
- return (
20
- <ControlActions>
21
- <TextField
22
- size="tiny"
23
- type="number"
24
- fullWidth
25
- value={ isEmptyOrNaN( value ) ? '' : value }
26
- onChange={ handleChange }
27
- placeholder={ placeholder }
28
- />
29
- </ControlActions>
30
- );
31
- } );
11
+ export const NumberControl = createControl(
12
+ ( {
13
+ placeholder,
14
+ max = Number.MAX_VALUE,
15
+ min = -Number.MAX_VALUE,
16
+ step = 1,
17
+ shouldForceInt = false,
18
+ }: {
19
+ placeholder?: string;
20
+ max?: number;
21
+ min?: number;
22
+ step?: number;
23
+ shouldForceInt?: boolean;
24
+ } ) => {
25
+ const { value, setValue } = useBoundProp< number | undefined >();
26
+
27
+ const handleChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
28
+ const eventValue: string = event.target.value;
29
+
30
+ if ( isEmptyOrNaN( eventValue ) ) {
31
+ setValue( undefined );
32
+
33
+ return;
34
+ }
35
+
36
+ const formattedValue = shouldForceInt ? +parseFloat( eventValue ) : Number( eventValue );
37
+
38
+ setValue( Math.min( Math.max( formattedValue, min ), max ) );
39
+ };
40
+
41
+ return (
42
+ <ControlActions>
43
+ <TextField
44
+ size="tiny"
45
+ type="number"
46
+ fullWidth
47
+ value={ isEmptyOrNaN( value ) ? '' : value }
48
+ onChange={ handleChange }
49
+ placeholder={ placeholder }
50
+ inputProps={ { step } }
51
+ />
52
+ </ControlActions>
53
+ );
54
+ }
55
+ );
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { type PropValue, type StrokePropValue, type TransformablePropValue } from '@elementor/editor-props';
2
+ import { type ColorPropValue, type PropValue, type SizePropValue, type StrokePropValue } from '@elementor/editor-props';
3
3
  import { Grid, Stack } from '@elementor/ui';
4
4
  import { __ } from '@wordpress/i18n';
5
5
 
@@ -11,31 +11,22 @@ import { SizeControl, type Unit } from './size-control';
11
11
 
12
12
  type SetContextValue = ( v: PropValue ) => void;
13
13
 
14
- const defaultStrokeControlValue: StrokePropValue = {
15
- $$type: 'stroke',
16
- value: {
17
- color: {
18
- $$type: 'color',
19
- value: '#000000',
20
- },
21
- width: {
22
- $$type: 'size',
23
- value: {
24
- unit: 'px',
25
- size: NaN,
26
- },
27
- },
28
- },
14
+ export type StrokeProps< T > = {
15
+ bind: string;
16
+ value: T;
17
+ setValue: ( v: T ) => void;
18
+ label: string;
19
+ children: React.ReactNode;
29
20
  };
30
21
 
31
22
  const units: Unit[] = [ 'px', 'em', 'rem' ];
32
23
 
33
24
  export const StrokeControl = createControl( () => {
34
- const { value, setValue } = useBoundProp< StrokePropValue >( defaultStrokeControlValue );
25
+ const { value, setValue } = useBoundProp< StrokePropValue >();
35
26
 
36
- const setStrokeWidth = ( newValue: TransformablePropValue< 'size', { unit: Unit; size: number } > ) => {
27
+ const setStrokeWidth = ( newValue: SizePropValue ) => {
37
28
  const updatedValue = {
38
- ...( value?.value ?? defaultStrokeControlValue.value ),
29
+ ...value?.value,
39
30
  width: newValue,
40
31
  };
41
32
 
@@ -45,9 +36,9 @@ export const StrokeControl = createControl( () => {
45
36
  } );
46
37
  };
47
38
 
48
- const setStrokeColor = ( newValue: TransformablePropValue< 'color', string > ) => {
39
+ const setStrokeColor = ( newValue: ColorPropValue ) => {
49
40
  const updatedValue = {
50
- ...( value?.value ?? defaultStrokeControlValue.value ),
41
+ ...value?.value,
51
42
  color: newValue,
52
43
  };
53
44
 
@@ -62,7 +53,7 @@ export const StrokeControl = createControl( () => {
62
53
  <Control
63
54
  bind="width"
64
55
  label={ __( 'Stroke Width', 'elementor' ) }
65
- value={ value?.value.width ?? defaultStrokeControlValue.value.width }
56
+ value={ value?.value.width }
66
57
  setValue={ setStrokeWidth }
67
58
  >
68
59
  <SizeControl units={ units } />
@@ -71,7 +62,7 @@ export const StrokeControl = createControl( () => {
71
62
  <Control
72
63
  bind="color"
73
64
  label={ __( 'Stroke Color', 'elementor' ) }
74
- value={ value?.value.color ?? defaultStrokeControlValue.value.color }
65
+ value={ value?.value.color }
75
66
  setValue={ setStrokeColor }
76
67
  >
77
68
  <ColorControl />
@@ -80,19 +71,7 @@ export const StrokeControl = createControl( () => {
80
71
  );
81
72
  } );
82
73
 
83
- const Control = < T extends PropValue >( {
84
- bind,
85
- value,
86
- setValue,
87
- label,
88
- children,
89
- }: {
90
- bind: string;
91
- value: T;
92
- setValue: ( v: T ) => void;
93
- label: string;
94
- children: React.ReactNode;
95
- } ) => (
74
+ const Control = < T extends PropValue >( { bind, value, setValue, label, children }: StrokeProps< T > ) => (
96
75
  <BoundPropProvider bind={ bind } value={ value } setValue={ setValue as SetContextValue }>
97
76
  <Grid container gap={ 2 } alignItems="center" flexWrap="nowrap">
98
77
  <Grid item xs={ 6 }>
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export { FontFamilyControl } from './controls/font-family-control';
16
16
 
17
17
  // components
18
18
  export { ControlLabel } from './components/control-label';
19
+ export { ControlToggleButtonGroup } from './components/control-toggle-button-group';
19
20
 
20
21
  // types
21
22
  export type { ControlComponent } from './create-control';