feat: dynamic radio button for Filter component

This commit is contained in:
Hafid Nur
2025-03-31 22:46:09 +07:00
parent 984a31c587
commit d7a90f47d0
3 changed files with 60 additions and 22 deletions
+13 -1
View File
@@ -66,6 +66,11 @@ export const Components: FC = (): ReactElement => {
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({}); const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
const [showFilter, setShowFilter] = useState(false); const [showFilter, setShowFilter] = useState(false);
const deliveryOptions = [
{ id: 'option1', value: 'undelivered', label: 'Undelivered' },
{ id: 'option1', value: 'delivered', label: 'Delivered' },
];
const columns: ColumnDef<Prize>[] = [ const columns: ColumnDef<Prize>[] = [
{ {
id: 'select', id: 'select',
@@ -214,7 +219,14 @@ export const Components: FC = (): ReactElement => {
</Button> </Button>
{showFilter && ( {showFilter && (
<div className="absolute right-0 top-[calc(100%+12px)] z-10 shadow-lg"> <div className="absolute right-0 top-[calc(100%+12px)] z-10 shadow-lg">
<Filter onClose={() => setShowFilter(false)} /> <Filter
options={deliveryOptions}
onClose={() => setShowFilter(false)}
onFilterChange={(value) => {
console.log('Selected filter:', value);
// Filter logic di sini
}}
/>
</div> </div>
)} )}
</div> </div>
+14 -1
View File
@@ -50,6 +50,12 @@ export const Components: FC = (): ReactElement => {
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({}); const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
const [showFilter, setShowFilter] = useState(false); const [showFilter, setShowFilter] = useState(false);
const validationOptions = [
{ id: 'option1', value: 'unchecked', label: 'Unchecked' },
{ id: 'option2', value: 'valid', label: 'Valid' },
{ id: 'option3', value: 'invalid', label: 'Invalid' },
];
const columns: ColumnDef<Transaction>[] = [ const columns: ColumnDef<Transaction>[] = [
{ {
id: 'select', id: 'select',
@@ -174,7 +180,14 @@ export const Components: FC = (): ReactElement => {
</Button> </Button>
{showFilter && ( {showFilter && (
<div className="absolute right-0 top-[calc(100%+12px)] z-10 shadow-lg"> <div className="absolute right-0 top-[calc(100%+12px)] z-10 shadow-lg">
<Filter onClose={() => setShowFilter(false)} /> <Filter
options={validationOptions}
onClose={() => setShowFilter(false)}
onFilterChange={(value) => {
console.log('Selected filter:', value);
// Filter logic di sini
}}
/>
</div> </div>
)} )}
</div> </div>
+33 -20
View File
@@ -42,13 +42,31 @@ const Radio = ({
interface FilterProps { interface FilterProps {
onClose?: () => void; onClose?: () => void;
options: Array<{
id: string;
value: string;
label: string;
}>;
selectedValue?: string;
onFilterChange?: (value: string) => void;
title?: string;
} }
export const Filter = ({ onClose }: FilterProps) => { export const Filter = ({
const [selectedStatus, setSelectedStatus] = useState('delivered'); onClose,
options,
selectedValue,
onFilterChange,
title = 'Status',
}: FilterProps) => {
const [selectedStatus, setSelectedStatus] = useState(
selectedValue || options[0]?.value || ''
);
const handleStatusChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleStatusChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSelectedStatus(e.target.value); const newValue = e.target.value;
setSelectedStatus(newValue);
onFilterChange?.(newValue);
}; };
return ( return (
@@ -63,24 +81,19 @@ export const Filter = ({ onClose }: FilterProps) => {
</button> </button>
</div> </div>
<hr className="border-primary-200" /> <hr className="border-primary-200" />
<span className="font-semibold text-primary-500">Status</span> <span className="font-semibold text-primary-500">{title}</span>
<div className="flex flex-col gap-[10px]"> <div className="flex flex-col gap-[10px]">
<Radio {options.map((option) => (
id="option1" <Radio
name="status" key={option.id}
value="undelivered" id={option.id}
label="Undelivered" name="status"
checked={selectedStatus === 'undelivered'} value={option.value}
onChange={handleStatusChange} label={option.label}
/> checked={selectedStatus === option.value}
<Radio onChange={handleStatusChange}
id="option2" />
name="status" ))}
value="delivered"
label="Delivered"
checked={selectedStatus === 'delivered'}
onChange={handleStatusChange}
/>
</div> </div>
</div> </div>
); );