Skip to content

Commit

Permalink
chore: consolidate duplicate react/angular files into common
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisolsen committed Jan 16, 2025
1 parent 8add27f commit aa9cdf6
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 289 deletions.
1 change: 1 addition & 0 deletions libs/angular-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"directory": "libs/angular-components"
},
"peerDependencies": {
"@abgov/ui-components-common": "^0.0.0 || ^1.0.0",
"@angular/forms": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
"@angular/common": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
"@angular/core": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
Expand Down
3 changes: 1 addition & 2 deletions libs/angular-components/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./lib/angular-components.module";
export * from "./lib/components";
export * from "./lib/validation";
export * from "./lib/public-form-utils";
export * from "@abgov/ui-components-common";
3 changes: 2 additions & 1 deletion libs/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"module": "./index.js",
"publishConfig": {
"access": "public"
}
},
"semantic-release": "semantic-release"
}
2 changes: 2 additions & 0 deletions libs/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./lib/common";
export * from "./lib/validators";
export * from "./lib/public-form-controller";
4 changes: 2 additions & 2 deletions libs/common/src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export type GoabSkeletonSize = "1" | "2" | "3" | "4";

export type GoabRadioGroupOrientation = "horizontal" | "vertical";

export interface GoabRadioGroup extends Margins {
export interface GoabRadioGroupProps extends Margins {
name: string;
value?: string;
disabled?: boolean;
Expand All @@ -228,7 +228,7 @@ export interface GoabRadioGroup extends Margins {
ariaLabel?: string;
}

export interface GoabRadioItem {
export interface GoabRadioItemProps {
value?: string;
label?: string;
name?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldValidator } from "./validation";
import { FieldsetItemState, FieldValidator } from "./validators";

export type FormStatus = "not-started" | "incomplete" | "complete";

Expand All @@ -21,13 +21,6 @@ export type Fieldset<T> = {
| { type: "list"; items: AppState<T>[] };
};

// Public type to define the state of the fieldset items
export type FieldsetItemState = {
name: string;
label: string;
value: string;
};

export class PublicFormController<T> {
state?: AppState<T> | AppState<T>[];
_formData?: Record<string, string> = undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
SINValidator,
emailValidator,
postalCodeValidator,
} from "./validation";
dateValidator,
} from "./validators";

describe("Validation", () => {
describe("Email", () => {
Expand Down Expand Up @@ -153,6 +154,35 @@ describe("Validation", () => {

describe("Date", () => {
it("needs a test");
// const validValues = ["", "123456"];
// const invalidValues = ["12345"];
//
// const validate = lengthValidator({ min: 6, optional: true });
//
// for (const val of validValues) {
// it(`${val} should be valid`, () => {
// const msg = validate(val);
// expect(msg).toBe("");
// });
// }
//
// for (const val of invalidValues) {
// it(`${val} should be invalid`, () => {
// const msg = validate(val);
// expect(msg).not.toBe("");
// });
// }

describe("Start date", () => {
// const start = new Date(2025, 0, 1);
// const validator = dateValidator({ min: start });
// const validDate = new Date(2025, 2, 1);
it("needs a test");
});

// describe("End date", () => {
//
// })
});

describe("Regex", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { FieldsetItemState } from "./public-form-utils";
export type FieldsetItemState = {
name: string;
label: string;
value: string;
};

export type FieldValidator = (value: unknown) => string;
export type FieldsetState = Record<string, FieldsetItemState>;

export class FormValidator {
private readonly validators: Record<string, FieldValidator[]>;

constructor(validators?: Record<string, FieldValidator[]>) {
this.validators = validators || {};
}
Expand Down Expand Up @@ -32,10 +37,6 @@ export class FormValidator {
}
}

// **********
// Validators
// **********

export function birthDayValidator(): FieldValidator[] {
return [
requiredValidator("Day is required"),
Expand Down Expand Up @@ -155,17 +156,18 @@ export function regexValidator(regex: RegExp, msg: string): FieldValidator {

interface DateValidatorOptions {
invalidMsg?: string;
startMsg?: string;
endMsg?: string;
start?: Date;
end?: Date;
minMsg?: string;
maxMsg?: string;
min?: Date;
max?: Date;
}

export function dateValidator({
invalidMsg,
startMsg,
endMsg,
start,
end,
minMsg,
maxMsg,
min,
max,
}: DateValidatorOptions): FieldValidator {
return (date: unknown) => {
let _date: Date = new Date(0);
Expand All @@ -181,11 +183,11 @@ export function dateValidator({
return invalidMsg || "Invalid date";
}

if (_date && start && _date < start) {
return startMsg || `Must be after ${start}`;
if (_date && min && _date < min) {
return minMsg || `Must be after ${min}`;
}
if (_date && end && _date > end) {
return endMsg || `Must be before ${end}`;
if (_date && max && _date > max) {
return maxMsg || `Must be before ${max}`;
}

return "";
Expand All @@ -199,6 +201,7 @@ interface NumericValidatorOptions {
min?: number;
max?: number;
}

export function numericValidator({
invalidTypeMsg,
minMsg,
Expand Down Expand Up @@ -239,6 +242,7 @@ interface LengthValidatorOptions {
min?: number;
optional?: boolean;
}

export function lengthValidator({
invalidTypeMsg,
minMsg,
Expand Down
2 changes: 1 addition & 1 deletion libs/react-components/src/experimental/form/form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactNode, useEffect, useRef } from "react";
import { Margins, GoabFormStorageType, GoabFormOnMountDetail, GoabFormOnStateChange } from "@abgov/ui-components-common";
import { relay } from "../validators";
import { relay } from "@abgov/ui-components-common";

interface WCProps extends Margins {
ref?: React.MutableRefObject<HTMLElement | undefined>;
Expand Down
Loading

0 comments on commit aa9cdf6

Please sign in to comment.