fix: handle optional chaining for address and validation in StaticIpv4Card and StaticIpv6Card components

This commit is contained in:
Adam Shiervani 2025-10-14 22:48:53 +02:00
parent 2f51cba03a
commit c4c3880718
2 changed files with 16 additions and 13 deletions

View File

@ -26,9 +26,9 @@ export default function StaticIpv4Card() {
const hideSubnetMask = ipv4StaticAddress?.includes("/"); const hideSubnetMask = ipv4StaticAddress?.includes("/");
useEffect(() => { useEffect(() => {
const parts = ipv4StaticAddress?.split("/", 2); const parts = ipv4StaticAddress?.split("/", 2);
if (parts.length !== 2) return; if (parts?.length !== 2) return;
const cidrNotation = parseInt(parts[1]); const cidrNotation = parseInt(parts?.[1] ?? "");
if (isNaN(cidrNotation) || cidrNotation < 0 || cidrNotation > 32) return; if (isNaN(cidrNotation) || cidrNotation < 0 || cidrNotation > 32) return;
const mask = netMaskFromCidr4(cidrNotation); const mask = netMaskFromCidr4(cidrNotation);
@ -60,7 +60,9 @@ export default function StaticIpv4Card() {
size="SM" size="SM"
placeholder="192.168.1.100" placeholder="192.168.1.100"
{ {
...register("ipv4_static.address", { validate: validateIsIPOrCIDR4 })} ...register("ipv4_static.address", {
validate: (value: string | undefined) => validateIsIPOrCIDR4(value ?? "")
})}
error={formState.errors.ipv4_static?.address?.message} error={formState.errors.ipv4_static?.address?.message}
/> />
@ -69,7 +71,7 @@ export default function StaticIpv4Card() {
type="text" type="text"
size="SM" size="SM"
placeholder="255.255.255.0" placeholder="255.255.255.0"
{...register("ipv4_static.netmask", { validate })} {...register("ipv4_static.netmask", { validate: (value: string | undefined) => validate(value ?? "") })}
error={formState.errors.ipv4_static?.netmask?.message} error={formState.errors.ipv4_static?.netmask?.message}
/>} />}
</div> </div>
@ -79,7 +81,7 @@ export default function StaticIpv4Card() {
type="text" type="text"
size="SM" size="SM"
placeholder="192.168.1.1" placeholder="192.168.1.1"
{...register("ipv4_static.gateway", { validate })} {...register("ipv4_static.gateway", { validate: (value: string | undefined) => validate(value ?? "") })}
error={formState.errors.ipv4_static?.gateway?.message} error={formState.errors.ipv4_static?.gateway?.message}
/> />
@ -95,7 +97,10 @@ export default function StaticIpv4Card() {
type="text" type="text"
size="SM" size="SM"
placeholder="1.1.1.1" placeholder="1.1.1.1"
{...register(`ipv4_static.dns.${index}`, { validate })} {...register(
`ipv4_static.dns.${index}`,
{ validate: (value: string | undefined) => validate(value ?? "") }
)}
error={formState.errors.ipv4_static?.dns?.[index]?.message} error={formState.errors.ipv4_static?.dns?.[index]?.message}
/> />
</div> </div>
@ -123,7 +128,7 @@ export default function StaticIpv4Card() {
LeadingIcon={LuPlus} LeadingIcon={LuPlus}
type="button" type="button"
text="Add DNS Server" text="Add DNS Server"
disabled={dns[0] === ""} disabled={dns?.[0] === ""}
/> />
</div> </div>
</div> </div>

View File

@ -55,7 +55,7 @@ export default function StaticIpv6Card() {
type="text" type="text"
size="SM" size="SM"
placeholder="2001:db8::1/64" placeholder="2001:db8::1/64"
{...register("ipv6_static.prefix", { validate: cidrValidation })} {...register("ipv6_static.prefix", { validate: (value: string | undefined) => cidrValidation(value ?? "") })}
error={formState.errors.ipv6_static?.prefix?.message} error={formState.errors.ipv6_static?.prefix?.message}
/> />
@ -64,7 +64,7 @@ export default function StaticIpv6Card() {
type="text" type="text"
size="SM" size="SM"
placeholder="2001:db8::1" placeholder="2001:db8::1"
{...register("ipv6_static.gateway", { validate: ipv6Validation })} {...register("ipv6_static.gateway", { validate: (value: string | undefined) => ipv6Validation(value ?? "") })}
error={formState.errors.ipv6_static?.gateway?.message} error={formState.errors.ipv6_static?.gateway?.message}
/> />
@ -80,9 +80,7 @@ export default function StaticIpv6Card() {
type="text" type="text"
size="SM" size="SM"
placeholder="2001:4860:4860::8888" placeholder="2001:4860:4860::8888"
{...register(`ipv6_static.dns.${index}`, { {...register(`ipv6_static.dns.${index}`, { validate: (value: string | undefined) => ipv6Validation(value ?? "") })}
validate: ipv6Validation,
})}
error={formState.errors.ipv6_static?.dns?.[index]?.message} error={formState.errors.ipv6_static?.dns?.[index]?.message}
/> />
</div> </div>
@ -110,7 +108,7 @@ export default function StaticIpv6Card() {
LeadingIcon={LuPlus} LeadingIcon={LuPlus}
type="button" type="button"
text="Add DNS Server" text="Add DNS Server"
disabled={dns[0] === ""} disabled={dns?.[0] === ""}
/> />
</div> </div>
</div> </div>