create a complex react component
- User
create a complex react component
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
create a complex react component
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a complex React component involves designing a reusable piece of UI that can handle various data and states. Here's an example of a complex component named ProductTable, which displays a table of products with sorting and filtering functionality:
import React, { useState, useEffect } from 'react';
const ProductTable = ({ products, columns, initialSortKey, initialSortOrder }) => {
const [sortedProducts, setSortedProducts] = useState(products);
const [sortKey, setSortKey] = useState(initialSortKey);
const [sortOrder, setSortOrder] = useState(initialSortOrder);
useEffect(() => {
setSortedProducts(products);
}, [products]);
const handleSort = (key) => {
if (key === sortKey) {
setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc');
} else {
setSortKey(key);
setSortOrder('asc');
}
setSortedProducts(products.sort((a, b) => {
if (a[key] < b[key]) return sortOrder === 'asc' ? -1 : 1;
if (a[key] > b[key]) return sortOrder === 'asc' ? 1 : -1;
return 0;
}));
};
const handleFilter = (filterText) => {
setSortedProducts(products.filter(product =>
Object.values(product).some(value =>
String(value).toLowerCase().includes(filterText.toLowerCase())
)
));
};
return (
<div>
<input
type="text"
placeholder="Search by any field..."
onChange={(e) => handleFilter(e.target.value)}
/>
<table>
<thead>
<tr>
{columns.map(column => (
<th key={column.key} onClick={() => handleSort(column.key)}>
{column.label}
{sortKey === column.key && (
<span>{sortOrder === 'asc' ? '↑' : '↓'}</span>
)}
</th>
))}
</tr>
</thead>
<tbody>
{sortedProducts.map(product => (
<tr key={product.id}>
{columns.map(column => (
<td key={column.key}>{product[column.key]}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
};
export default ProductTable;
In this example:
ProductTable component accepts three props: products (an array of product objects), columns (an array of column objects defining the table headers), and initialSortKey and initialSortOrder for sorting the products by default.useState hook to manage the sorted products, sort key, and sort order.handleSort function handles the sorting of products based on the clicked column header.handleFilter function filters the products based on the user's input.This component can be used in another file like this:
import React from 'react';
import ProductTable from './ProductTable';
const products = [
// Array of product objects
];
const columns = [
{ key: 'id', label: 'ID' },
{ key: 'name', label: 'Name' },
{ key: 'price', label: 'Price' },
];
function App() {
return (
<div className="App">
<ProductTable
products={products}
columns={columns}
initialSortKey="name"
initialSortOrder="asc"
/>
</div>
);
}
export default App;