Skip to content

Commit

Permalink
fix: remove length limit for MAC address fields MAASENG-3518 (#5500)
Browse files Browse the repository at this point in the history
- Removed length limit for MAC address inputs
---
Fixes [MAASENG-3518](https://warthogs.atlassian.net/browse/MAASENG-3518)
  • Loading branch information
ndv99 authored Jul 18, 2024
1 parent 18e1fb8 commit 64baa11
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const MacAddressField = ({ name, ...props }: Props): JSX.Element => {

return (
<FormikField
maxLength={17}
name={name}
onChange={(evt: React.ChangeEvent<HTMLInputElement>) => {
setFieldValue(name, formatMacAddress(evt.target.value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export const AddMachineFormFields = ({ saved }: Props): JSX.Element => {
<Input
aria-label={`Extra MAC address ${i + 1}`}
error={errors?.extra_macs && errors.extra_macs[i]}
maxLength={17}
onChange={(e) => {
const newExtraMACs = [...extraMACs];
newExtraMACs[i] = formatMacAddress(e.target.value);
Expand Down
6 changes: 2 additions & 4 deletions src/app/utils/formatMacAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ describe("formatMacAddress", () => {
expect(formatMacAddress("123456789abc")).toEqual("12:34:56:78:9a:bc");
});

it("truncates correctly", () => {
expect(formatMacAddress("12:34:56:78:9a:bc:de")).toEqual(
"12:34:56:78:9a:bc"
);
it("stops adding separators after the fifth one", () => {
expect(formatMacAddress("123456789abcde")).toEqual("12:34:56:78:9a:bcde");
});
});
11 changes: 8 additions & 3 deletions src/app/utils/formatMacAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

export const formatMacAddress = (value: string): string => {
const hexValues = value.replace(/:/g, "");
if (value.length % 3 === 0) {
return hexValues.replace(/([0-9A-Za-z]{2})/g, "$1:").substring(0, 17);

if (hexValues.length > 10) {
const firstTenValues = hexValues.slice(0, 10);
const lastValues = hexValues.slice(10);

return firstTenValues.replace(/([0-9A-Za-z]{2})/g, "$1:") + lastValues;
} else {
return hexValues.replace(/([0-9A-Za-z]{2})/g, "$1:");
}
return value.substring(0, 17);
};

0 comments on commit 64baa11

Please sign in to comment.