Handle numbers and value=0 better

This commit is contained in:
Justin Edmund 2023-07-05 20:40:01 -07:00
parent 9650c34957
commit 6cb607ed7a
2 changed files with 37 additions and 21 deletions

View file

@ -14,7 +14,7 @@ interface Props
imageAlt?: string imageAlt?: string
imageClass?: string imageClass?: string
imageSrc?: string[] imageSrc?: string[]
onValueChange: (value?: string) => void onValueChange: (value?: string | number | readonly string[]) => void
} }
const InputTableField = ({ const InputTableField = ({
@ -25,10 +25,12 @@ const InputTableField = ({
imageSrc, imageSrc,
...props ...props
}: Props) => { }: Props) => {
const [inputValue, setInputValue] = useState('') const [inputValue, setInputValue] = useState<
string | number | readonly string[]
>()
useEffect(() => { useEffect(() => {
if (props.value) setInputValue(`${props.value}`) if (props.value !== undefined) setInputValue(props.value)
}, [props.value]) }, [props.value])
useEffect(() => { useEffect(() => {
@ -54,7 +56,7 @@ const InputTableField = ({
<Input <Input
className={props.className} className={props.className}
placeholder={props.placeholder} placeholder={props.placeholder}
value={inputValue ? `${inputValue}` : ''} value={inputValue !== undefined ? inputValue : ''}
step={1} step={1}
tabIndex={props.tabIndex} tabIndex={props.tabIndex}
bound={true} bound={true}

View file

@ -132,7 +132,6 @@ const EditPartyModal = ({
} }
function handleEditorUpdate(content: JSONContent) { function handleEditorUpdate(content: JSONContent) {
console.log('Editor updated')
setDescription(JSON.stringify(content)) setDescription(JSON.stringify(content))
} }
@ -160,22 +159,23 @@ const EditPartyModal = ({
if (!isNaN(value)) setClearTime(value) if (!isNaN(value)) setClearTime(value)
} }
function handleTurnCountChanged(value?: string) { function handleTurnCountChanged(value?: string | number | readonly string[]) {
if (!value) return if (value === null || value === undefined) return
const numericalValue = parseInt(value) setTurnCount(value as number)
if (!isNaN(numericalValue)) setTurnCount(numericalValue)
} }
function handleButtonCountChanged(value?: string) { function handleButtonCountChanged(
if (!value) return value?: string | number | readonly string[]
const numericalValue = parseInt(value) ) {
if (!isNaN(numericalValue)) setButtonCount(numericalValue) if (value === null || value === undefined) return
setButtonCount(value as number)
} }
function handleChainCountChanged(value?: string) { function handleChainCountChanged(
if (!value) return value?: string | number | readonly string[]
const numericalValue = parseInt(value) ) {
if (!isNaN(numericalValue)) setChainCount(numericalValue) if (value === null || value === undefined) return
setChainCount(value as number)
} }
function handleTextAreaChanged(event: React.ChangeEvent<HTMLDivElement>) { function handleTextAreaChanged(event: React.ChangeEvent<HTMLDivElement>) {
@ -298,7 +298,6 @@ const EditPartyModal = ({
function hasBeenModified() { function hasBeenModified() {
const nameChanged = const nameChanged =
name !== party.name && !(name === '' && party.name === undefined) name !== party.name && !(name === '' && party.name === undefined)
const descriptionChanged = const descriptionChanged =
description !== party.description && description !== party.description &&
!(description === '' && party.description === undefined) !(description === '' && party.description === undefined)
@ -313,6 +312,21 @@ const EditPartyModal = ({
const buttonCountChanged = buttonCount !== party.buttonCount const buttonCountChanged = buttonCount !== party.buttonCount
const chainCountChanged = chainCount !== party.chainCount const chainCountChanged = chainCount !== party.chainCount
// Debugging for if you need to check if a value is being changed
// console.log(`
// nameChanged: ${nameChanged}\n
// descriptionChanged: ${descriptionChanged}\n
// raidChanged: ${raidChanged}\n
// chargeAttackChanged: ${chargeAttackChanged}\n
// fullAutoChanged: ${fullAutoChanged}\n
// autoGuardChanged: ${autoGuardChanged}\n
// autoSummonChanged: ${autoSummonChanged}\n
// clearTimeChanged: ${clearTimeChanged}\n
// turnCountChanged: ${turnCountChanged}\n
// buttonCountChanged: ${buttonCountChanged}\n
// chainCountChanged: ${chainCountChanged}\n
// `)
return ( return (
nameChanged || nameChanged ||
descriptionChanged || descriptionChanged ||
@ -339,9 +353,9 @@ const EditPartyModal = ({
setFullAuto(party.fullAuto) setFullAuto(party.fullAuto)
setChargeAttack(party.chargeAttack) setChargeAttack(party.chargeAttack)
setClearTime(party.clearTime) setClearTime(party.clearTime)
if (party.turnCount) setTurnCount(party.turnCount) if (party.turnCount !== undefined) setTurnCount(party.turnCount)
if (party.buttonCount) setButtonCount(party.buttonCount) if (party.buttonCount !== undefined) setButtonCount(party.buttonCount)
if (party.chainCount) setChainCount(party.chainCount) if (party.chainCount !== undefined) setChainCount(party.chainCount)
} }
async function updateDetails(event: React.MouseEvent) { async function updateDetails(event: React.MouseEvent) {