Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 1x 4x 1x 11x 1x 25x 25x 25x 11x 11x 9x 25x 14x 14x 14x 8x 6x 6x 14x 14x 14x 5x 14x 10x 25x | import React from 'react';
import { FC, useEffect, useRef, useState, ChangeEvent } from 'react';
interface FilterProps {
onFilterChange: (filters: {
address: string;
minPrice: string;
maxPrice: string;
bedrooms: string;
page: number;
size: number;
}) => void;
initialFilters?: {
address: string;
minPrice: string;
maxPrice: string;
bedrooms: string;
page: number;
size: number;
};
}
// Validation functions
export const validateNumber = (value: string): string => {
// Only allow digits and limit length
return value.replace(/\D/g, '').slice(0, 10);
};
export const validateAddress = (value: string): string => {
// Remove HTML tags and limit length
return value.replace(/<[^>]*>/g, '').slice(0, 100);
};
const Filter: FC<FilterProps> = ({ onFilterChange, initialFilters = {
address: '',
minPrice: '',
maxPrice: '',
bedrooms: '',
page: 0,
size: 10
} }) => {
const [filters, setFilters] = useState(initialFilters);
const timeoutRef = useRef<NodeJS.Timeout>();
useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
// Apply validation based on field type
let validatedValue = value;
if (name === 'address') {
validatedValue = validateAddress(value);
} else Eif (['minPrice', 'maxPrice', 'bedrooms'].includes(name)) {
// Note: HTML number inputs handle validation natively in the browser,
// but we apply our validation as an extra safety measure for programmatic inputs
validatedValue = value;
}
const newFilters = { ...filters, [name]: validatedValue, page: 0 }; // Reset to first page when filters change
setFilters(newFilters);
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
onFilterChange(newFilters);
}, 500);
};
return (
<div className="py-3 mb-2">
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
<div>
<label
htmlFor="address-input"
className="block text-sm font-medium text-gray-700 mb-1"
>
Address
</label>
<input
id="address-input"
type="text"
name="address"
value={filters.address}
onChange={handleChange}
placeholder="Search by address"
className="w-full px-4 py-3 border-2 border-[#e5e5e5] rounded-xl focus:outline-none focus:ring-0 focus:border-[#00deb6] bg-white text-[#262637] transition-colors"
maxLength={100}
/>
</div>
<div>
<label
htmlFor="min-price-input"
className="block text-sm font-medium text-gray-700 mb-1"
>
Min Price
</label>
<input
id="min-price-input"
type="number"
name="minPrice"
value={filters.minPrice}
onChange={handleChange}
placeholder="Min price"
className="w-full px-4 py-3 border-2 border-[#e5e5e5] rounded-xl focus:outline-none focus:ring-0 focus:border-[#00deb6] bg-white text-[#262637] transition-colors"
min="0"
max="9999999999"
/>
</div>
<div>
<label
htmlFor="max-price-input"
className="block text-sm font-medium text-gray-700 mb-1"
>
Max Price
</label>
<input
id="max-price-input"
type="number"
name="maxPrice"
value={filters.maxPrice}
onChange={handleChange}
placeholder="Max price"
className="w-full px-4 py-3 border-2 border-[#e5e5e5] rounded-xl focus:outline-none focus:ring-0 focus:border-[#00deb6] bg-white text-[#262637] transition-colors"
min="0"
max="9999999999"
/>
</div>
<div>
<label
htmlFor="bedrooms-input"
className="block text-sm font-medium text-gray-700 mb-1"
>
Bedrooms
</label>
<input
id="bedrooms-input"
type="number"
name="bedrooms"
value={filters.bedrooms}
onChange={handleChange}
placeholder="Number of bedrooms"
className="w-full px-4 py-3 border-2 border-[#e5e5e5] rounded-xl focus:outline-none focus:ring-0 focus:border-[#00deb6] bg-white text-[#262637] transition-colors"
min="0"
max="99"
/>
</div>
</div>
</div>
);
};
export default Filter; |