|
| 1 | +import Close from '@mui/icons-material/Close'; |
| 2 | +import Grid from '@mui/material/Unstable_Grid2'; |
| 3 | +import { styled } from '@mui/material/styles'; |
| 4 | +import * as React from 'react'; |
| 5 | + |
| 6 | +import { Button } from 'src/components/Button/Button'; |
| 7 | +import { FormHelperText } from 'src/components/FormHelperText'; |
| 8 | +import { TextField } from 'src/components/TextField'; |
| 9 | +import { RESERVED_IP_NUMBER } from 'src/utilities/subnets'; |
| 10 | +import { SubnetFieldState } from 'src/utilities/subnets'; |
| 11 | +import { calculateAvailableIPv4s } from 'src/utilities/subnets'; |
| 12 | + |
| 13 | +interface Props { |
| 14 | + disabled?: boolean; |
| 15 | + // extra props enable SubnetNode to be an independent component or be part of MultipleSubnetInput |
| 16 | + // potential refactor - isRemoveable, and subnetIdx & remove in onChange prop |
| 17 | + idx?: number; |
| 18 | + isRemovable?: boolean; |
| 19 | + onChange: ( |
| 20 | + subnet: SubnetFieldState, |
| 21 | + subnetIdx?: number, |
| 22 | + remove?: boolean |
| 23 | + ) => void; |
| 24 | + subnet: SubnetFieldState; |
| 25 | +} |
| 26 | + |
| 27 | +// TODO: VPC - currently only supports IPv4, must update when/if IPv6 is also supported |
| 28 | +export const SubnetNode = (props: Props) => { |
| 29 | + const { disabled, idx, isRemovable, onChange, subnet } = props; |
| 30 | + |
| 31 | + const onLabelChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 32 | + const newSubnet = { |
| 33 | + ...subnet, |
| 34 | + label: e.target.value, |
| 35 | + labelError: '', |
| 36 | + }; |
| 37 | + onChange(newSubnet, idx); |
| 38 | + }; |
| 39 | + |
| 40 | + const onIpv4Change = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 41 | + const availIPs = calculateAvailableIPv4s(e.target.value); |
| 42 | + const newSubnet = { |
| 43 | + ...subnet, |
| 44 | + ip: { availIPv4s: availIPs, ipv4: e.target.value }, |
| 45 | + }; |
| 46 | + onChange(newSubnet, idx); |
| 47 | + }; |
| 48 | + |
| 49 | + const removeSubnet = () => { |
| 50 | + onChange(subnet, idx, isRemovable); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <Grid key={idx} sx={{ maxWidth: 460 }}> |
| 55 | + <Grid container direction="row" spacing={2}> |
| 56 | + <Grid xs={isRemovable ? 11 : 12}> |
| 57 | + <TextField |
| 58 | + disabled={disabled} |
| 59 | + errorText={subnet.labelError} |
| 60 | + inputId={`subnet-label-${idx}`} |
| 61 | + label="Subnet label" |
| 62 | + onChange={onLabelChange} |
| 63 | + value={subnet.label} |
| 64 | + /> |
| 65 | + </Grid> |
| 66 | + {isRemovable && !!idx && ( |
| 67 | + <Grid xs={1}> |
| 68 | + <StyledButton onClick={removeSubnet}> |
| 69 | + <Close data-testid={`delete-subnet-${idx}`} /> |
| 70 | + </StyledButton> |
| 71 | + </Grid> |
| 72 | + )} |
| 73 | + </Grid> |
| 74 | + <Grid xs={isRemovable ? 11 : 12}> |
| 75 | + <TextField |
| 76 | + disabled={disabled} |
| 77 | + errorText={subnet.ip.ipv4Error} |
| 78 | + inputId={`subnet-ipv4-${idx}`} |
| 79 | + label="Subnet IP Address Range" |
| 80 | + onChange={onIpv4Change} |
| 81 | + value={subnet.ip.ipv4} |
| 82 | + /> |
| 83 | + {subnet.ip.availIPv4s && ( |
| 84 | + <FormHelperText> |
| 85 | + Available IP Addresses:{' '} |
| 86 | + {subnet.ip.availIPv4s > 4 |
| 87 | + ? subnet.ip.availIPv4s - RESERVED_IP_NUMBER |
| 88 | + : 0} |
| 89 | + </FormHelperText> |
| 90 | + )} |
| 91 | + </Grid> |
| 92 | + </Grid> |
| 93 | + ); |
| 94 | +}; |
| 95 | + |
| 96 | +const StyledButton = styled(Button, { label: 'StyledButton' })(({ theme }) => ({ |
| 97 | + '& :hover, & :focus': { |
| 98 | + backgroundColor: theme.color.grey2, |
| 99 | + }, |
| 100 | + '& > span': { |
| 101 | + padding: 2, |
| 102 | + }, |
| 103 | + color: theme.textColors.tableHeader, |
| 104 | + marginTop: theme.spacing(6), |
| 105 | + minHeight: 'auto', |
| 106 | + minWidth: 'auto', |
| 107 | + padding: 0, |
| 108 | +})); |
0 commit comments