Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TreeView] Allow editing mid-id digits #3300

Merged
merged 8 commits into from
Aug 21, 2024
Merged
22 changes: 11 additions & 11 deletions src/components/TreeView/TreeSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ export default function TreeSearch(props: TreeSearchProps): ReactElement {
onKeyUp={handleOnKeyUp}
margin="normal"
autoComplete="off"
inputProps={{ "data-testid": testId }}
inputProps={{ "data-testid": testId, maxLength: 99 }}
value={input}
error={searchError}
helperText={searchError ? t("treeView.domainNotFound") : undefined}
/>
);
}

/** Automatically convert a string of form 123 to 1.2.3. */
/** Adds periods to a string of digits.
* If string is digits with at most 1 period between each digit
* (e.g.: 1234, 12.34, 123.4, 1.23.4),
* then removes all periods (e.g.: 1234)
* and inserts a period between each digit (e.g.: 1.2.3.4).
*
* Note: doesn't act on strings with double/initial/final period (e.g.: .2.3.4, 1..3.4),
* because a user may be changing a digit (e.g.: 1.0.3.4 -> 1..3.4 -> 1.2.3.4). */
export function insertDecimalPoints(value: string): string {
// Test if input is strictly of the form: 1.2.3 or 123
if (/^[.\d]+$/.test(value) && !value.endsWith(".")) {
// Automatically insert decimal points between two numbers.
value = value
.replace(/\./g, "")
.split("")
.map((char) => `${char}.`)
.join("")
.slice(0, -1);
if (/^\d(\.?\d)+$/.test(value)) {
value = value.replace(/\./g, "").split("").join(".");
}

return value;
Expand Down
20 changes: 11 additions & 9 deletions src/components/TreeView/tests/TreeSearch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,21 @@ describe("TreeSearch", () => {
});

describe("insertDecimalPoints", () => {
test.each(["a", "1a", "1", "1.", "1.0", "1-2", "1..2", "1.2.3.", ".123"])(
"does not change",
(input) => {
expect(insertDecimalPoints(input)).toBe(input);
}
);

test.each([
["a", "a"],
["1a", "1a"],
["1", "1"],
["1.", "1."],
["1.0", "1.0"],
["10", "1.0"],
["12", "1.2"],
["123", "1.2.3"],
["1.2.3.", "1.2.3."],
["..1", "1"],
["1..2", "1.2"],
])("inserts correctly", (input, output) => {
["1.23", "1.2.3"],
["12.3", "1.2.3"],
["1.23.4", "1.2.3.4"],
])("changes correctly", (input, output) => {
expect(insertDecimalPoints(input)).toBe(output);
});
});
Loading