META-INF.resources.js.SelectFolder.js Maven / Gradle / Ivy
/**
* 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 {TreeView as ClayTreeView} from '@clayui/core';
import ClayEmptyState from '@clayui/empty-state';
import ClayForm, {ClayInput} from '@clayui/form';
import ClayIcon from '@clayui/icon';
import ClayLayout from '@clayui/layout';
import {getOpener} from 'frontend-js-web';
import React, {useMemo, useState} from 'react';
const SelectFolder = ({itemSelectorSaveEvent, nodes}) => {
const [filterQuery, setFilterQuery] = useState('');
const handleSelectionChange = (item) => {
getOpener().Liferay.fire(itemSelectorSaveEvent, {
data: {
resourceid: item.id,
resourcename: item.name,
},
});
};
return (
setFilterQuery(event.target.value)
}
placeholder={`${Liferay.Language.get('search')}`}
type="text"
/>
);
};
function FolderTree({filterQuery, handleSelectionChange, items: initialItems}) {
const [items, setItems] = useState(initialItems);
const nodeByName = (items, name) => {
return items.reduce(function reducer(acc, item) {
if (item.name?.toLowerCase().includes(name.toLowerCase())) {
acc.push(item);
}
else if (item.children) {
acc.concat(item.children.reduce(reducer, acc));
}
return acc;
}, []);
};
const filteredItems = useMemo(() => {
if (!filterQuery) {
return items;
}
return nodeByName(items, filterQuery);
}, [items, filterQuery]);
const onClick = (event, item) => {
event.preventDefault();
handleSelectionChange(item);
};
const onKeyUp = (event, item) => {
if (event.key === ' ' || event.key === 'Enter') {
event.preventDefault();
handleSelectionChange(item);
}
};
return filteredItems.length ? (
{(item) => (
onClick(event, item)}
onKeyUp={(event) => onKeyUp(event, item)}
>
{item.name}
{(item) => (
onClick(event, item)}
onKeyUp={(event) => onKeyUp(event, item)}
>
{item.name}
)}
)}
) : (
);
}
export default SelectFolder;
© 2015 - 2024 Weber Informatics LLC | Privacy Policy