The Input Range component provides numerical range filtering capabilities with support for minimum and maximum values, step increments, and a clean input interface.
- Installation
- Basic Usage
- Configuration
- UI Components
- API Reference
- Events
- Examples
- TypeScript
- Best Practices
import { InputRange } from 'advanced-filter-system';
// As part of AFS
const afs = createAFS();
// Access input range
const inputRange = afs.inputRange;
<!-- Input Range Container -->
<div id="price-filter"></div>
<!-- Filterable Items -->
<div class="filter-item" data-price="99.99">Product 1</div>
<div class="filter-item" data-price="149.99">Product 2</div>
// Initialize input range
afs.inputRange.addInputRange({
key: 'price',
container: document.querySelector('#price-filter'),
min: 0,
max: 1000,
step: 10,
label: 'Price Range'
});
{
key: string; // Data attribute key
container: Element; // Container element
min?: number; // Minimum value
max?: number; // Maximum value
step?: number; // Step increment (default: 1)
label?: string; // Label for the range (optional)
}
The input range consists of:
- Range label (optional)
- Minimum value input
- Maximum value input
- Labels for inputs
<div class="afs-input-range-container">
<div class="afs-input-wrapper">
<label>Min</label>
<input type="number" class="afs-input min">
</div>
<div class="afs-input-wrapper">
<label>Max</label>
<input type="number" class="afs-input max">
</div>
</div>
Add a new input range filter.
afs.inputRange.addInputRange({
key: 'price',
container: document.querySelector('#price-filter'),
min: 0,
max: 1000
});
Get current range values.
const range = afs.inputRange.getRange('price');
console.log(range.min, range.max);
Set range values programmatically.
afs.inputRange.setRange('price', 100, 500);
Remove an input range filter.
afs.inputRange.removeInputRange('price');
interface RangeValues {
min: number;
max: number;
}
interface RangeState {
min: number;
max: number;
step: number;
currentMin: number;
currentMax: number;
}
// Range changed
afs.on('inputRangeFilter', (data) => {
console.log('Key:', data.key);
console.log('Min:', data.min);
console.log('Max:', data.max);
});
// Range created
afs.on('inputRangeCreated', (data) => {
console.log('Input range created:', data.key);
});
// Range removed
afs.on('inputRangeRemoved', (data) => {
console.log('Input range removed:', data.key);
});
// Simple price range
afs.inputRange.addInputRange({
key: 'price',
container: document.querySelector('#price-filter'),
min: 0,
max: 1000
});
// Range with custom step
afs.inputRange.addInputRange({
key: 'quantity',
container: document.querySelector('#quantity-filter'),
min: 0,
max: 100,
step: 5
});
// Price range
afs.inputRange.addInputRange({
key: 'price',
container: document.querySelector('#price-filter'),
label: 'Price Range'
});
// Rating range
afs.inputRange.addInputRange({
key: 'rating',
container: document.querySelector('#rating-filter'),
min: 0,
max: 5,
step: 0.5,
label: 'Rating Filter'
});
// Calculate range based on items
function calculatePriceRange() {
const prices = Array.from(items).map(item =>
parseFloat(item.dataset.price)
).filter(price => !isNaN(price));
return {
min: Math.min(...prices),
max: Math.max(...prices)
};
}
// Apply dynamic range
const priceRange = calculatePriceRange();
afs.inputRange.setRange('price', priceRange.min, priceRange.max);
interface InputRangeOptions {
key: string;
container: HTMLElement;
min?: number;
max?: number;
step?: number;
label?: string;
}
interface RangeValues {
min: number;
max: number;
}
interface InputRangeEvent {
key: string;
min: number;
max: number;
}
-
Value Validation
function validateRange(min, max) { if (min > max) { throw new Error('Minimum value must be less than maximum value'); } if (min < absMin || max > absMax) { throw new Error('Values must be within allowed range'); } }
-
Number Formatting
// Format number display const formatter = new Intl.NumberFormat(locale, { style: 'decimal', minimumFractionDigits: 0, maximumFractionDigits: 2 }); afs.inputRange.addInputRange({ key: 'price', container: element, formatter: formatter.format });
-
Error Handling
try { afs.inputRange.setRange('price', min, max); } catch (error) { console.error('Range error:', error); // Handle error appropriately }
-
Input Debouncing
// Debounce input updates const debouncedUpdate = debounce(value => { afs.inputRange.setRange('price', value.min, value.max); }, 300);
-
Accessibility
// Add ARIA attributes const minInput = document.querySelector('.min-input'); minInput.setAttribute('aria-label', 'Minimum value'); minInput.setAttribute('aria-required', 'true');
// Integration with main filter system
afs.on('inputRangeFilter', (data) => {
afs.filter.applyInputRangeFilter(data.key, data.min, data.max);
});
// Clear filters
afs.filter.on('filtersCleared', () => {
// Reset all input ranges to their initial values
afs.inputRange.activeRanges.forEach((range, key) => {
afs.inputRange.setRange(key, range.state.min, range.state.max);
});
});