2023-11-15 03:50:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-11-27 04:26:01 +00:00
|
|
|
import { getContext, store } from '@woocommerce/interactivity';
|
2023-11-15 03:50:05 +00:00
|
|
|
|
|
|
|
export type DropdownContext = {
|
2023-11-27 04:26:01 +00:00
|
|
|
currentItem: {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
2023-11-15 03:50:05 +00:00
|
|
|
};
|
2023-11-27 04:26:01 +00:00
|
|
|
selectedItem: {
|
|
|
|
label: string | null;
|
|
|
|
value: string | null;
|
|
|
|
};
|
|
|
|
hoveredItem: {
|
|
|
|
label: string | null;
|
|
|
|
value: string | null;
|
|
|
|
};
|
|
|
|
isOpen: boolean;
|
2023-11-15 03:50:05 +00:00
|
|
|
};
|
|
|
|
|
2023-11-27 04:26:01 +00:00
|
|
|
store( 'woocommerce/interactivity-dropdown', {
|
2023-11-29 05:27:59 +00:00
|
|
|
state: {
|
|
|
|
get placeholderText() {
|
2023-11-27 04:26:01 +00:00
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
const { selectedItem } = context;
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-11-27 04:26:01 +00:00
|
|
|
return selectedItem.label || 'Select an option';
|
|
|
|
},
|
2023-11-29 05:27:59 +00:00
|
|
|
get isSelected() {
|
2023-11-27 04:26:01 +00:00
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
|
|
|
|
const {
|
|
|
|
currentItem: { value },
|
|
|
|
} = context;
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-11-27 04:26:01 +00:00
|
|
|
return (
|
|
|
|
context.selectedItem.value === value ||
|
|
|
|
context.hoveredItem.value === value
|
|
|
|
);
|
2023-11-15 03:50:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
2023-11-27 04:26:01 +00:00
|
|
|
toggleIsOpen: () => {
|
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
|
|
|
|
context.isOpen = ! context.isOpen;
|
|
|
|
},
|
|
|
|
selectDropdownItem: ( event: MouseEvent ) => {
|
|
|
|
const context = getContext< DropdownContext >();
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-11-27 04:26:01 +00:00
|
|
|
const {
|
|
|
|
currentItem: { label, value },
|
|
|
|
} = context;
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-11-27 04:26:01 +00:00
|
|
|
const { selectedItem } = context;
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-11-27 04:26:01 +00:00
|
|
|
if (
|
|
|
|
selectedItem.value === value &&
|
|
|
|
selectedItem.label === label
|
|
|
|
) {
|
|
|
|
context.selectedItem = {
|
2023-11-15 03:50:05 +00:00
|
|
|
label: null,
|
|
|
|
value: null,
|
|
|
|
};
|
2023-11-27 04:26:01 +00:00
|
|
|
} else {
|
|
|
|
context.selectedItem = { label, value };
|
|
|
|
}
|
|
|
|
|
|
|
|
context.isOpen = false;
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
},
|
|
|
|
addHoverClass: () => {
|
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
|
|
|
|
const {
|
|
|
|
currentItem: { label, value },
|
|
|
|
} = context;
|
|
|
|
|
|
|
|
context.hoveredItem = { label, value };
|
|
|
|
},
|
|
|
|
removeHoverClass: () => {
|
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
|
|
|
|
context.hoveredItem = {
|
|
|
|
label: null,
|
|
|
|
value: null,
|
|
|
|
};
|
2023-11-15 03:50:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} );
|