Skip to content

Instantly share code, notes, and snippets.

@Karan-Palan
Created March 15, 2024 13:52
Show Gist options
  • Save Karan-Palan/9522bce7a429d0ef4b0bcdcbf8d0bf1b to your computer and use it in GitHub Desktop.
Save Karan-Palan/9522bce7a429d0ef4b0bcdcbf8d0bf1b to your computer and use it in GitHub Desktop.
Example code for a palette in MB4
import React, { useState, useEffect } from 'react';
interface PaletteProps {
onItemClick: (item: any) => void;
}
const Palette: React.FC<PaletteProps> = (props) => {
const [items, setItems] = useState<any[]>([]);
useEffect(() => {
// Fetch the items for the palette from the server or local storage
const items = fetchPaletteItems();
// Set the state to update the component with the fetched items
setItems(items);
}, []);
const fetchPaletteItems = (): any[] => {
// Make a request to the server or local storage to get the palette items
const items: any[] = [];
// Return the fetched items
return items;
};
const handleItemClick = (item: any) => {
// Trigger an action with the selected item
props.onItemClick(item);
};
return (
<div>
<h2>Palette</h2>
{items.map(item => (
<div key={item.id} onClick={() => handleItemClick(item)}>
{item.name}
</div>
))}
</div>
);
};
export default Palette;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment