dstpabuaran.com/resources/js/components/rupiah-input.tsx
Yoga Pangestu c3d8ea2f66 feat: implement cash account management with deposit and withdrawal functionality
- Introduced CashAccountController for managing cash accounts.
- Created CashTransactionRequest and CashAccountRequest for transaction validation.
- Developed CashAccountService to handle business logic for cash transactions.
- Added UI components for cash account management, including deposit and withdrawal dialogs.
- Implemented data tables for displaying transactions and cash account details.
- Updated routes to include cash account management endpoints.
- Added tests for cash account functionality, including deposit and withdrawal operations.
2026-07-29 02:08:39 +07:00

81 lines
2.0 KiB
TypeScript

import { useCallback, useRef, useState } from 'react';
import {
InputGroup,
InputGroupAddon,
InputGroupInput,
InputGroupText,
} from '@/components/ui/input-group';
type RupiahInputProps = {
name?: string;
defaultValue?: number;
placeholder?: string;
disabled?: boolean;
min?: number;
max?: number;
className?: string;
};
function formatRupiah(value: number): string {
return value.toLocaleString('id-ID');
}
function parseRupiah(value: string): number {
const cleaned = value.replace(/[^0-9]/g, '');
return cleaned === '' ? 0 : parseInt(cleaned, 10);
}
export function RupiahInput({
name,
defaultValue = 0,
placeholder = '0',
disabled = false,
min,
max,
className,
}: RupiahInputProps) {
const [displayValue, setDisplayValue] = useState(formatRupiah(defaultValue));
const lastValidRef = useRef(defaultValue);
const handleChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const raw = parseRupiah(e.target.value);
let clamped = raw;
if (min !== undefined && raw < min) {
clamped = min;
}
if (max !== undefined && raw > max) {
clamped = max;
}
lastValidRef.current = clamped;
setDisplayValue(formatRupiah(clamped));
},
[min, max],
);
const handleBlur = useCallback(() => {
setDisplayValue(formatRupiah(lastValidRef.current));
}, []);
return (
<InputGroup className={className}>
<InputGroupAddon>
<InputGroupText>Rp</InputGroupText>
</InputGroupAddon>
<InputGroupInput
name={name}
type="text"
inputMode="numeric"
value={displayValue}
onChange={handleChange}
onBlur={handleBlur}
placeholder={placeholder}
disabled={disabled}
autoComplete="off"
/>
</InputGroup>
);
}