All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.js.components.CheckboxMultiSelect.tsx Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

import {ClayCheckbox} from '@clayui/form';
import ClayMultiSelect from '@clayui/multi-select';
import React from 'react';

type TItem = {
	key?: string;
	label?: string;
	value?: string;
	[propName: string]: any;
};

type TItems = Array;

interface IProps extends React.ComponentProps {
	items: TItems;
	onItemsChange: Exclude<
		React.ComponentProps['onItemsChange'],
		undefined
	>;
	sourceItems: TItems;
	value: Exclude<
		React.ComponentProps['value'],
		undefined
	>;
}

const isChecked = (items: TItems, item: TItem) => {
	return !!items.find((val) => val.value === item.value);
};

function CheckboxMultiSelect({
	items,
	onItemsChange,
	sourceItems,
	value,
	...otherProps
}: IProps) {
	const toggleItemChecked = (item: TItem) => {
		if (!isChecked(items, item)) {
			onItemsChange([
				...items,
				sourceItems.find(
					(entry) => item.value === entry.value
				) as TItems[0],
			]);
		}
		else {
			onItemsChange(items.filter((entry) => item.value !== entry.value));
		}
	};

	return (
		
			{(item: any) => (
				 {
						event.preventDefault();

						toggleItemChecked(item);
					}}
					textValue={item.label}
				>
					
{ event.stopPropagation(); toggleItemChecked(item); }} />
{item.label}
)}
); } export default CheckboxMultiSelect;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy