@canonical/react-components 3.9.1 → 3.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/components/PrefixedInput/PrefixedInput.d.ts +13 -0
  2. package/dist/components/PrefixedInput/PrefixedInput.js +64 -0
  3. package/dist/components/PrefixedInput/PrefixedInput.scss +22 -0
  4. package/dist/components/PrefixedInput/PrefixedInput.stories.d.ts +11 -0
  5. package/dist/components/PrefixedInput/PrefixedInput.stories.js +58 -0
  6. package/dist/components/PrefixedInput/PrefixedInput.test.d.ts +1 -0
  7. package/dist/components/PrefixedInput/index.d.ts +1 -0
  8. package/dist/components/PrefixedInput/index.js +13 -0
  9. package/dist/components/PrefixedIpInput/PrefixedIpInput.d.ts +27 -0
  10. package/dist/components/PrefixedIpInput/PrefixedIpInput.js +65 -0
  11. package/dist/components/PrefixedIpInput/PrefixedIpInput.stories.d.ts +13 -0
  12. package/dist/components/PrefixedIpInput/PrefixedIpInput.stories.js +137 -0
  13. package/dist/components/PrefixedIpInput/PrefixedIpInput.test.d.ts +1 -0
  14. package/dist/components/PrefixedIpInput/index.d.ts +2 -0
  15. package/dist/components/PrefixedIpInput/index.js +56 -0
  16. package/dist/components/PrefixedIpInput/utils.d.ts +39 -0
  17. package/dist/components/PrefixedIpInput/utils.js +125 -0
  18. package/dist/components/PrefixedIpInput/utils.test.d.ts +1 -0
  19. package/dist/esm/components/PrefixedInput/PrefixedInput.d.ts +13 -0
  20. package/dist/esm/components/PrefixedInput/PrefixedInput.js +57 -0
  21. package/dist/esm/components/PrefixedInput/PrefixedInput.scss +22 -0
  22. package/dist/esm/components/PrefixedInput/PrefixedInput.stories.d.ts +11 -0
  23. package/dist/esm/components/PrefixedInput/PrefixedInput.stories.js +51 -0
  24. package/dist/esm/components/PrefixedInput/PrefixedInput.test.d.ts +1 -0
  25. package/dist/esm/components/PrefixedInput/index.d.ts +1 -0
  26. package/dist/esm/components/PrefixedInput/index.js +1 -0
  27. package/dist/esm/components/PrefixedIpInput/PrefixedIpInput.d.ts +27 -0
  28. package/dist/esm/components/PrefixedIpInput/PrefixedIpInput.js +58 -0
  29. package/dist/esm/components/PrefixedIpInput/PrefixedIpInput.stories.d.ts +13 -0
  30. package/dist/esm/components/PrefixedIpInput/PrefixedIpInput.stories.js +128 -0
  31. package/dist/esm/components/PrefixedIpInput/PrefixedIpInput.test.d.ts +1 -0
  32. package/dist/esm/components/PrefixedIpInput/index.d.ts +2 -0
  33. package/dist/esm/components/PrefixedIpInput/index.js +2 -0
  34. package/dist/esm/components/PrefixedIpInput/utils.d.ts +39 -0
  35. package/dist/esm/components/PrefixedIpInput/utils.js +112 -0
  36. package/dist/esm/components/PrefixedIpInput/utils.test.d.ts +1 -0
  37. package/dist/esm/index.d.ts +4 -0
  38. package/dist/esm/index.js +2 -0
  39. package/dist/index.d.ts +4 -0
  40. package/dist/index.js +65 -0
  41. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { type ReactElement } from "react";
