### Installation and Setup
Source: https://context7.com/primefaces/primereact/llms.txt
Instructions for installing PrimeReact and setting up a theme for styled mode.
```APIDOC
## Installation
Install PrimeReact via npm, yarn, or pnpm.
```bash
# Using npm
npm install primereact
# Using yarn
yarn add primereact
# Using pnpm
pnpm add primereact
```
## Setup with Theme
Import a theme CSS file to enable styled mode with pre-built component styles.
```javascript
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
// Import theme (choose one)
import 'primereact/resources/themes/lara-light-cyan/theme.css';
// Optional: Import PrimeIcons
import 'primeicons/primeicons.css';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render();
```
```
--------------------------------
### Start Local Development Server
Source: https://github.com/primefaces/primereact/wiki/Building-From-Source
Starts the local development server to run the PrimeReact showcase, typically accessible at http://localhost:3000.
```bash
npm start
```
--------------------------------
### Implementing PrimeReact Menu Component
Source: https://context7.com/primefaces/primereact/llms.txt
This example demonstrates how to define menu items with icons and commands, and how to render them as a basic list, a grouped list, or a popup menu triggered by a button click.
```javascript
import React, { useRef } from 'react';
import { Menu } from 'primereact/menu';
import { Button } from 'primereact/button';
import { Toast } from 'primereact/toast';
export default function MenuDemo() {
const menu = useRef(null);
const toast = useRef(null);
const items = [
{
label: 'Documents',
items: [
{
label: 'New',
icon: 'pi pi-plus',
command: () => {
toast.current.show({ severity: 'success', summary: 'New', detail: 'File created' });
}
},
{
label: 'Search',
icon: 'pi pi-search'
}
]
},
{
label: 'Profile',
items: [
{
label: 'Settings',
icon: 'pi pi-cog'
},
{
label: 'Logout',
icon: 'pi pi-sign-out'
}
]
}
];
const basicItems = [
{ label: 'New', icon: 'pi pi-plus' },
{ label: 'Search', icon: 'pi pi-search' },
{ separator: true },
{ label: 'Delete', icon: 'pi pi-trash' }
];
return (
Basic
Grouped
Popup
);
}
```
--------------------------------
### Install Project Dependencies
Source: https://github.com/primefaces/primereact/wiki/Building-From-Source
Installs all required Node.js packages defined in the package.json file.
```bash
npm install
```
--------------------------------
### Setup PrimeReact with Theme and Icons
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates how to set up PrimeReact in a React application by importing a theme CSS file and optionally PrimeIcons. This enables styled mode with pre-built component styles.
```javascript
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
// Import theme (choose one)
import 'primereact/resources/themes/lara-light-cyan/theme.css';
// Optional: Import PrimeIcons
import 'primeicons/primeicons.css';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render();
```
--------------------------------
### Install PrimeReact using npm, yarn, or pnpm
Source: https://github.com/primefaces/primereact/blob/master/README.md
Demonstrates how to add the PrimeReact library to your project using different package managers. This is the first step to using PrimeReact components in your React application.
```bash
# Using npm
npm install primereact
# Using yarn
yarn add primereact
# Using pnpm
pnpm add primereact
```
--------------------------------
### PrimeReact InputText Component Examples
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates the usage of the PrimeReact InputText component, showcasing basic input, float labels, invalid states, key filtering for input validation, disabled states, and tooltips. Requires React and primereact.
```javascript
import React, { useState } from 'react';
import { InputText } from 'primereact/inputtext';
import { FloatLabel } from 'primereact/floatlabel';
export default function InputTextDemo() {
const [value, setValue] = useState('');
const [email, setEmail] = useState('');
const [username, setUsername] = useState('');
return (
{/* Basic Input */}
setValue(e.target.value)}
placeholder="Enter text"
/>
{/* With Float Label */}
setUsername(e.target.value)} />
{/* Invalid State */}
setEmail(e.target.value)}
invalid={!email.includes('@')}
placeholder="Email"
/>
{/* With Key Filter (only allow alphanumeric) */}
{/* Disabled */}
{/* With Tooltip */}
);
}
```
--------------------------------
### React FileUpload Component Examples
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates various modes and configurations for the FileUpload component, including basic, advanced, auto-upload, and custom templates. It utilizes PrimeReact's FileUpload and Toast components for user feedback and file handling.
```javascript
import React, { useRef } from 'react';
import { FileUpload } from 'primereact/fileupload';
import { Toast } from 'primereact/toast';
export default function FileUploadDemo() {
const toast = useRef(null);
const fileUploadRef = useRef(null);
const onUpload = () => {
toast.current.show({ severity: 'info', summary: 'Success', detail: 'File Uploaded' });
};
const onSelect = (e) => {
toast.current.show({ severity: 'info', summary: 'Selected', detail: `${e.files.length} file(s) selected` });
};
const onError = () => {
toast.current.show({ severity: 'error', summary: 'Error', detail: 'Upload failed' });
};
const onClear = () => {
toast.current.show({ severity: 'warn', summary: 'Cleared', detail: 'Files cleared' });
};
return (
{/* Basic Mode */}
Basic
{/* Advanced Mode */}
Advanced
Drag and drop files here to upload.}
/>
{/* Auto Upload */}
);
}
```
--------------------------------
### PrimeReact InputNumber: Basic and Configured Examples
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates the usage of the PrimeReact InputNumber component for various input types including basic integers, numbers without grouping, decimal handling with min/max fraction digits, and setting min/max boundaries. It also shows how to configure currency formatting for different locales (USD and EUR) and add prefixes or suffixes.
```javascript
import React, { useState } from 'react';
import { InputNumber } from 'primereact/inputnumber';
export default function InputNumberDemo() {
const [value1, setValue1] = useState(42723);
const [value2, setValue2] = useState(58151);
const [price, setPrice] = useState(1500);
const [percent, setPercent] = useState(50);
return (
);
}
```
--------------------------------
### Compile SASS Theme Files
Source: https://github.com/primefaces/primereact/wiki/Migration-Guide-for-Templates
This command compiles SASS files to generate CSS, typically used after updating theme files. Ensure you have SASS installed globally. The command updates files in the 'public/theme' directory.
```bash
sass --update public/theme:public/theme
```
--------------------------------
### PrimeReact Checkbox Component Implementation (JavaScript)
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates how to use the PrimeReact Checkbox component for single binary selection and dynamic grouping of options. It includes examples for handling checked states, managing selections in an array, and rendering disabled checkboxes. Dependencies include React and the PrimeReact Checkbox module.
```javascript
import React, { useState } from 'react';
import { Checkbox } from 'primereact/checkbox';
export default function CheckboxDemo() {
const [checked, setChecked] = useState(false);
const [ingredients, setIngredients] = useState([]);
const pizzaIngredients = [
{ name: 'Cheese', key: 'C' },
{ name: 'Mushroom', key: 'M' },
{ name: 'Pepper', key: 'P' },
{ name: 'Onion', key: 'O' }
];
const onIngredientsChange = (e) => {
let _ingredients = [...ingredients];
if (e.checked)
_ingredients.push(e.value);
else
_ingredients = _ingredients.filter(ingredient => ingredient.key !== e.value.key);
setIngredients(_ingredients);
};
return (
{/* Basic Checkbox */}
setChecked(e.checked)}
checked={checked}
/>
{/* Dynamic Checkboxes */}
{pizzaIngredients.map((ingredient) => (
item.key === ingredient.key)}
/>
))}
{/* Disabled */}
);
}
```
--------------------------------
### Import PrimeReact Theme CSS
Source: https://github.com/primefaces/primereact/blob/master/README.md
Illustrates how to import a theme's CSS file for PrimeReact's styled mode. This is necessary to apply the visual styling of a chosen theme, like 'lara-light-cyan', to your components.
```javascript
// theme
import 'primereact/resources/themes/lara-light-cyan/theme.css';
```
--------------------------------
### Menu Component Configuration
Source: https://context7.com/primefaces/primereact/llms.txt
Details on how to configure the Menu component using the model property for items and sub-items.
```APIDOC
## Menu Component Configuration
### Description
The Menu component allows for the creation of navigation lists. It supports nested groups, icons, and command callbacks for individual items.
### Properties
- **model** (array) - Required - An array of menu items. Each item can contain 'label', 'icon', 'command', 'items' (for sub-menus), or 'separator'.
- **popup** (boolean) - Optional - Defines if the menu should be displayed as a popup. Default is false.
### Usage Example
```javascript
const items = [
{ label: 'New', icon: 'pi pi-plus', command: () => { /* logic */ } },
{ label: 'Search', icon: 'pi pi-search' }
];
```
### Popup Usage
To use as a popup, set the 'popup' property to true and use the 'toggle' method on the component reference.
```javascript
menu.current.toggle(e)} />
```
```
--------------------------------
### Implement TabView Component in PrimeReact
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates the usage of the TabView component, including basic tabs, controlled active indices, icon integration, and closable or disabled tab configurations. It requires importing TabView and TabPanel from primereact/tabview.
```javascript
import React, { useState } from 'react';
import { TabView, TabPanel } from 'primereact/tabview';
export default function TabViewDemo() {
const [activeIndex, setActiveIndex] = useState(0);
return (
Content of Tab 1
Content of Tab 2
Content of Tab 3
setActiveIndex(e.index)}>
Active index: {activeIndex}
Active index: {activeIndex}
);
}
```
--------------------------------
### Variable Naming and Declaration Standards
Source: https://github.com/primefaces/primereact/blob/master/CODING_STANDARDS.md
Demonstrates best practices for variable naming, using descriptive names, verb-based booleans, and proper declaration scoping.
```javascript
let numberOfUsers = 10;
let isLoggedIn = false;
let canAccess = true;
function exampleFunction() {
let x;
let y;
}
```
--------------------------------
### PrimeReact Tree Component Implementation (JavaScript/React)
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates the implementation of the PrimeReact Tree component in a React application. It shows how to initialize tree data, configure selection modes (single, checkbox), and enable filtering. The component is suitable for displaying hierarchical data structures.
```javascript
import React, { useState, useEffect } from 'react';
import { Tree } from 'primereact/tree';
export default function TreeDemo() {
const [nodes, setNodes] = useState([]);
const [selectedKey, setSelectedKey] = useState(null);
const [selectedKeys, setSelectedKeys] = useState(null);
useEffect(() => {
const data = [
{
key: '0',
label: 'Documents',
data: 'Documents Folder',
icon: 'pi pi-fw pi-inbox',
children: [
{
key: '0-0',
label: 'Work',
data: 'Work Folder',
icon: 'pi pi-fw pi-cog',
children: [
{ key: '0-0-0', label: 'Expenses.doc', icon: 'pi pi-fw pi-file', data: 'Expenses Document' },
{ key: '0-0-1', label: 'Resume.doc', icon: 'pi pi-fw pi-file', data: 'Resume Document' }
]
},
{
key: '0-1',
label: 'Home',
data: 'Home Folder',
icon: 'pi pi-fw pi-home',
children: [
{ key: '0-1-0', label: 'Invoices.txt', icon: 'pi pi-fw pi-file', data: 'Invoices' }
]
}
]
},
{
key: '1',
label: 'Events',
data: 'Events Folder',
icon: 'pi pi-fw pi-calendar',
children: [
{ key: '1-0', label: 'Meeting', icon: 'pi pi-fw pi-calendar-plus', data: 'Meeting' },
{ key: '1-1', label: 'Product Launch', icon: 'pi pi-fw pi-calendar-plus', data: 'Product Launch' }
]
}
];
setNodes(data);
}, []);
return (
{/* Basic Tree */}
Basic
{/* Single Selection */}
Single Selection
setSelectedKey(e.value)}
className="w-full md:w-30rem"
/>
{/* Multiple Selection with Checkbox */}
Checkbox Selection
setSelectedKeys(e.value)}
className="w-full md:w-30rem"
/>
{/* With Filter */}
Filter
);
}
```
--------------------------------
### Calendar Component Configuration
Source: https://context7.com/primefaces/primereact/llms.txt
Documentation for the Calendar component properties and usage patterns for date and time selection.
```APIDOC
## Calendar Component
### Description
The Calendar component allows users to select dates or times through an interactive UI. It supports various modes including range selection, multiple dates, and time-only picking.
### Properties
- **value** (Date | Date[]) - Optional - Current value of the calendar.
- **onChange** (Function) - Optional - Callback to execute when the value changes.
- **selectionMode** (string) - Optional - Defines selection behavior: 'single', 'multiple', or 'range'.
- **showIcon** (boolean) - Optional - Displays a calendar icon next to the input.
- **showTime** (boolean) - Optional - Enables time selection alongside the date.
- **timeOnly** (boolean) - Optional - Hides the date selection and shows only time.
- **view** (string) - Optional - Defines the view mode: 'date', 'month', or 'year'.
- **minDate** (Date) - Optional - Minimum selectable date.
- **maxDate** (Date) - Optional - Maximum selectable date.
- **disabledDays** (number[]) - Optional - Array of days to disable (0 for Sunday, 6 for Saturday).
### Usage Example
```javascript
setDate(e.value)} showIcon selectionMode="range" />
```
### Response
- **e.value** (Date | Date[]) - The selected date object or array of dates.
```
--------------------------------
### RadioButton Component Usage
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates the basic and dynamic usage of the RadioButton component for single selection from a group of options.
```APIDOC
## RadioButton Component
### Description
The RadioButton component allows users to select a single option from a predefined set of choices. It is commonly used for form inputs where only one selection is permitted.
### Method
N/A (Client-side Component)
### Endpoint
N/A (Client-side Component)
### Parameters
This component does not have traditional API parameters as it's a client-side React component. Key props include:
#### Key Props
- **inputId** (string) - Required - Unique identifier for the radio button.
- **name** (string) - Required - Name of the radio button group.
- **value** (any) - Required - The value associated with the radio button.
- **onChange** (function) - Required - Callback function triggered when the radio button state changes.
- **checked** (boolean) - Required - Determines if the radio button is currently selected.
### Request Example
```javascript
setIngredient(e.value)}
checked={ingredient === 'Cheese'}
/>
```
### Response
N/A (Client-side Component)
#### Success Response (200)
N/A
#### Response Example
N/A
```
--------------------------------
### React PrimeReactProvider Configuration
Source: https://context7.com/primefaces/primereact/llms.txt
Sets up global configurations for PrimeReact components, including theme, ripple effect, unstyled mode, locale, and z-index management. This provider should wrap the entire application to ensure consistent behavior.
```javascript
import React from 'react';
import { PrimeReactProvider } from 'primereact/api';
import App from './App';
// Theme import
import 'primereact/resources/themes/lara-light-cyan/theme.css';
const config = {
// Enable ripple effect globally
ripple: true,
// Enable unstyled mode (for Tailwind CSS)
// unstyled: true,
// Custom CSS transition settings
cssTransition: true,
// Locale configuration
locale: 'en',
// Z-index layering
zIndex: {
modal: 1100,
overlay: 1000,
menu: 1000,
tooltip: 1100
},
// Append overlay elements to specific container
appendTo: 'self', // or document.body
// Null sort order (1 for beginning, -1 for end)
nullSortOrder: 1,
// Filter match mode options
filterMatchModeOptions: {
text: ['startsWith', 'contains', 'notContains', 'endsWith', 'equals', 'notEquals'],
numeric: ['equals', 'notEquals', 'lt', 'lte', 'gt', 'gte'],
date: ['dateIs', 'dateIsNot', 'dateBefore', 'dateAfter']
}
};
export default function Root() {
return (
);
}
```
--------------------------------
### Clone PrimeReact Repository
Source: https://github.com/primefaces/primereact/wiki/Building-From-Source
Uses Git to clone the official PrimeReact repository from GitHub to your local machine.
```bash
git clone git@github.com:primefaces/primereact.git
```
--------------------------------
### Build CSS Assets
Source: https://github.com/primefaces/primereact/wiki/Building-From-Source
Compiles Sass files for the application and themes using Sass and Gulp build tasks.
```bash
sass --update src/sass/App.scss
sass --update public/resources/themes
gulp build
```
--------------------------------
### InputText Component
Source: https://context7.com/primefaces/primereact/llms.txt
Illustrates the PrimeReact InputText component, covering basic input, floating labels, validation states, key filtering, and tooltips.
```APIDOC
## InputText Component
InputText is a standard text input field with support for validation states, key filtering, and floating labels.
### Request Example
```javascript
import React, { useState } from 'react';
import { InputText } from 'primereact/inputtext';
import { FloatLabel } from 'primereact/floatlabel';
export default function InputTextDemo() {
const [value, setValue] = useState('');
const [email, setEmail] = useState('');
const [username, setUsername] = useState('');
return (
{/* Basic Input */}
setValue(e.target.value)}
placeholder="Enter text"
/>
{/* With Float Label */}
setUsername(e.target.value)} />
{/* Invalid State */}
setEmail(e.target.value)}
invalid={!email.includes('@')}
placeholder="Email"
/>
{/* With Key Filter (only allow alphanumeric) */}
{/* Disabled */}
{/* With Tooltip */}
);
}
```
```
--------------------------------
### Button Component
Source: https://context7.com/primefaces/primereact/llms.txt
Demonstrates the usage of the PrimeReact Button component with various options like labels, icons, loading states, severity, variants, sizes, badges, and disabled states.
```APIDOC
## Button Component
The Button component is a versatile UI element supporting labels, icons, loading states, badges, and various severity levels.
### Request Example
```javascript
import React, { useState } from 'react';
import { Button } from 'primereact/button';
export default function ButtonDemo() {
const [loading, setLoading] = useState(false);
const handleClick = () => {
setLoading(true);
setTimeout(() => setLoading(false), 2000);
};
return (
{/* Basic Button */}
{/* Button with Icon */}
{/* Icon-only Button */}
{/* Loading Button */}
{/* Severity Variants */}
{/* Outlined and Text Variants */}
{/* Sizes */}
{/* With Badge */}
{/* Link Button */}
{/* Disabled */}