@mrshmllw/smores-react 10.0.0 → 10.1.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.
package/README.md CHANGED
@@ -166,6 +166,7 @@ yalc remove --all
166
166
  * [Tag](https://github.com/marshmallow-insurance/smores-react/tree/master/src/Tag)
167
167
  * [Text](https://github.com/marshmallow-insurance/smores-react/tree/master/src/Text)
168
168
  * [Textarea](https://github.com/marshmallow-insurance/smores-react/tree/master/src/Textarea)
169
+ * [TextDateOfBirthInput](https://github.com/marshmallow-insurance/smores-react/tree/master/src/TextDateOfBirthInput)
169
170
  * [TextInput](https://github.com/marshmallow-insurance/smores-react/tree/master/src/TextInput)
170
171
  * [Toggle](https://github.com/marshmallow-insurance/smores-react/tree/master/src/Toggle)
171
172
  * [Tooltip](https://github.com/marshmallow-insurance/smores-react/tree/master/src/Tooltip)
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+ import { FieldsetProps } from '../fields/Fieldset';
3
+ export type DateObject = {
4
+ day?: string | null;
5
+ month?: string | null;
6
+ year?: string | null;
7
+ };
8
+ export type TextDateOfBirthInputProps = {
9
+ value: DateObject;
10
+ onChange: (value: DateObject) => void;
11
+ showCompleted?: boolean;
12
+ hookformError?: {
13
+ type: string;
14
+ message?: string;
15
+ };
16
+ dataTestId?: string;
17
+ } & Pick<FieldsetProps, 'label' | 'assistiveText'>;
18
+ /**
19
+ * Renders a set of input fields to collect a date of birth as separate day, month,
20
+ * and year fields.
21
+ *
22
+ * ### Date of birth input validations
23
+ * - All fields are present and correctly formatted.
24
+ * - The age is at least 17 years old.
25
+ * - The year is not before 1900.
26
+ */
27
+ export declare const TextDateOfBirthInput: React.ForwardRefExoticComponent<{
28
+ value: DateObject;
29
+ onChange: (value: DateObject) => void;
30
+ showCompleted?: boolean;
31
+ hookformError?: {
32
+ type: string;
33
+ message?: string;
34
+ };
35
+ dataTestId?: string;
36
+ } & Pick<FieldsetProps, "label" | "assistiveText"> & React.RefAttributes<HTMLInputElement>>;
@@ -0,0 +1,48 @@
1
+ import React, { forwardRef } from 'react';
2
+ import styled from 'styled-components';
3
+ import { Box } from '../Box';
4
+ import { Fieldset } from '../fields/Fieldset';
5
+ import { TextInput } from '../TextInput';
6
+ import { InputValidationError } from '../utils/dateOfBirth/schema';
7
+ /**
8
+ * Renders a set of input fields to collect a date of birth as separate day, month,
9
+ * and year fields.
10
+ *
11
+ * ### Date of birth input validations
12
+ * - All fields are present and correctly formatted.
13
+ * - The age is at least 17 years old.
14
+ * - The year is not before 1900.
15
+ */
16
+ export const TextDateOfBirthInput = forwardRef(function TextDateOfBirthInput({ onChange, value, label, assistiveText, hookformError, showCompleted = false, dataTestId, }, ref) {
17
+ const inputValidationErrorValues = Object.values(InputValidationError);
18
+ return (React.createElement(Fieldset, { label: label, assistiveText: assistiveText, completed: showCompleted && Boolean(value.day && value.month && value.year), error: Boolean(hookformError), errorMsg: hookformError?.message, "data-testid": dataTestId },
19
+ React.createElement(Box, { flex: true, gap: "16px" },
20
+ React.createElement(SetWidthTextInput, { ref: ref, label: "day", autoCompleteAttr: "bday-day", inputModeAttr: "numeric", "$width": 60, value: value.day || '', placeholder: "", onChange: (day) => {
21
+ onChange({
22
+ ...value,
23
+ day,
24
+ });
25
+ }, error: Boolean(hookformError?.type?.includes('day') ||
26
+ (hookformError?.type &&
27
+ inputValidationErrorValues.includes(hookformError.type))) }),
28
+ React.createElement(SetWidthTextInput, { label: "month", autoCompleteAttr: "bday-month", inputModeAttr: "numeric", "$width": 60, value: value.month || '', placeholder: "", onChange: (month) => {
29
+ onChange({
30
+ ...value,
31
+ month,
32
+ });
33
+ }, error: Boolean(hookformError?.type?.includes('month') ||
34
+ (hookformError?.type &&
35
+ inputValidationErrorValues.includes(hookformError.type))) }),
36
+ React.createElement(SetWidthTextInput, { label: "year", autoCompleteAttr: "bday-year", inputModeAttr: "numeric", "$width": 80, value: value.year || '', placeholder: "", onChange: (year) => {
37
+ onChange({
38
+ ...value,
39
+ year,
40
+ });
41
+ }, error: Boolean(hookformError?.type?.includes('year') ||
42
+ (hookformError?.type &&
43
+ inputValidationErrorValues.includes(hookformError.type))) }))));
44
+ });
45
+ const SetWidthTextInput = styled(TextInput) `
46
+ width: ${({ $width }) => `${$width}px`};
47
+ `;
48
+ //# sourceMappingURL=TextDateOfBirthInput.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TextDateOfBirthInput.js","sourceRoot":"","sources":["../../src/TextDateOfBirthInput/TextDateOfBirthInput.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AACzC,OAAO,MAAM,MAAM,mBAAmB,CAAA;AAEtC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAA;AAC5B,OAAO,EAAE,QAAQ,EAAiB,MAAM,oBAAoB,CAAA;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAmBlE;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,UAAU,CAG5C,SAAS,oBAAoB,CAC7B,EACE,QAAQ,EACR,KAAK,EACL,KAAK,EACL,aAAa,EACb,aAAa,EACb,aAAa,GAAG,KAAK,EACrB,UAAU,GACX,EACD,GAAG;IAEH,MAAM,0BAA0B,GAAG,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAA;IAEtE,OAAO,CACL,oBAAC,QAAQ,IACP,KAAK,EAAE,KAAK,EACZ,aAAa,EAAE,aAAa,EAC5B,SAAS,EACP,aAAa,IAAI,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,EAElE,KAAK,EAAE,OAAO,CAAC,aAAa,CAAC,EAC7B,QAAQ,EAAE,aAAa,EAAE,OAAO,iBACnB,UAAU;QAEvB,oBAAC,GAAG,IAAC,IA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AI,EAAE,EAAE;oBACjB,QAAQ,CAAC;wBACP,GAAG,KAAK;wBACR,IAAI;qBACL,CAAC,CAAA;gBACJ,CAAC,EACD,KAAK,EAAE,OAAO,CACZ,aAAa,EAAE,IAAI,EAAE,QAAQ,CAAC,MAAM,CAAC;oBACnC,CAAC,aAAa,EAAE,IAAI;wBAClB,0BAA0B,CAAC,QAAQ,CACjC,aAAa,CAAC,IAA4B,CAC3C,CAAC,CACP,GACD,CACE,CACG,CACZ,CAAA;AACH,CAAC,CAAC,CAAA;AAEF,MAAM,iBAAiB,GAAG,MAAM,CAAC,SAAS,CAAC,CAAoB;WACpD,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,MAAM,IAAI;CACvC,CAAA"}
@@ -0,0 +1 @@
1
+ export * from './TextDateOfBirthInput';
@@ -0,0 +1,2 @@
1
+ export * from './TextDateOfBirthInput';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/TextDateOfBirthInput/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA"}
package/dist/index.d.ts CHANGED
@@ -42,8 +42,11 @@ export * from './Table';
42
42
  export * from './Tag';
43
43
  export * from './Text';
44
44
  export * from './Textarea';
45
+ export * from './TextDateOfBirthInput';
45
46
  export * from './TextInput';
46
47
  export * from './theme';
47
48
  export * from './Toggle';
48
49
  export * from './Tooltip';
49
50
  export * from './utils/space';
51
+ export * from './utils/dateOfBirth/dateOfBirthValidator';
52
+ export * from './utils/dateOfBirth/schema';
package/dist/index.js CHANGED
@@ -42,9 +42,12 @@ export * from './Table';
42
42
  export * from './Tag';
43
43
  export * from './Text';
44
44
  export * from './Textarea';
45
+ export * from './TextDateOfBirthInput';
45
46
  export * from './TextInput';
46
47
  export * from './theme';
47
48
  export * from './Toggle';
48
49
  export * from './Tooltip';
49
50
  export * from './utils/space';
51
+ export * from './utils/dateOfBirth/dateOfBirthValidator';
52
+ export * from './utils/dateOfBirth/schema';
50
53
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,kBAAkB,CAAA;AAChC,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA;AACxB,cAAc,OAAO,CAAA;AACrB,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA;AACxB,cAAc,QAAQ,CAAA;AACtB,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,QAAQ,CAAA;AACtB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,QAAQ,CAAA;AACtB,cAAc,cAAc,CAAA;AAC5B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,QAAQ,CAAA;AACtB,cAAc,UAAU,CAAA;AACxB,cAAc,QAAQ,CAAA;AACtB,cAAc,SAAS,CAAA;AACvB,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,kBAAkB,CAAA;AAChC,cAAc,OAAO,CAAA;AACrB,cAAc,eAAe,CAAA;AAC7B,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,kBAAkB,CAAA;AAChC,cAAc,SAAS,CAAA;AACvB,cAAc,OAAO,CAAA;AACrB,cAAc,QAAQ,CAAA;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AAc,SAAS,CAAA;AACvB,cAAc,OAAO,CAAA;AACrB,cAAc,QAAQ,CAAA;AACtB,cAAc,YAAY,CAAA;AAC1B,cAAc,wBAAwB,CAAA;AACtC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,0CAA0C,CAAA;AACxD,cAAc,4BAA4B,CAAA"}
@@ -0,0 +1,11 @@
1
+ import { FieldValidationError, InputValidationError } from './schema';
2
+ import { DateObject } from 'TextDateOfBirthInput';
3
+ export declare const dateOfBirthValidator: (dateObject: DateObject) => {
4
+ error: FieldValidationError;
5
+ fields?: string[];
6
+ } | {
7
+ error: InputValidationError;
8
+ fields?: string[];
9
+ } | {
10
+ isSuccess: boolean;
11
+ };
@@ -0,0 +1,29 @@
1
+ import { isValid, parse } from 'date-fns';
2
+ import { error, getImpossibleFields, getInvalidInputFields, getMissingFields, is17YearsOld, } from './dateOfBirthValidator.utils';
3
+ import { FieldValidationError, InputValidationError } from './schema';
4
+ export const dateOfBirthValidator = (dateObject) => {
5
+ const invalidFields = getInvalidInputFields(dateObject);
6
+ if (invalidFields.length !== 0) {
7
+ return error(FieldValidationError.InvalidInputValueError, invalidFields);
8
+ }
9
+ const missingFields = getMissingFields(dateObject);
10
+ if (missingFields.length !== 0) {
11
+ return error(FieldValidationError.MissingFieldError, missingFields);
12
+ }
13
+ const impossibleValues = getImpossibleFields(dateObject);
14
+ if (impossibleValues.length !== 0) {
15
+ return error(FieldValidationError.ImpossibleFieldValueError, impossibleValues);
16
+ }
17
+ const date = parse(`${dateObject.year}-${dateObject.month}-${dateObject.day}`, 'yyyy-MM-dd', new Date());
18
+ if (isValid(date)) {
19
+ if (!is17YearsOld(date)) {
20
+ return error(InputValidationError.YoungerThan17);
21
+ }
22
+ if (date.getFullYear() < 1900) {
23
+ return error(InputValidationError.DateBefore1900Error);
24
+ }
25
+ return { isSuccess: true };
26
+ }
27
+ return error(InputValidationError.RealDateError);
28
+ };
29
+ //# sourceMappingURL=dateOfBirthValidator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dateOfBirthValidator.js","sourceRoot":"","sources":["../../../src/utils/dateOfBirth/dateOfBirthValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAEzC,OAAO,EACL,KAAK,EACL,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,YAAY,GACb,MAAM,8BAA8B,CAAA;AACrC,OAAO,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAIrE,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,UAAsB,EAAE,EAAE;IAC7D,MAAM,aAAa,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAA;IAEvD,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,OAAO,KAAK,CAAC,oBAAoB,CAAC,sBAAsB,EAAE,aAAa,CAAC,CAAA;IAC1E,CAAC;IAED,MAAM,aAAa,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAA;IAElD,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,OAAO,KAAK,CAAC,oBAAoB,CAAC,iBAAiB,EAAE,aAAa,CAAC,CAAA;IACrE,CAAC;IAED,MAAM,gBAAgB,GAAG,mBAAmB,CAAC,UAAU,CAAC,CAAA;IAExD,IAAI,gBAAgB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClC,OAAO,KAAK,CACV,oBAAoB,CAAC,yBAAyB,EAC9C,gBAAgB,CACjB,CAAA;IACH,CAAC;IAED,MAAM,IAAI,GAAG,KAAK,CAChB,GAAG,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,KAAK,IAAI,UAAU,CAAC,GAAG,EAAE,EAC1D,YAAY,EACZ,IAAI,IAAI,EAAE,CACX,CAAA;IAED,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAClB,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;YACxB,OAAO,KAAK,CAAC,oBAAoB,CAAC,aAAa,CAAC,CAAA;QAClD,CAAC;QAED,IAAI,IAAI,CAAC,WAAW,EAAE,GAAG,IAAI,EAAE,CAAC;YAC9B,OAAO,KAAK,CAAC,oBAAoB,CAAC,mBAAmB,CAAC,CAAA;QACxD,CAAC;QACD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,CAAA;IAC5B,CAAC;IACD,OAAO,KAAK,CAAC,oBAAoB,CAAC,aAAa,CAAC,CAAA;AAClD,CAAC,CAAA"}
@@ -0,0 +1,18 @@
1
+ import { DateObject } from 'TextDateOfBirthInput';
2
+ declare const dateFields: readonly ["day", "month", "year"];
3
+ export declare const monthsArray: string[];
4
+ export declare const validators: Record<(typeof dateFields)[number], (dateField: string | undefined | null) => boolean>;
5
+ export declare const isAllowedValue: (value: string | null | undefined) => boolean;
6
+ export declare const getInvalidInputFields: (dateObject: DateObject) => ("month" | "day" | "year")[];
7
+ export declare const getMissingFields: (dateObject: DateObject) => ("month" | "day" | "year")[];
8
+ export declare const getImpossibleFields: (dateObject: DateObject) => ("month" | "day" | "year")[];
9
+ export declare const humanReadableJoin: (arr: string[]) => string;
10
+ export declare const createMissingFieldErrorMessage: (missingFields: string[]) => string;
11
+ export declare const createImpossibleFieldErrorMessage: (impossibleFields: string[]) => string;
12
+ export declare const createYoungerThan17ErrorMessage: () => string;
13
+ export declare const error: <T>(type: T, fields?: string[]) => {
14
+ error: T;
15
+ fields?: string[];
16
+ };
17
+ export declare const is17YearsOld: (date: Date) => boolean;
18
+ export {};
@@ -0,0 +1,89 @@
1
+ import { isAfter, subYears } from 'date-fns';
2
+ const dateFields = ['day', 'month', 'year'];
3
+ export const monthsArray = [
4
+ 'January',
5
+ 'February',
6
+ 'March',
7
+ 'April',
8
+ 'May',
9
+ 'June',
10
+ 'July',
11
+ 'August',
12
+ 'September',
13
+ 'October',
14
+ 'November',
15
+ 'December',
16
+ ];
17
+ export const validators = {
18
+ day: (day) => {
19
+ const dayNumber = Number(day);
20
+ return Boolean(day && dayNumber >= 1 && dayNumber <= 31);
21
+ },
22
+ month: (month) => {
23
+ const monthNumber = Number(month);
24
+ return Boolean(month && monthNumber >= 1 && monthNumber <= 12);
25
+ },
26
+ year: (year) => {
27
+ const yearNumber = Number(year);
28
+ return Boolean(year && yearNumber && year.length === 4 && yearNumber > 0);
29
+ },
30
+ };
31
+ export const isAllowedValue = (value) => {
32
+ return value === null || (value && /^\d+$/.test(value)) || value === '';
33
+ };
34
+ export const getInvalidInputFields = (dateObject) => {
35
+ const invalidValues = dateFields.filter((field) => {
36
+ const value = dateObject[field];
37
+ return !isAllowedValue(value);
38
+ });
39
+ return invalidValues;
40
+ };
41
+ export const getMissingFields = (dateObject) => {
42
+ const missingFields = dateFields.filter((field) => {
43
+ return !dateObject[field];
44
+ });
45
+ return missingFields;
46
+ };
47
+ export const getImpossibleFields = (dateObject) => {
48
+ const impossibleValues = dateFields.filter((field) => {
49
+ return !validators[field](dateObject[field]);
50
+ });
51
+ return impossibleValues;
52
+ };
53
+ export const humanReadableJoin = (arr) => {
54
+ if (arr.length === 0) {
55
+ return '';
56
+ }
57
+ else if (arr.length === 1) {
58
+ return arr[0];
59
+ }
60
+ else if (arr.length === 2) {
61
+ return `${arr[0]} and ${arr[1]}`;
62
+ }
63
+ else {
64
+ const last = arr.pop();
65
+ return `${arr.join(', ')} and ${last}`;
66
+ }
67
+ };
68
+ export const createMissingFieldErrorMessage = (missingFields) => {
69
+ const beginning = 'Date of birth must include ';
70
+ const orderedMissingFields = ['day', 'month', 'year'].filter((field) => missingFields.includes(field));
71
+ return beginning + humanReadableJoin(orderedMissingFields);
72
+ };
73
+ export const createImpossibleFieldErrorMessage = (impossibleFields) => {
74
+ const beginning = 'Please enter a valid ';
75
+ const orderedImpossibleFields = ['day', 'month', 'year'].filter((field) => impossibleFields.includes(field));
76
+ return beginning + humanReadableJoin(orderedImpossibleFields);
77
+ };
78
+ export const createYoungerThan17ErrorMessage = () => {
79
+ const comparisonDate = subYears(new Date(), 17);
80
+ return `Please enter a date of birth on or before ${comparisonDate.getDate()} ${monthsArray[comparisonDate.getMonth()]} ${comparisonDate.getFullYear()}`;
81
+ };
82
+ export const error = (type, fields) => {
83
+ return { error: type, fields };
84
+ };
85
+ export const is17YearsOld = (date) => {
86
+ const comparisonDate = subYears(new Date(), 17);
87
+ return !isAfter(date, comparisonDate);
88
+ };
89
+ //# sourceMappingURL=dateOfBirthValidator.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dateOfBirthValidator.utils.js","sourceRoot":"","sources":["../../../src/utils/dateOfBirth/dateOfBirthValidator.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAI5C,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAU,CAAA;AAEpD,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,SAAS;IACT,UAAU;IACV,OAAO;IACP,OAAO;IACP,KAAK;IACL,MAAM;IACN,MAAM;IACN,QAAQ;IACR,WAAW;IACX,SAAS;IACT,UAAU;IACV,UAAU;CACX,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAGnB;IACF,GAAG,EAAE,CAAC,GAAG,EAAE,EAAE;QACX,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;QAC7B,OAAO,OAAO,CAAC,GAAG,IAAI,SAAS,IAAI,CAAC,IAAI,SAAS,IAAI,EAAE,CAAC,CAAA;IAC1D,CAAC;IACD,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;QACf,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;QACjC,OAAO,OAAO,CAAC,KAAK,IAAI,WAAW,IAAI,CAAC,IAAI,WA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E,EAAE,CACxE,gBAAgB,CAAC,QAAQ,CAAC,KAAK,CAAC,CACjC,CAAA;IAED,OAAO,SAAS,GAAG,iBAAiB,CAAC,uBAAuB,CAAC,CAAA;AAC/D,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,+BAA+B,GAAG,GAAG,EAAE;IAClD,MAAM,cAAc,GAAG,QAAQ,CAAC,IAAI,IAAI,EAAE,EAAE,EAAE,CAAC,CAAA;IAE/C,OAAO,6CAA6C,cAAc,CAAC,OAAO,EAAE,IAAI,WAAW,CAAC,cAAc,CAAC,QAAQ,EAAE,CAAC,IAAI,cAAc,CAAC,WAAW,EAAE,EAAE,CAAA;AAC1J,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,KAAK,GAAG,CACnB,IAAO,EACP,MAAiB,EAIjB,EAAE;IACF,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,CAAA;AAChC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,IAAU,EAAE,EAAE;IACzC,MAAM,cAAc,GAAG,QAAQ,CAAC,IAAI,IAAI,EAAE,EAAE,EAAE,CAAC,CAAA;IAE/C,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,cAAc,CAAC,CAAA;AACvC,CAAC,CAAA"}
@@ -0,0 +1,12 @@
1
+ export declare enum InputValidationError {
2
+ RealDateError = "realDateError",
3
+ DateBefore1900Error = "dateBefore1900Error",
4
+ YoungerThan17 = "youngerThan17"
5
+ }
6
+ export declare enum FieldValidationError {
7
+ MissingFieldError = "missingFieldError",
8
+ InvalidInputValueError = "invalidInputValueError",
9
+ ImpossibleFieldValueError = "impossibleFieldValueError"
10
+ }
11
+ export declare const dateOfBirthErrorMessages: Record<InputValidationError | FieldValidationError, (value: string[]) => string>;
12
+ export declare const createErrorMessage: (errorKey: keyof typeof dateOfBirthErrorMessages, fields?: string[]) => string;
@@ -0,0 +1,28 @@
1
+ import { createImpossibleFieldErrorMessage, createMissingFieldErrorMessage, createYoungerThan17ErrorMessage, } from './dateOfBirthValidator.utils';
2
+ export var InputValidationError;
3
+ (function (InputValidationError) {
4
+ InputValidationError["RealDateError"] = "realDateError";
5
+ InputValidationError["DateBefore1900Error"] = "dateBefore1900Error";
6
+ InputValidationError["YoungerThan17"] = "youngerThan17";
7
+ })(InputValidationError || (InputValidationError = {}));
8
+ export var FieldValidationError;
9
+ (function (FieldValidationError) {
10
+ FieldValidationError["MissingFieldError"] = "missingFieldError";
11
+ FieldValidationError["InvalidInputValueError"] = "invalidInputValueError";
12
+ FieldValidationError["ImpossibleFieldValueError"] = "impossibleFieldValueError";
13
+ })(FieldValidationError || (FieldValidationError = {}));
14
+ export const dateOfBirthErrorMessages = {
15
+ [InputValidationError.RealDateError]: () => 'Please enter a valid date of birth',
16
+ [InputValidationError.DateBefore1900Error]: () => 'Please enter a date of birth after 1900',
17
+ [FieldValidationError.MissingFieldError]: createMissingFieldErrorMessage,
18
+ [FieldValidationError.InvalidInputValueError]: () => 'Please enter a numeric value',
19
+ [FieldValidationError.ImpossibleFieldValueError]: createImpossibleFieldErrorMessage,
20
+ [InputValidationError.YoungerThan17]: createYoungerThan17ErrorMessage,
21
+ };
22
+ export const createErrorMessage = (errorKey, fields) => {
23
+ const messageFunc = dateOfBirthErrorMessages[errorKey];
24
+ const message = messageFunc(fields ?? []);
25
+ const fieldsSuffix = fields ? ` (${fields.join('-')})` : '';
26
+ return message + fieldsSuffix;
27
+ };
28
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/utils/dateOfBirth/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,8BAA8B,EAC9B,+BAA+B,GAChC,MAAM,8BAA8B,CAAA;AAErC,MAAM,CAAN,IAAY,oBAIX;AAJD,WAAY,oBAAoB;IAC9B,uDAA+B,CAAA;IAC/B,mEAA2C,CAAA;IAC3C,uDAA+B,CAAA;AACjC,CAAC,EAJW,oBAAoB,KAApB,oBAAoB,QAI/B;AAED,MAAM,CAAN,IAAY,oBAIX;AAJD,WAAY,oBAAoB;IAC9B,+DAAuC,CAAA;IACvC,yEAAiD,CAAA;IACjD,+EAAuD,CAAA;AACzD,CAAC,EAJW,oBAAoB,KAApB,oBAAoB,QAI/B;AAED,MAAM,CAAC,MAAM,wBAAwB,GAGjC;IACF,CAAC,oBAAoB,CAAC,aAAa,CAAC,EAAE,GAAG,EAAE,CACzC,oCAAoC;IACtC,CAAC,oBAAoB,CAAC,mBAAmB,CAAC,EAAE,GAAG,EAAE,CAC/C,yCAAyC;IAC3C,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAAE,8BAA8B;IACxE,CAAC,oBAAoB,CAAC,sBAAsB,CAAC,EAAE,GAAG,EAAE,CAClD,8BAA8B;IAChC,CAAC,oBAAoB,CAAC,yBAAyB,CAAC,EAC9C,iCAAiC;IACnC,CAAC,oBAAoB,CAAC,aAAa,CAAC,EAAE,+BAA+B;CACtE,CAAA;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,QAA+C,EAC/C,MAAiB,EACjB,EAAE;IACF,MAAM,WAAW,GAAG,wBAAwB,CAAC,QAAQ,CAAC,CAAA;IACtD,MAAM,OAAO,GAAG,WAAW,CAAC,MAAM,IAAI,EAAE,CAAC,CAAA;IACzC,MAAM,YAAY,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAA;IAC3D,OAAO,OAAO,GAAG,YAAY,CAAA;AAC/B,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrshmllw/smores-react",
3
- "version": "10.0.0",
3
+ "version": "10.1.0",
4
4
  "main": "./dist/index.js",
5
5
  "description": "Collection of React components used by Marshmallow Technology",
6
6
  "keywords": [
@@ -43,7 +43,7 @@
43
43
  "@commitlint/types": "^19.5.0",
44
44
  "@semantic-release/changelog": "^6.0.3",
45
45
  "@semantic-release/git": "^10.0.1",
46
- "@semantic-release/github": "^11.0.0",
46
+ "@semantic-release/github": "^11.0.1",
47
47
  "@semantic-release/npm": "^12.0.0",
48
48
  "@snyk/protect": "^1.1294.0",
49
49
  "@storybook/addon-a11y": "^8.4.2",
@@ -58,7 +58,7 @@
58
58
  "@testing-library/react": "^16.0.1",
59
59
  "@types/body-scroll-lock": "^3.1.0",
60
60
  "@types/dompurify": "^3.0.5",
61
- "@types/node": "^22.8.7",
61
+ "@types/node": "^22.9.0",
62
62
  "@types/react": "^18.0.24",
63
63
  "@types/react-dom": "^18.0.8",
64
64
  "@typescript-eslint/eslint-plugin": "^7.18.0",