one
Version:
One is a React framework for web, iOS, and Android apps.
111 lines (108 loc) • 4.59 kB
JavaScript
import { isSyncState } from "./syncStore.mjs";
import { validateDatePickerProps } from "./composeValidation.mjs";
function composeRangeDay(date) {
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
}
function localRangeDay(date) {
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
}
function validateComposeFamily(kind, props) {
const fail = (key) => {
throw new Error(`Compose ${kind} invalid ${key}`);
};
const number = (key, minimum = 0, maximum = Infinity) => {
const value = props[key];
if (value !== void 0 && (typeof value !== "number" || !Number.isFinite(value) || value < minimum || value > maximum)) fail(key);
};
const boolean = (key) => {
if (props[key] !== void 0 && typeof props[key] !== "boolean") fail(key);
};
const callback = (key) => {
if (props[key] !== void 0 && typeof props[key] !== "function") fail(key);
};
if (kind.endsWith("carousel")) {
for (const key of [
"maxItemWidth",
"preferredItemWidth",
"itemWidth",
"minSmallItemWidth",
"maxSmallItemWidth"
]) number(key, Number.MIN_VALUE);
number("itemSpacing");
boolean("userScrollEnabled");
if (kind === "multibrowsecarousel" && props.preferredItemWidth === void 0) fail("preferredItemWidth");
if (kind === "uncontainedcarousel" && props.itemWidth === void 0) fail("itemWidth");
if (props.minSmallItemWidth !== void 0 && props.maxSmallItemWidth !== void 0 && props.minSmallItemWidth > props.maxSmallItemWidth) fail("small item widths");
if (props.flingBehavior !== void 0 && !["singleAdvance", "noSnap"].includes(props.flingBehavior)) fail("flingBehavior");
if (typeof props.contentPadding === "number") {
if (!Number.isFinite(props.contentPadding) || props.contentPadding < 0) fail("contentPadding");
} else if (props.contentPadding !== void 0) {
if (!props.contentPadding || typeof props.contentPadding !== "object") fail("contentPadding");
for (const value of Object.values(props.contentPadding)) if (typeof value !== "number" || !Number.isFinite(value) || value < 0) fail("contentPadding");
}
} else if (kind === "shape") {
for (const key of [
"smoothing",
"cornerRounding",
"innerRadius",
"radius"
]) number(key, 0, 1);
number("verticesCount", 3);
if (props.verticesCount !== void 0 && !Number.isInteger(props.verticesCount)) fail("verticesCount");
for (const key of [
"topStart",
"topEnd",
"bottomStart",
"bottomEnd"
]) number(key);
} else if (kind === "dialog") {
callback("onDismissRequest");
if (props.properties !== void 0) {
if (!props.properties || typeof props.properties !== "object") fail("properties");
for (const [key, value] of Object.entries(props.properties)) {
if (![
"dismissOnBackPress",
"dismissOnClickOutside",
"usePlatformDefaultWidth",
"decorFitsSystemWindows"
].includes(key) || typeof value !== "boolean") fail(key);
}
}
} else if (kind === "toolbar") {
if (props.variant !== void 0 && !["standard", "vibrant"].includes(props.variant)) fail("variant");
} else if (kind === "syncswitch") {
if (!isSyncState(props.isOn) || typeof props.isOn.value !== "boolean") fail("isOn (requires NativeState<boolean>)");
boolean("enabled");
} else if (kind === "range") {
const date = (key) => {
const value = props[key];
if (value == null) return void 0;
if (typeof value !== "string" || !Number.isFinite(new Date(value).getTime())) fail(key);
return new Date(value);
};
const start = date("initialStartDate");
const end = date("initialEndDate");
if (end && (!start || composeRangeDay(end) < composeRangeDay(start))) fail("initialEndDate");
callback("onDateRangeSelected");
for (const key of [
"showTitle",
"showHeadline",
"showVariantToggle"
]) boolean(key);
validateDatePickerProps({
selection: localRangeDay(start ?? props.selectableDates?.start ?? props.selectableDates?.end ?? /* @__PURE__ */ new Date()),
minimumDate: props.selectableDates?.start && localRangeDay(props.selectableDates.start),
maximumDate: props.selectableDates?.end && localRangeDay(props.selectableDates.end),
variant: props.variant,
colors: props.elementColors,
color: props.color
});
if (end) validateDatePickerProps({
selection: localRangeDay(end),
minimumDate: props.selectableDates?.start && localRangeDay(props.selectableDates.start),
maximumDate: props.selectableDates?.end && localRangeDay(props.selectableDates.end)
});
}
}
export { composeRangeDay, validateComposeFamily };
//# sourceMappingURL=composeFamilyValidation.mjs.map