@wordpress/components 21.0.6 → 21.0.7
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/CHANGELOG.md +18 -0
- package/build/autocomplete/index.js +11 -9
- package/build/autocomplete/index.js.map +1 -1
- package/build/circular-option-picker/index.js +14 -14
- package/build/circular-option-picker/index.js.map +1 -1
- package/build/color-palette/index.js +83 -30
- package/build/color-palette/index.js.map +1 -1
- package/build/color-palette/styles.js +3 -3
- package/build/color-palette/styles.js.map +1 -1
- package/build/color-palette/types.js +6 -0
- package/build/color-palette/types.js.map +1 -0
- package/build/form-token-field/index.js +12 -10
- package/build/form-token-field/index.js.map +1 -1
- package/build/popover/index.js +1 -26
- package/build/popover/index.js.map +1 -1
- package/build-module/autocomplete/index.js +10 -9
- package/build-module/autocomplete/index.js.map +1 -1
- package/build-module/circular-option-picker/index.js +14 -14
- package/build-module/circular-option-picker/index.js.map +1 -1
- package/build-module/color-palette/index.js +81 -28
- package/build-module/color-palette/index.js.map +1 -1
- package/build-module/color-palette/styles.js +3 -3
- package/build-module/color-palette/styles.js.map +1 -1
- package/build-module/color-palette/types.js +2 -0
- package/build-module/color-palette/types.js.map +1 -0
- package/build-module/form-token-field/index.js +11 -10
- package/build-module/form-token-field/index.js.map +1 -1
- package/build-module/popover/index.js +1 -26
- package/build-module/popover/index.js.map +1 -1
- package/build-types/border-control/types.d.ts +1 -1
- package/build-types/border-control/types.d.ts.map +1 -1
- package/build-types/circular-option-picker/index.d.ts +4 -24
- package/build-types/circular-option-picker/index.d.ts.map +1 -1
- package/build-types/color-palette/index.d.ts +33 -18
- package/build-types/color-palette/index.d.ts.map +1 -1
- package/build-types/color-palette/stories/index.d.ts +21 -0
- package/build-types/color-palette/stories/index.d.ts.map +1 -0
- package/build-types/color-palette/styles.d.ts +2 -1
- package/build-types/color-palette/styles.d.ts.map +1 -1
- package/build-types/color-palette/test/index.d.ts +2 -0
- package/build-types/color-palette/test/index.d.ts.map +1 -0
- package/build-types/color-palette/types.d.ts +86 -0
- package/build-types/color-palette/types.d.ts.map +1 -0
- package/build-types/confirm-dialog/component.d.ts +4 -4
- package/build-types/form-token-field/index.d.ts.map +1 -1
- package/build-types/popover/index.d.ts.map +1 -1
- package/build-types/popover/types.d.ts +0 -14
- package/build-types/popover/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/autocomplete/index.js +18 -9
- package/src/border-control/types.ts +1 -1
- package/src/circular-option-picker/index.js +14 -20
- package/src/color-palette/README.md +51 -49
- package/src/color-palette/{index.js → index.tsx} +132 -51
- package/src/color-palette/stories/{index.js → index.tsx} +38 -27
- package/src/color-palette/{styles.js → styles.ts} +0 -0
- package/src/color-palette/test/__snapshots__/index.tsx.snap +270 -0
- package/src/color-palette/test/index.tsx +164 -0
- package/src/color-palette/types.ts +93 -0
- package/src/form-token-field/index.tsx +21 -10
- package/src/form-token-field/test/index.tsx +167 -73
- package/src/popover/README.md +3 -9
- package/src/popover/index.tsx +1 -25
- package/src/popover/types.ts +0 -14
- package/tsconfig.tsbuildinfo +1 -1
- package/src/color-palette/test/__snapshots__/index.js.snap +0 -1207
- package/src/color-palette/test/index.js +0 -118
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { render, screen, within } from '@testing-library/react';
|
|
5
|
+
import userEvent from '@testing-library/user-event';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Internal dependencies
|
|
9
|
+
*/
|
|
10
|
+
import ColorPalette from '..';
|
|
11
|
+
|
|
12
|
+
describe( 'ColorPalette', () => {
|
|
13
|
+
const colors = [
|
|
14
|
+
{ name: 'red', color: '#f00' },
|
|
15
|
+
{ name: 'white', color: '#fff' },
|
|
16
|
+
{ name: 'blue', color: '#00f' },
|
|
17
|
+
];
|
|
18
|
+
const currentColor = '#f00';
|
|
19
|
+
const onChange = jest.fn();
|
|
20
|
+
|
|
21
|
+
beforeEach( () => {
|
|
22
|
+
onChange.mockClear();
|
|
23
|
+
} );
|
|
24
|
+
|
|
25
|
+
test( 'should render a dynamic toolbar of colors', () => {
|
|
26
|
+
const { container } = render(
|
|
27
|
+
<ColorPalette
|
|
28
|
+
colors={ colors }
|
|
29
|
+
value={ currentColor }
|
|
30
|
+
onChange={ onChange }
|
|
31
|
+
/>
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
expect( container ).toMatchSnapshot();
|
|
35
|
+
} );
|
|
36
|
+
|
|
37
|
+
test( 'should render three color button options', () => {
|
|
38
|
+
render(
|
|
39
|
+
<ColorPalette
|
|
40
|
+
colors={ colors }
|
|
41
|
+
value={ currentColor }
|
|
42
|
+
onChange={ onChange }
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
expect(
|
|
47
|
+
screen.getAllByRole( 'button', { name: /^Color:/ } )
|
|
48
|
+
).toHaveLength( 3 );
|
|
49
|
+
} );
|
|
50
|
+
|
|
51
|
+
test( 'should call onClick on an active button with undefined', async () => {
|
|
52
|
+
const user = userEvent.setup( {
|
|
53
|
+
advanceTimers: jest.advanceTimersByTime,
|
|
54
|
+
} );
|
|
55
|
+
|
|
56
|
+
render(
|
|
57
|
+
<ColorPalette
|
|
58
|
+
colors={ colors }
|
|
59
|
+
value={ currentColor }
|
|
60
|
+
onChange={ onChange }
|
|
61
|
+
/>
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
await user.click(
|
|
65
|
+
screen.getByRole( 'button', { name: /^Color:/, pressed: true } )
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect( onChange ).toHaveBeenCalledTimes( 1 );
|
|
69
|
+
expect( onChange ).toHaveBeenCalledWith( undefined );
|
|
70
|
+
} );
|
|
71
|
+
|
|
72
|
+
test( 'should call onClick on an inactive button', async () => {
|
|
73
|
+
const user = userEvent.setup( {
|
|
74
|
+
advanceTimers: jest.advanceTimersByTime,
|
|
75
|
+
} );
|
|
76
|
+
|
|
77
|
+
render(
|
|
78
|
+
<ColorPalette
|
|
79
|
+
colors={ colors }
|
|
80
|
+
value={ currentColor }
|
|
81
|
+
onChange={ onChange }
|
|
82
|
+
/>
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
await user.click(
|
|
86
|
+
screen.getAllByRole( 'button', {
|
|
87
|
+
name: /^Color:/,
|
|
88
|
+
pressed: false,
|
|
89
|
+
} )[ 0 ]
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect( onChange ).toHaveBeenCalledTimes( 1 );
|
|
93
|
+
expect( onChange ).toHaveBeenCalledWith( '#fff' );
|
|
94
|
+
} );
|
|
95
|
+
|
|
96
|
+
test( 'should call onClick with undefined, when the clearButton onClick is triggered', async () => {
|
|
97
|
+
const user = userEvent.setup( {
|
|
98
|
+
advanceTimers: jest.advanceTimersByTime,
|
|
99
|
+
} );
|
|
100
|
+
|
|
101
|
+
render(
|
|
102
|
+
<ColorPalette
|
|
103
|
+
colors={ colors }
|
|
104
|
+
value={ currentColor }
|
|
105
|
+
onChange={ onChange }
|
|
106
|
+
/>
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
await user.click( screen.getByRole( 'button', { name: 'Clear' } ) );
|
|
110
|
+
|
|
111
|
+
expect( onChange ).toHaveBeenCalledTimes( 1 );
|
|
112
|
+
expect( onChange ).toHaveBeenCalledWith( undefined );
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
test( 'should allow disabling custom color picker', () => {
|
|
116
|
+
const { container } = render(
|
|
117
|
+
<ColorPalette
|
|
118
|
+
colors={ colors }
|
|
119
|
+
disableCustomColors
|
|
120
|
+
value={ currentColor }
|
|
121
|
+
onChange={ onChange }
|
|
122
|
+
/>
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
expect( container ).toMatchSnapshot();
|
|
126
|
+
} );
|
|
127
|
+
|
|
128
|
+
test( 'should render dropdown and its content', async () => {
|
|
129
|
+
const user = userEvent.setup( {
|
|
130
|
+
advanceTimers: jest.advanceTimersByTime,
|
|
131
|
+
} );
|
|
132
|
+
|
|
133
|
+
render(
|
|
134
|
+
<ColorPalette
|
|
135
|
+
colors={ colors }
|
|
136
|
+
value={ currentColor }
|
|
137
|
+
onChange={ onChange }
|
|
138
|
+
/>
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
await user.click(
|
|
142
|
+
screen.getByRole( 'button', {
|
|
143
|
+
name: /^Custom color picker/,
|
|
144
|
+
expanded: false,
|
|
145
|
+
} )
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const dropdownButton = screen.getByRole( 'button', {
|
|
149
|
+
name: /^Custom color picker/,
|
|
150
|
+
expanded: true,
|
|
151
|
+
} );
|
|
152
|
+
|
|
153
|
+
expect( dropdownButton ).toBeVisible();
|
|
154
|
+
|
|
155
|
+
expect(
|
|
156
|
+
within( dropdownButton ).getByText( colors[ 0 ].name )
|
|
157
|
+
).toBeVisible();
|
|
158
|
+
expect(
|
|
159
|
+
within( dropdownButton ).getByText(
|
|
160
|
+
colors[ 0 ].color.replace( '#', '' )
|
|
161
|
+
)
|
|
162
|
+
).toBeVisible();
|
|
163
|
+
} );
|
|
164
|
+
} );
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CSSProperties, MouseEventHandler, ReactNode } from 'react';
|
|
5
|
+
|
|
6
|
+
type OnColorChange = ( newColor?: string ) => void;
|
|
7
|
+
|
|
8
|
+
export type ColorObject = {
|
|
9
|
+
name: string;
|
|
10
|
+
color: NonNullable< CSSProperties[ 'color' ] >;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type PaletteObject = {
|
|
14
|
+
name: string;
|
|
15
|
+
colors: ColorObject[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type PaletteProps = {
|
|
19
|
+
className?: string;
|
|
20
|
+
clearColor: () => void;
|
|
21
|
+
onChange: OnColorChange;
|
|
22
|
+
value?: string;
|
|
23
|
+
actions?: ReactNode;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type SinglePaletteProps = PaletteProps & {
|
|
27
|
+
colors: ColorObject[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type MultiplePalettesProps = PaletteProps & {
|
|
31
|
+
colors: PaletteObject[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// TODO: should extend `Dropdown`'s props once it gets refactored to TypeScript
|
|
35
|
+
export type CustomColorPickerDropdownProps = {
|
|
36
|
+
isRenderedInSidebar: boolean;
|
|
37
|
+
renderContent: () => ReactNode;
|
|
38
|
+
popoverProps?: string[];
|
|
39
|
+
renderToggle: ( props: {
|
|
40
|
+
isOpen: boolean;
|
|
41
|
+
onToggle: MouseEventHandler< HTMLButtonElement >;
|
|
42
|
+
} ) => ReactNode;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type ColorPaletteProps = {
|
|
46
|
+
/**
|
|
47
|
+
* Whether the palette should have a clearing button.
|
|
48
|
+
*
|
|
49
|
+
* @default true
|
|
50
|
+
*/
|
|
51
|
+
clearable?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Array with the colors to be shown. When displaying multiple color palettes
|
|
54
|
+
* to choose from, the format of the array changes from an array of colors
|
|
55
|
+
* objects, to an array of color palettes.
|
|
56
|
+
*
|
|
57
|
+
* @default []
|
|
58
|
+
*/
|
|
59
|
+
colors?: ( PaletteObject | ColorObject )[];
|
|
60
|
+
/**
|
|
61
|
+
* Whether to allow the user to pick a custom color on top of the predefined
|
|
62
|
+
* choices (defined via the `colors` prop).
|
|
63
|
+
*
|
|
64
|
+
* @default false
|
|
65
|
+
*/
|
|
66
|
+
disableCustomColors?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Whether the color picker should display the alpha channel
|
|
69
|
+
* both in the bottom inputs as well as in the color picker itself.
|
|
70
|
+
*/
|
|
71
|
+
enableAlpha?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Callback called when a color is selected.
|
|
74
|
+
*/
|
|
75
|
+
onChange: OnColorChange;
|
|
76
|
+
/**
|
|
77
|
+
* Currently active value.
|
|
78
|
+
*/
|
|
79
|
+
value?: string;
|
|
80
|
+
/**
|
|
81
|
+
* Whether the colors prop is an array of color palettes,
|
|
82
|
+
* rather than an array of color objects.
|
|
83
|
+
*
|
|
84
|
+
* @default false
|
|
85
|
+
*/
|
|
86
|
+
__experimentalHasMultipleOrigins?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Whether this is rendered in the sidebar.
|
|
89
|
+
*
|
|
90
|
+
* @default false
|
|
91
|
+
*/
|
|
92
|
+
__experimentalIsRenderedInSidebar?: boolean;
|
|
93
|
+
};
|
|
@@ -11,6 +11,17 @@ import { useEffect, useRef, useState } from '@wordpress/element';
|
|
|
11
11
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
|
12
12
|
import { useDebounce, useInstanceId, usePrevious } from '@wordpress/compose';
|
|
13
13
|
import { speak } from '@wordpress/a11y';
|
|
14
|
+
import {
|
|
15
|
+
BACKSPACE,
|
|
16
|
+
ENTER,
|
|
17
|
+
UP,
|
|
18
|
+
DOWN,
|
|
19
|
+
LEFT,
|
|
20
|
+
RIGHT,
|
|
21
|
+
SPACE,
|
|
22
|
+
DELETE,
|
|
23
|
+
ESCAPE,
|
|
24
|
+
} from '@wordpress/keycodes';
|
|
14
25
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
15
26
|
|
|
16
27
|
/**
|
|
@@ -172,34 +183,34 @@ export function FormTokenField( props: FormTokenFieldProps ) {
|
|
|
172
183
|
if ( event.defaultPrevented ) {
|
|
173
184
|
return;
|
|
174
185
|
}
|
|
175
|
-
switch ( event.
|
|
176
|
-
case
|
|
186
|
+
switch ( event.keyCode ) {
|
|
187
|
+
case BACKSPACE:
|
|
177
188
|
preventDefault = handleDeleteKey( deleteTokenBeforeInput );
|
|
178
189
|
break;
|
|
179
|
-
case
|
|
190
|
+
case ENTER:
|
|
180
191
|
preventDefault = addCurrentToken();
|
|
181
192
|
break;
|
|
182
|
-
case
|
|
193
|
+
case LEFT:
|
|
183
194
|
preventDefault = handleLeftArrowKey();
|
|
184
195
|
break;
|
|
185
|
-
case
|
|
196
|
+
case UP:
|
|
186
197
|
preventDefault = handleUpArrowKey();
|
|
187
198
|
break;
|
|
188
|
-
case
|
|
199
|
+
case RIGHT:
|
|
189
200
|
preventDefault = handleRightArrowKey();
|
|
190
201
|
break;
|
|
191
|
-
case
|
|
202
|
+
case DOWN:
|
|
192
203
|
preventDefault = handleDownArrowKey();
|
|
193
204
|
break;
|
|
194
|
-
case
|
|
205
|
+
case DELETE:
|
|
195
206
|
preventDefault = handleDeleteKey( deleteTokenAfterInput );
|
|
196
207
|
break;
|
|
197
|
-
case
|
|
208
|
+
case SPACE:
|
|
198
209
|
if ( tokenizeOnSpace ) {
|
|
199
210
|
preventDefault = addCurrentToken();
|
|
200
211
|
}
|
|
201
212
|
break;
|
|
202
|
-
case
|
|
213
|
+
case ESCAPE:
|
|
203
214
|
preventDefault = handleEscapeKey( event );
|
|
204
215
|
break;
|
|
205
216
|
default:
|