A React component for filtering and displaying categorized product lists. Users can search products and filter out-of-stock items.
React application demonstrating state management and data filtering with controlled components. Implements a classic product filtering use case with an intuitive user interface.
- 🔍 Real-time product search
- ✅ In-stock/out-of-stock filtering
- 📊 Category-based display
- 🎨 Visual indication of out-of-stock products (red text)
- ⚛️ Modern React architecture with functional components and hooks
src/
├── components/
│ ├── products/
│ │ ├── ProductTable.jsx
│ │ ├── ProductRow.jsx
│ │ └── ProductCategoryRow.jsx
│ └── searching/
│ ├── SearchBar.jsx
│ ├── Input.jsx
│ └── CheckBox.jsx
└── App.jsx
Main component that:
- Maintains global state (search and stock filter)
- Handles product filtering logic
- Orchestrates child components
Composite search bar including:
- Search field (
Input
) - Checkbox for filtering in-stock products (
CheckBox
)
Product table that:
- Organizes products by categories
- Displays category headers
- Manages product row display
ProductRow
: Displays a product lineProductCategoryRow
: Displays a category headerInput
: Controlled input componentCheckBox
: Controlled checkbox component
States are managed at the App.jsx
level:
const [stockedOnly, setStockedOnly] = useState(false);
const [search, setSearch] = useState("");
Data flow is unidirectional:
- User interacts with
SearchBar
- Changes trigger state setters
- New state triggers re-render
- Product list is filtered based on criteria
ProductTable
displays filtered products
- Clone the repository
git clone [repository-url]
- Install dependencies
npm install
- Run the application
npm run dev
- React Hooks usage (
useState
) - Controlled components for forms
- JSDoc typed props
- Efficient filtering using
Array.filter()
- Modular and reusable component structure
- Consistent props and event naming conventions
const PRODUCTS = [
{category: "Fruits", price: "$1", stocked: true, name: "Apple"},
{category: "Fruits", price: "$1", stocked: true, name: "Dragonfruit"},
{category: "Vegetables", price: "$2", stocked: true, name: "Spinach"},
// ...
]
interface ProductTableProps {
products: Array<{
category: string,
price: string,
stocked: boolean,
name: string
}>
}
interface SearchBarProps {
search: string,
onSearchChange: (value: string) => void,
stocked: boolean,
onStockedOnlyChange: (value: boolean) => void
}
Contributions are welcome! Please:
- Fork the project
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
MIT License - feel free to use this code for your own projects.