diff --git a/packages/kitchen-sink/src/examples/discount-calculator.tsx b/packages/kitchen-sink/src/examples/discount-calculator.tsx new file mode 100644 index 0000000000..0f0011cb75 --- /dev/null +++ b/packages/kitchen-sink/src/examples/discount-calculator.tsx @@ -0,0 +1,72 @@ +import { useState } from 'react'; +import { block } from 'million/react'; + +interface Results { + discountPrice: number; + finalPrice: number; + } + + + +const DiscountCalculator = block(() => { + const [originalPrice, setOriginalPrice] = useState(''); + const [discountRate, setDiscountRate] = useState(''); + const [results, setResults] = useState(null); + + + const calculateDiscount =()=>{ + const p = parseFloat(originalPrice as string); + const r = parseFloat(discountRate as string) / 100; + + + if (isNaN(p) || isNaN(r) || p <= 0 || r <= 0 ) { + alert('Please enter valid numbers.'); + return; + } + + const discountPrice = p * r; + const finalPrice = p - discountPrice; + + setResults({ + discountPrice, + finalPrice, + }); + } + + + return ( +
+

Discount Calculator

+
+ + setOriginalPrice(e.target.value)} + style={{ display: 'block', marginBottom: '15px' }} + /> +
+
+ + setDiscountRate(e.target.value)} + style={{ display: 'block', marginBottom: '15px' }} + /> +
+ + + + {results && ( +
+

Results:

+
Final Price : ${results.finalPrice.toFixed(2)}
+
You Save : ${results.discountPrice.toFixed(2)}
+
+ )} +
+ ); +}); + +export default DiscountCalculator; \ No newline at end of file