2
+ import { type InputProps } from "../Input";
3
+ import "./PrefixedInput.scss";
4
+ import { PropsWithSpread } from "../../types";
5
+ export type PrefixedInputProps = PropsWithSpread<{
6
+ /**
7
+ * The immutable text that appears at the beginning of the input field.
8
+ * This text is not editable by the user and visually appears inside the input.
9
+ */
10
+ immutableText: string;
11
+ }, Omit<InputProps, "type">>;
12
+ declare const PrefixedInput: ({ immutableText, ...props }: PrefixedInputProps) => ReactElement;
13
+ export default PrefixedInput;
@@ -0,0 +1,57 @@
1
+ var _excluded = ["immutableText"];
2
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
5
+ import React from "react";
6
+ import { useLayoutEffect, useRef, useCallback } from "react";
7
+ import Input from "../Input";
8
+ import classNames from "classnames";
9
+ import "./PrefixedInput.scss";
10
+ import { useListener } from "../../hooks/useListener";
11
+
12
+ // export type PrefixedInputProps = Omit<InputProps, "type"> & {
13
+ // /**
14
+ // * The immutable text that appears at the beginning of the input field.
15
+ // * This text is not editable by the user and visually appears inside the input.
16
+ // */
17
+ // immutableText: string;
18
+ // };
19
+
20
+ var PrefixedInput = _ref => {
21
+ var {
22
+ immutableText
23
+ } = _ref,
24
+ props = _objectWithoutProperties(_ref, _excluded);
25
+ var prefixTextRef = useRef(null);
26
+ var inputWrapperRef = useRef(null);
27
+ var updatePadding = useCallback(() => {
28
+ var _inputWrapperRef$curr;
29
+ var prefixElement = prefixTextRef.current;
30
+ var inputElement = (_inputWrapperRef$curr = inputWrapperRef.current) === null || _inputWrapperRef$curr === void 0 ? void 0 : _inputWrapperRef$curr.querySelector("input");
31
+ if (prefixElement && inputElement) {
32
+ // Adjust the left padding of the input to be the same width as the immutable text.
33
+ // This displays the user input and the unchangeable text together as one combined string.
34
+ var prefixWidth = prefixElement.getBoundingClientRect().width;
35
+ inputElement.style.paddingLeft = "".concat(prefixWidth, "px");
36
+ }
37
+ }, []);
38
+ useListener(window, updatePadding, "resize", true);
39
+ useLayoutEffect(() => {
40
+ updatePadding();
41
+ }, [immutableText, props.label, updatePadding]);
42
+ return /*#__PURE__*/React.createElement("div", {
43
+ className: classNames("prefixed-input", {
44
+ "prefixed-input--with-label": !!props.label
45
+ })
46
+ }, /*#__PURE__*/React.createElement("div", {
47
+ className: "prefixed-input__text",
48
+ ref: prefixTextRef
49
+ }, immutableText), /*#__PURE__*/React.createElement("div", {
50
+ ref: inputWrapperRef
51
+ }, /*#__PURE__*/React.createElement(Input, _extends({}, props, {
52
+ className: classNames("prefixed-input__input", props.className),
53
+ type: "text",
54
+ wrapperClassName: classNames("prefixed-input__wrapper", props.wrapperClassName)
55
+ }))));
56
+ };
57
+ export default PrefixedInput;
@@ -0,0 +1,22 @@
1
+ @import "vanilla-framework";
2
+
3
+ .prefixed-input {
4
+ position: relative;
5
+
6
+ .prefixed-input__input {
7
+ padding-top: 0.25rem;
8
+ }
9
+
10
+ .prefixed-input__text {
11
+ padding-left: $spv--large;
12
+ padding-top: 0.3rem;
13
+ pointer-events: none;
14
+ position: absolute;
15
+ }
16
+
17
+ &--with-label {
18
+ .prefixed-input__text {
19
+ top: 2.5rem;
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,11 @@
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import PrefixedInput from "./PrefixedInput";
3
+ declare const meta: Meta<typeof PrefixedInput>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof PrefixedInput>;
6
+ export declare const Default: Story;
7
+ export declare const WithLabel: Story;
8
+ export declare const Disabled: Story;
9
+ export declare const WithError: Story;
10
+ export declare const WithHelpText: Story;
11
+ export declare const Required: Story;
@@ -0,0 +1,51 @@
1
+ import PrefixedInput from "./PrefixedInput";
2
+ var meta = {
3
+ component: PrefixedInput,
4
+ tags: ["autodocs"]
5
+ };
6
+ export default meta;
7
+ export var Default = {
8
+ args: {
9
+ immutableText: "https://",
10
+ placeholder: "example.com"
11
+ }
12
+ };
13
+ export var WithLabel = {
14
+ args: {
15
+ immutableText: "https://",
16
+ label: "Website URL",
17
+ placeholder: "example.com"
18
+ }
19
+ };
20
+ export var Disabled = {
21
+ args: {
22
+ immutableText: "@",
23
+ label: "Username",
24
+ placeholder: "username",
25
+ disabled: true
26
+ }
27
+ };
28
+ export var WithError = {
29
+ args: {
30
+ immutableText: "https://",
31
+ label: "Website URL",
32
+ placeholder: "example.com",
33
+ error: "Invalid URL format"
34
+ }
35
+ };
36
+ export var WithHelpText = {
37
+ args: {
38
+ immutableText: "User ID:",
39
+ label: "User Identifier",
40
+ placeholder: " Enter user ID",
41
+ help: "This will be used to identify your account"
42
+ }
43
+ };
44
+ export var Required = {
45
+ args: {
46
+ immutableText: "https://",
47
+ label: "Website URL",
48
+ placeholder: "example.com",
49
+ required: true
50
+ }
51
+ };
@@ -0,0 +1 @@
1
+ export { default, type PrefixedInputProps } from "./PrefixedInput";
@@ -0,0 +1 @@
1
+ export { default } from "./PrefixedInput";
@@ -0,0 +1,27 @@
1
+ import { type ReactElement } from "react";
2
+ import { type PrefixedInputProps } from "../PrefixedInput";
3
+ import { PropsWithSpread } from "../../types";
4
+ export type PrefixedIpInputProps = PropsWithSpread<{
5
+ /**
6
+ * The CIDR for the subnet (e.g., "192.168.1.0/24" or "2001:db8::/32").
7
+ * Used to calculate the immutable prefix and available IP range.
8
+ */
9
+ cidr: string;
10
+ /**
11
+ * The full IP address value (if available).
12
+ * For IPv4: e.g., "192.168.1.100"
13
+ * For IPv6: e.g., "2001:db8::1"
14
+ */
15
+ ip: string;
16
+ /**
17
+ * The name attribute for the input field.
18
+ */
19
+ name: string;
20
+ /**
21
+ * Callback function that is called when the IP address changes.
22
+ * Receives the full IP address as a string parameter.
23
+ */
24
+ onIpChange: (ip: string) => void;
25
+ }, Omit<PrefixedInputProps, "immutableText" | "maxLength" | "placeholder" | "name">>;
26
+ declare const PrefixedIpInput: ({ cidr, help, onIpChange, ip, name, ...props }: PrefixedIpInputProps) => ReactElement;
27
+ export default PrefixedIpInput;
@@ -0,0 +1,58 @@
1
+ var _excluded = ["cidr", "help", "onIpChange", "ip", "name"];
2
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
5
+ import React from "react";
6
+ import PrefixedInput from "../PrefixedInput";
7
+ import { getImmutableAndEditable, isIPv4 } from "./utils";
8
+ var PrefixedIpInput = _ref => {
9
+ var {
10
+ cidr,
11
+ help,
12
+ onIpChange,
13
+ ip,
14
+ name
15
+ } = _ref,
16
+ props = _objectWithoutProperties(_ref, _excluded);
17
+ var [networkAddress] = cidr.split("/");
18
+ var isIPV4 = isIPv4(networkAddress);
19
+ var [immutable, editable] = getImmutableAndEditable(cidr);
20
+ var inputValue = isIPV4 ? ip.split(".").slice(immutable.split(".").length).join(".") : ip.replace(immutable, "");
21
+ var getIPv4MaxLength = () => {
22
+ var immutableOctetsLength = immutable.split(".").length;
23
+ var lengths = [15, 11, 7, 3]; // Corresponding to 0-3 immutable octets
24
+ return lengths[immutableOctetsLength];
25
+ };
26
+ var maxLength = isIPV4 ? getIPv4MaxLength() : editable.length;
27
+ var placeholder = props.disabled ? "" : editable;
28
+ var setIp = editableValue => {
29
+ var fullIp = editableValue ? isIPV4 ? "".concat(immutable, ".").concat(editableValue) : "".concat(immutable).concat(editableValue) : "";
30
+ onIpChange(fullIp);
31
+ };
32
+ var handlePaste = e => {
33
+ e.preventDefault();
34
+ var pastedText = e.clipboardData.getData("text");
35
+ if (isIPV4) {
36
+ var octets = pastedText.split(".");
37
+ var trimmed = octets.slice(0 - editable.split(".").length);
38
+ var _ip = trimmed.join(".");
39
+ setIp(_ip);
40
+ } else {
41
+ var _ip2 = pastedText.replace(immutable, "");
42
+ setIp(_ip2);
43
+ }
44
+ };
45
+ return /*#__PURE__*/React.createElement(PrefixedInput, _extends({
46
+ help: help ? help : /*#__PURE__*/React.createElement(React.Fragment, null, " ", isIPV4 ? /*#__PURE__*/React.createElement(React.Fragment, null, " ", "The available range in this subnet is", " ", /*#__PURE__*/React.createElement("code", null, immutable, ".", editable, " ")) : /*#__PURE__*/React.createElement(React.Fragment, null, " ", "The available IPV6 address range is", " ", /*#__PURE__*/React.createElement("code", null, immutable, editable, " ")), "."),
47
+ immutableText: isIPV4 ? "".concat(immutable, ".") : immutable,
48
+ maxLength: maxLength,
49
+ name: name,
50
+ onPaste: handlePaste,
51
+ value: inputValue,
52
+ onChange: e => {
53
+ setIp(e.target.value);
54
+ },
55
+ placeholder: placeholder
56
+ }, props));
57
+ };
58
+ export default PrefixedIpInput;
@@ -0,0 +1,13 @@
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import PrefixedIpInput from "./PrefixedIpInput";
3
+ declare const meta: Meta<typeof PrefixedIpInput>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof PrefixedIpInput>;
6
+ export declare const IPv4Default: Story;
7
+ export declare const IPv4WithValue: Story;
8
+ export declare const IPv4WithError: Story;
9
+ export declare const IPv4Disabled: Story;
10
+ export declare const IPv6Default: Story;
11
+ export declare const IPv6WithValue: Story;
12
+ export declare const WithCustomHelp: Story;
13
+ export declare const Required: Story;
@@ -0,0 +1,128 @@
1
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
+ import React, { useState } from "react";
3
+ import PrefixedIpInput from "./PrefixedIpInput";
4
+ var PrefixedIpInputWrapper = args => {
5
+ var [ip, setIp] = useState(args.ip);
6
+ return /*#__PURE__*/React.createElement(PrefixedIpInput, _extends({}, args, {
7
+ ip: ip,
8
+ onIpChange: newIp => {
9
+ var _args$onIpChange;
10
+ setIp(newIp);
11
+ (_args$onIpChange = args.onIpChange) === null || _args$onIpChange === void 0 || _args$onIpChange.call(args, newIp);
12
+ }
13
+ }));
14
+ };
15
+ var meta = {
16
+ component: PrefixedIpInput,
17
+ tags: ["autodocs"],
18
+ argTypes: {
19
+ ip: {
20
+ control: "text"
21
+ },
22
+ cidr: {
23
+ control: "text"
24
+ },
25
+ label: {
26
+ control: "text"
27
+ },
28
+ name: {
29
+ control: "text"
30
+ },
31
+ error: {
32
+ control: "text"
33
+ },
34
+ help: {
35
+ control: "text"
36
+ },
37
+ disabled: {
38
+ control: "boolean"
39
+ },
40
+ required: {
41
+ control: "boolean"
42
+ }
43
+ },
44
+ render: args => /*#__PURE__*/React.createElement(PrefixedIpInputWrapper, args)
45
+ };
46
+ export default meta;
47
+ export var IPv4Default = {
48
+ name: "IPv4 default",
49
+ args: {
50
+ cidr: "192.168.1.0/24",
51
+ ip: "",
52
+ name: "ip-address",
53
+ label: "IP Address",
54
+ onIpChange: ip => console.log("IP changed:", ip)
55
+ }
56
+ };
57
+ export var IPv4WithValue = {
58
+ name: "IPv4 with value",
59
+ args: {
60
+ cidr: "192.168.1.0/24",
61
+ ip: "192.168.1.100",
62
+ name: "ip-address",
63
+ label: "IP Address",
64
+ onIpChange: ip => console.log("IP changed:", ip)
65
+ }
66
+ };
67
+ export var IPv4WithError = {
68
+ name: "IPv4 with error",
69
+ args: {
70
+ cidr: "192.168.1.0/24",
71
+ ip: "192.168.1.256",
72
+ name: "ip-address",
73
+ label: "IP Address",
74
+ error: "Invalid IP address",
75
+ onIpChange: ip => console.log("IP changed:", ip)
76
+ }
77
+ };
78
+ export var IPv4Disabled = {
79
+ name: "IPv4 disabled",
80
+ args: {
81
+ cidr: "192.168.1.0/24",
82
+ ip: "192.168.1.50",
83
+ name: "ip-address",
84
+ label: "IP Address",
85
+ disabled: true,
86
+ onIpChange: ip => console.log("IP changed:", ip)
87
+ }
88
+ };
89
+ export var IPv6Default = {
90
+ name: "IPv6 default",
91
+ args: {
92
+ cidr: "2001:db8::/32",
93
+ ip: "",
94
+ name: "ipv6-address",
95
+ label: "IPv6 Address",
96
+ onIpChange: ip => console.log("IP changed:", ip)
97
+ }
98
+ };
99
+ export var IPv6WithValue = {
100
+ name: "IPv6 with value",
101
+ args: {
102
+ cidr: "2001:db8::/32",
103
+ ip: "2001:db8::1",
104
+ name: "ipv6-address",
105
+ label: "IPv6 Address",
106
+ onIpChange: ip => console.log("IP changed:", ip)
107
+ }
108
+ };
109
+ export var WithCustomHelp = {
110
+ args: {
111
+ cidr: "10.0.0.0/16",
112
+ ip: "",
113
+ name: "ip-address",
114
+ label: "IP Address",
115
+ help: "Enter a custom IP address for this device",
116
+ onIpChange: ip => console.log("IP changed:", ip)
117
+ }
118
+ };
119
+ export var Required = {
120
+ args: {
121
+ cidr: "192.168.0.0/24",
122
+ ip: "",
123
+ name: "ip-address",
124
+ label: "IP Address",
125
+ required: true,
126
+ onIpChange: ip => console.log("IP changed:", ip)
127
+ }
128
+ };
@@ -0,0 +1,2 @@
1
+ export { default, type PrefixedIpInputProps } from "./PrefixedIpInput";
2
+ export { isIPv4, getIpRangeFromCidr, getFirstValidIp, convertIpToUint32, isIpInSubnet, getImmutableAndEditableOctets, getImmutableAndEditable, } from "./utils";
@@ -0,0 +1,2 @@
1
+ export { default } from "./PrefixedIpInput";
2
+ export { isIPv4, getIpRangeFromCidr, getFirstValidIp, convertIpToUint32, isIpInSubnet, getImmutableAndEditableOctets, getImmutableAndEditable } from "./utils";
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Checks if a given IP address is a valid IPv4 address.
3
+ * @param ip The IP address to check
4
+ * @returns True if the IP is a valid IPv4 address, false otherwise
5
+ */
6
+ export declare const isIPv4: (ip: string) => boolean;
7
+ /**
8
+ * Takes a subnet CIDR notation (IPv4) and returns the first and last IP of the subnet.
9
+ * The network and host addresses are excluded.
10
+ *
11
+ * @param cidr The CIDR notation of the subnet
12
+ * @returns The first and last valid IP addresses as two strings in a list.
13
+ */
14
+ export declare const getIpRangeFromCidr: (cidr: string) => string[];
15
+ export declare const getFirstValidIp: (ip: string) => string;
16
+ export declare const convertIpToUint32: (ip: string) => number;
17
+ /**
18
+ * Checks if an IPv4 address is valid for the given subnet.
19
+ *
20
+ * @param ip The IPv4 address to check, as a string
21
+ * @param cidr The subnet's CIDR notation e.g. 192.168.0.0/24
22
+ * @returns True if the IP is in the subnet, false otherwise
23
+ */
24
+ export declare const isIpInSubnet: (ip: string, cidr: string) => boolean;
25
+ /**
26
+ * Separates the immutable and editable octets of an IPv4 subnet range.
27
+ *
28
+ * @param startIp The start IP of the subnet
29
+ * @param endIp The end IP of the subnet
30
+ * @returns The immutable and editable octects as two strings in a list
31
+ */
32
+ export declare const getImmutableAndEditableOctets: (startIp: string, endIp: string) => string[];
33
+ /**
34
+ * Get the immutable and editable parts of an IPv4 or IPv6 subnet.
35
+ *
36
+ * @param cidr The CIDR notation of the subnet
37
+ * @returns The immutable and editable as two strings in a list
38
+ */
39
+ export declare const getImmutableAndEditable: (cidr: string) => string[];
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Checks if a given IP address is a valid IPv4 address.
3
+ * @param ip The IP address to check
4
+ * @returns True if the IP is a valid IPv4 address, false otherwise
5
+ */
6
+ export var isIPv4 = ip => {
7
+ var ipv4Regex = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
8
+ return ipv4Regex.test(ip);
9
+ };
10
+
11
+ /**
12
+ * Takes a subnet CIDR notation (IPv4) and returns the first and last IP of the subnet.
13
+ * The network and host addresses are excluded.
14
+ *
15
+ * @param cidr The CIDR notation of the subnet
16
+ * @returns The first and last valid IP addresses as two strings in a list.
17
+ */
18
+ export var getIpRangeFromCidr = cidr => {
19
+ // https://gist.github.com/binarymax/6114792
20
+
21
+ // Get start IP and number of valid addresses
22
+ var [startIp, mask] = cidr.split("/");
23
+ var numberOfAddresses = (1 << 32 - parseInt(mask)) - 1;
24
+
25
+ // IPv4 can be represented by an unsigned 32-bit integer, so we can use a Uint32Array to store the IP
26
+ var buffer = new ArrayBuffer(4); //4 octets
27
+ var int32 = new Uint32Array(buffer);
28
+
29
+ // Convert starting IP to Uint32 and add the number of addresses to get the end IP.
30
+ // Subtract 1 from the number of addresses to exclude the broadcast address.
31
+ int32[0] = convertIpToUint32(startIp) + numberOfAddresses - 1;
32
+
33
+ // Convert the buffer to a Uint8Array to get the octets, then convert it to an array
34
+ var arrayApplyBuffer = Array.from(new Uint8Array(buffer));
35
+
36
+ // Reverse the octets and join them with "." to get the end IP
37
+ var endIp = arrayApplyBuffer.reverse().join(".");
38
+ var firstValidIp = getFirstValidIp(startIp);
39
+ return [firstValidIp, endIp];
40
+ };
41
+ export var getFirstValidIp = ip => {
42
+ var buffer = new ArrayBuffer(4); //4 octets
43
+ var int32 = new Uint32Array(buffer);
44
+
45
+ // add 1 because the first IP is the network address
46
+ int32[0] = convertIpToUint32(ip) + 1;
47
+ var arrayApplyBuffer = Array.from(new Uint8Array(buffer));
48
+ return arrayApplyBuffer.reverse().join(".");
49
+ };
50
+ export var convertIpToUint32 = ip => {
51
+ var octets = ip.split(".").map(a => parseInt(a));
52
+ var buffer = new ArrayBuffer(4);
53
+ var int32 = new Uint32Array(buffer);
54
+ int32[0] = (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) + octets[3];
55
+ return int32[0];
56
+ };
57
+
58
+ /**
59
+ * Checks if an IPv4 address is valid for the given subnet.
60
+ *
61
+ * @param ip The IPv4 address to check, as a string
62
+ * @param cidr The subnet's CIDR notation e.g. 192.168.0.0/24
63
+ * @returns True if the IP is in the subnet, false otherwise
64
+ */
65
+ export var isIpInSubnet = (ip, cidr) => {
66
+ var [startIP, endIP] = getIpRangeFromCidr(cidr);
67
+ var ipUint32 = convertIpToUint32(ip);
68
+ var startIPUint32 = convertIpToUint32(startIP);
69
+ var endIPUint32 = convertIpToUint32(endIP);
70
+ return ipUint32 >= startIPUint32 && ipUint32 <= endIPUint32;
71
+ };
72
+
73
+ /**
74
+ * Separates the immutable and editable octets of an IPv4 subnet range.
75
+ *
76
+ * @param startIp The start IP of the subnet
77
+ * @param endIp The end IP of the subnet
78
+ * @returns The immutable and editable octects as two strings in a list
79
+ */
80
+ export var getImmutableAndEditableOctets = (startIp, endIp) => {
81
+ var startIpOctetList = startIp.split(".");
82
+ var endIpOctetList = endIp.split(".");
83
+ var immutable = [];
84
+ var editable = [];
85
+ startIpOctetList.forEach((octet, index) => {
86
+ if (octet === endIpOctetList[index]) {
87
+ immutable.push(octet);
88
+ } else {
89
+ editable.push("[".concat(octet, "-").concat(endIpOctetList[index], "]"));
90
+ }
91
+ });
92
+ return [immutable.join("."), editable.join(".")];
93
+ };
94
+
95
+ /**
96
+ * Get the immutable and editable parts of an IPv4 or IPv6 subnet.
97
+ *
98
+ * @param cidr The CIDR notation of the subnet
99
+ * @returns The immutable and editable as two strings in a list
100
+ */
101
+ export var getImmutableAndEditable = cidr => {
102
+ var isIPV4 = isIPv4(cidr.split("/")[0]);
103
+ if (isIPV4) {
104
+ var [startIp, endIp] = getIpRangeFromCidr(cidr);
105
+ return getImmutableAndEditableOctets(startIp, endIp);
106
+ }
107
+ var [networkAddress] = cidr.split("/");
108
+ var immutableIPV6 = networkAddress.substring(0, networkAddress.lastIndexOf(":"));
109
+ var ipv6PlaceholderColons = 7 - (immutableIPV6.match(/:/g) || []).length; // 7 is the maximum number of colons in an IPv6 address
110
+ var editableIPV6 = "".concat("0000:".repeat(ipv6PlaceholderColons), "0000");
111
+ return [immutableIPV6, editableIPV6];
112
+ };
@@ -43,6 +43,8 @@ export { default as LoginPageLayout } from "./components/LoginPageLayout";
43
43
  export { default as Pagination } from "./components/Pagination";
44
44
  export { default as Panel } from "./components/Panel";
45
45
  export { default as PasswordToggle } from "./components/PasswordToggle";
46
+ export { default as PrefixedInput } from "./components/PrefixedInput";
47
+ export { default as PrefixedIpInput, isIPv4, getIpRangeFromCidr, getFirstValidIp, convertIpToUint32, isIpInSubnet, getImmutableAndEditableOctets, getImmutableAndEditable, } from "./components/PrefixedIpInput";
46
48
  export { default as RadioInput } from "./components/RadioInput";
47
49
  export { default as Row } from "./components/Row";
48
50
  export { default as ScrollableContainer } from "./components/ScrollableContainer";
@@ -118,6 +120,8 @@ export type { NotificationAction, NotificationType, QueuedNotification, Notifica
118
120
  export type { LoginPageLayoutProps } from "./components/LoginPageLayout";
119
121
  export type { PaginationProps } from "./components/Pagination";
120
122
  export type { PanelProps } from "./components/Panel";
123
+ export type { PrefixedInputProps } from "./components/PrefixedInput";
124
+ export type { PrefixedIpInputProps } from "./components/PrefixedIpInput";
121
125
  export type { RadioInputProps } from "./components/RadioInput";
122
126
  export type { RowProps } from "./components/Row";
123
127
  export type { ScrollableTableProps } from "./components/ScrollableTable";
package/dist/esm/index.js CHANGED
@@ -43,6 +43,8 @@ export { default as LoginPageLayout } from "./components/LoginPageLayout";
43
43
  export { default as Pagination } from "./components/Pagination";
44
44
  export { default as Panel } from "./components/Panel";
45
45
  export { default as PasswordToggle } from "./components/PasswordToggle";
46
+ export { default as PrefixedInput } from "./components/PrefixedInput";
47
+ export { default as PrefixedIpInput, isIPv4, getIpRangeFromCidr, getFirstValidIp, convertIpToUint32, isIpInSubnet, getImmutableAndEditableOctets, getImmutableAndEditable } from "./components/PrefixedIpInput";
46
48
  export { default as RadioInput } from "./components/RadioInput";
47
49
  export { default as Row } from "./components/Row";
48
50
  export { default as ScrollableContainer } from "./components/ScrollableContainer";
package/dist/index.d.ts CHANGED
@@ -43,6 +43,8 @@ export { default as LoginPageLayout } from "./components/LoginPageLayout";
43
43
  export { default as Pagination } from "./components/Pagination";
44
44
  export { default as Panel } from "./components/Panel";
45
45
  export { default as PasswordToggle } from "./components/PasswordToggle";
46
+ export { default as PrefixedInput } from "./components/PrefixedInput";
47
+ export { default as PrefixedIpInput, isIPv4, getIpRangeFromCidr, getFirstValidIp, convertIpToUint32, isIpInSubnet, getImmutableAndEditableOctets, getImmutableAndEditable, } from "./components/PrefixedIpInput";
46
48
  export { default as RadioInput } from "./components/RadioInput";
47
49
  export { default as Row } from "./components/Row";
48
50
  export { default as ScrollableContainer } from "./components/ScrollableContainer";
@@ -118,6 +120,8 @@ export type { NotificationAction, NotificationType, QueuedNotification, Notifica
118
120
  export type { LoginPageLayoutProps } from "./components/LoginPageLayout";
119
121
  export type { PaginationProps } from "./components/Pagination";
120
122
  export type { PanelProps } from "./components/Panel";
123
+ export type { PrefixedInputProps } from "./components/PrefixedInput";
124
+ export type { PrefixedIpInputProps } from "./components/PrefixedIpInput";
121
125
  export type { RadioInputProps } from "./components/RadioInput";
122
126
  export type { RowProps } from "./components/Row";
123
127
  export type { ScrollableTableProps } from "./components/ScrollableTable";