Page MenuHomeSealhub

phone-number-with-country-code.ts
No OneTemporary

phone-number-with-country-code.ts

import * as countryCodes from "country-codes-list";
import { Context } from "koa";
import { FieldTypes as SealiousFieldTypes } from "sealious";
import { FormControl } from "../controls/controls.js";
import { Hybrid } from "../controls/hybrid.js";
import { FormDataValue } from "../form-types.js";
import { FieldParseResult, FormField } from "./field.js";
import { PickFromListField } from "./pick-from-list.js";
import { PhoneNumberWithoutCountryCode } from "./phone-number.js";
import { FormControlContext } from "../../index.js";
export class PhoneNumberWithCountryCode<
Required extends boolean,
> extends FormField<Required, SealiousFieldTypes.PhoneNumberValue> {
public getEmptyValue(): SealiousFieldTypes.PhoneNumberValue {
return new SealiousFieldTypes.PhoneNumberValue("48", "0000000");
}
async parse(
ctx: Context,
raw_value: FormDataValue
): Promise<FieldParseResult<SealiousFieldTypes.PhoneNumberValue>> {
try {
return {
parsable: true,
parsed_value: SealiousFieldTypes.PhoneNumberValue.fromInput(
ctx.$context,
raw_value
),
error: null,
};
} catch (e) {
return {
parsable: false,
parsed_value: null,
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
error: e.message as string,
};
}
}
getControl(): FormControl {
return new Hybrid(this, {
country_code: new PickFromListField(this.required, async () =>
countryCodes
.all()
.sort((a, b) =>
a.countryCallingCode < b.countryCallingCode ? -1 : 1
)
.map((code) => ({
value: code.countryCallingCode,
label: "+" + code.countryCallingCode + " " + code.flag,
}))
).setLabel("Country code"),
number: new PhoneNumberWithoutCountryCode(this.required).setLabel(
"Phone number"
),
});
}
async getSealiousCreateValue(fctx: FormControlContext): Promise<string> {
const { parsed } = await this.getParsedValue(
fctx.ctx,
fctx.data.raw_values
);
if (!parsed) {
return "";
}
return parsed.toString();
}
async sealiousValueToForm(
ctx: Context,
sealiousValue: SealiousFieldTypes.PhoneNumberValue | null
): Promise<FormDataValue> {
return sealiousValue ?
{
country_code: sealiousValue.country_code,
number: sealiousValue.number,
}
: {};
}
}

File Metadata

Mime Type
text/x-java
Expires
Sun, Nov 2, 17:57 (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1029151
Default Alt Text
phone-number-with-country-code.ts (2 KB)

Event Timeline