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
|
|
|
|
2023-12-18 09:47:24 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2023-11-15 03:50:05 +00:00
|
|
|
export type DropdownContext = {
|
2023-12-22 00:34:28 +00:00
|
|
|
selectType: 'multiple' | 'single';
|
2024-01-03 07:52:45 +00:00
|
|
|
defaultPlaceholder: string;
|
2023-12-22 00:34:28 +00:00
|
|
|
item: {
|
2023-11-27 04:26:01 +00:00
|
|
|
label: string;
|
|
|
|
value: string;
|
2023-11-15 03:50:05 +00:00
|
|
|
};
|
2023-12-22 00:34:28 +00:00
|
|
|
selectedItems: {
|
2023-11-27 04:26:01 +00:00
|
|
|
label: string | null;
|
|
|
|
value: string | null;
|
2023-12-22 00:34:28 +00:00
|
|
|
}[];
|
2023-11-27 04:26:01 +00:00
|
|
|
hoveredItem: {
|
|
|
|
label: string | null;
|
|
|
|
value: string | null;
|
|
|
|
};
|
|
|
|
isOpen: boolean;
|
2023-11-15 03:50:05 +00:00
|
|
|
};
|
|
|
|
|
2023-12-18 09:47:24 +00:00
|
|
|
type DropdownStore = {
|
2023-11-29 05:27:59 +00:00
|
|
|
state: {
|
2023-12-18 09:47:24 +00:00
|
|
|
placeholderText: string;
|
|
|
|
isSelected: boolean;
|
|
|
|
};
|
2023-11-15 03:50:05 +00:00
|
|
|
|
|
|
|
actions: {
|
2023-12-18 09:47:24 +00:00
|
|
|
toggleIsOpen: () => void;
|
|
|
|
selectDropdownItem: ( event: MouseEvent ) => void;
|
2023-12-22 00:34:28 +00:00
|
|
|
unselectDropdownItem: ( event: MouseEvent ) => void;
|
2023-12-18 09:47:24 +00:00
|
|
|
};
|
|
|
|
};
|
2023-11-27 04:26:01 +00:00
|
|
|
|
2023-12-22 00:34:28 +00:00
|
|
|
store< DropdownStore >( 'woocommerce/interactivity-dropdown', {
|
|
|
|
state: {
|
|
|
|
get placeholderText(): string {
|
2024-01-03 07:52:45 +00:00
|
|
|
const { selectType, selectedItems, defaultPlaceholder } =
|
2023-12-22 00:34:28 +00:00
|
|
|
getContext< DropdownContext >();
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-12-22 00:34:28 +00:00
|
|
|
if ( selectType === 'single' ) {
|
|
|
|
return selectedItems?.length && selectedItems[ 0 ].label
|
|
|
|
? selectedItems[ 0 ]?.label
|
2024-01-03 07:52:45 +00:00
|
|
|
: defaultPlaceholder;
|
2023-12-22 00:34:28 +00:00
|
|
|
} else if (
|
|
|
|
selectType === 'multiple' &&
|
|
|
|
selectedItems.length === 0
|
|
|
|
) {
|
2024-01-03 07:52:45 +00:00
|
|
|
return defaultPlaceholder;
|
2023-12-22 00:34:28 +00:00
|
|
|
}
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-12-22 00:34:28 +00:00
|
|
|
return '';
|
|
|
|
},
|
2023-11-15 03:50:05 +00:00
|
|
|
|
2023-12-22 00:34:28 +00:00
|
|
|
get isSelected(): boolean {
|
|
|
|
const { item, selectedItems } = getContext< DropdownContext >();
|
|
|
|
|
|
|
|
return selectedItems.some( ( i ) => {
|
|
|
|
return i.value === item.value && i.label === item.label;
|
|
|
|
} );
|
2023-12-18 09:47:24 +00:00
|
|
|
},
|
2023-12-22 00:34:28 +00:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
toggleIsOpen: () => {
|
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
context.isOpen = ! context.isOpen;
|
|
|
|
},
|
|
|
|
unselectDropdownItem: ( event: MouseEvent ) => {
|
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
|
|
|
|
const {
|
|
|
|
item: { label, value },
|
|
|
|
selectedItems,
|
|
|
|
} = context;
|
|
|
|
|
|
|
|
const items = selectedItems || [];
|
|
|
|
const selectedItemIndex = items.findIndex(
|
|
|
|
( item ) => item.value === value && item.label === label
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( selectedItemIndex !== -1 ) {
|
|
|
|
items.splice( selectedItemIndex, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
},
|
|
|
|
selectDropdownItem: ( event: MouseEvent ) => {
|
|
|
|
const context = getContext< DropdownContext >();
|
|
|
|
|
|
|
|
const {
|
|
|
|
item: { label, value },
|
|
|
|
selectedItems,
|
|
|
|
} = context;
|
|
|
|
|
|
|
|
// check if item already selected
|
|
|
|
const selectedItemIndex = selectedItems.findIndex(
|
|
|
|
( item ) => item.value === value && item.label === label
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( selectedItemIndex !== -1 ) {
|
|
|
|
selectedItems.splice( selectedItemIndex, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( context.selectType === 'single' && selectedItemIndex === -1 ) {
|
|
|
|
selectedItems.splice( 0, 1, { label, value } );
|
|
|
|
} else if ( selectedItemIndex === -1 ) {
|
|
|
|
selectedItems.push( {
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
context.isOpen = false;
|
|
|
|
event.stopPropagation();
|
2023-11-15 03:50:05 +00:00
|
|
|
},
|
2023-12-22 00:34:28 +00:00
|
|
|
},
|
|
|
|
} );
|