@adaptabletools/adaptable
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
36 lines (35 loc) • 1.59 kB
JavaScript
import React from 'react';
import { useDispatch } from 'react-redux';
import SimpleButton from '../components/SimpleButton';
export const getObjectTagsViewItems = (object, api) => {
if (!api.internalApi.shouldDisplayTagSections() || !object?.Tags?.length) {
return;
}
const currentLayoutName = api.layoutApi.getCurrentLayoutName();
const hasCurrentLayoutAsTag = (object?.Tags ?? [])?.includes(currentLayoutName);
return {
name: 'Tags',
values: object?.Tags?.map((tag) => api.internalApi.getLabelForTag(tag)),
viewAfter: (props) => {
const dispatch = useDispatch();
const viewOptions = props.module.getViewProperties();
const handleToggleTag = React.useCallback(() => {
let tags = props.data.Tags ?? [];
if (hasCurrentLayoutAsTag) {
// remove layout
tags = tags.filter((tag) => tag !== currentLayoutName);
}
else {
tags.push(currentLayoutName);
}
const newObject = {
...props.data,
Tags: tags,
};
const editAction = viewOptions.getEditAction(newObject);
dispatch(editAction);
}, [hasCurrentLayoutAsTag, props.data]);
return (React.createElement(SimpleButton, { onClick: handleToggleTag, mt: 1, icon: hasCurrentLayoutAsTag ? 'minus' : 'plus' }, hasCurrentLayoutAsTag ? 'Remove from Layout' : 'Add to Layout'));
},
};
};