Last active
January 29, 2024 08:50
-
-
Save joesoeph/05d7b1703c57cadbe322e822aa7f6d31 to your computer and use it in GitHub Desktop.
This is example custom input field antd component. For more detailed about custom input field, you can visit: https://ant.design/components/form#components-form-demo-customized-form-controls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Input } from 'antd'; | |
import React, { useEffect, useState } from 'react'; | |
interface PriceInputProps { | |
value?: number; | |
onChange?: (value: number) => void; | |
} | |
const CustomInput: React.FC<PriceInputProps> = ({ value = 0, onChange }) => { | |
const [number, setNumber] = useState(0); | |
const triggerChange = (changedValue: number) => { | |
onChange?.(changedValue); | |
}; | |
useEffect(() => { | |
console.log(value); | |
}, [value]); | |
const onNumberChange = (e: React.ChangeEvent<HTMLInputElement>) => { | |
const newNumber = parseInt(e.target.value || '0', 10); | |
if (Number.isNaN(number)) { | |
return; | |
} | |
if (typeof number === 'number') { | |
setNumber(newNumber); | |
} | |
triggerChange(newNumber); | |
}; | |
return ( | |
<span> | |
<Input type="text" value={value || number} onChange={onNumberChange} style={{ width: 100 }} /> | |
</span> | |
); | |
}; | |
export default CustomInput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment