UNPKG

react-iztro

Version:

基于iztro实现的react紫微斗数星盘组件。A react component used to generate an astrolabe of ziweidoushu based on iztro.

163 lines (162 loc) 7.81 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Iztrolabe = void 0; var react_1 = __importStar(require("react")); var Izpalace_1 = require("../Izpalace/Izpalace"); var IzpalaceCenter_1 = require("../IzpalaceCenter"); var classnames_1 = __importDefault(require("classnames")); var iztro_hook_1 = require("iztro-hook"); require("./Iztrolabe.css"); require("../theme/default.css"); var astro_1 = require("iztro/lib/astro"); var Iztrolabe = function (props) { var _a = (0, react_1.useState)(-1), taichiPoint = _a[0], setTaichiPoint = _a[1]; var _b = (0, react_1.useState)(), taichiPalaces = _b[0], setTaichiPalaces = _b[1]; var _c = (0, react_1.useState)(), activeHeavenlyStem = _c[0], setActiveHeavenlyStem = _c[1]; var _d = (0, react_1.useState)(), hoverHeavenlyStem = _d[0], setHoverHeavenlyStem = _d[1]; var _e = (0, react_1.useState)(), focusedIndex = _e[0], setFocusedIndex = _e[1]; var _f = (0, react_1.useState)(false), showDecadal = _f[0], setShowDecadal = _f[1]; var _g = (0, react_1.useState)(false), showYearly = _g[0], setShowYearly = _g[1]; var _h = (0, react_1.useState)(false), showMonthly = _h[0], setShowMonthly = _h[1]; var _j = (0, react_1.useState)(false), showDaily = _j[0], setShowDaily = _j[1]; var _k = (0, react_1.useState)(false), showHourly = _k[0], setShowShowHourly = _k[1]; var _l = (0, react_1.useState)(), horoscopeDate = _l[0], setHoroscopeDate = _l[1]; var _m = (0, react_1.useState)(), horoscopeHour = _m[0], setHoroscopeHour = _m[1]; var _o = (0, iztro_hook_1.useIztro)({ birthday: props.birthday, birthTime: props.birthTime, gender: props.gender, birthdayType: props.birthdayType, fixLeap: props.fixLeap, isLeapMonth: props.isLeapMonth, lang: props.lang, options: props.options, }), astrolabe = _o.astrolabe, horoscope = _o.horoscope, setHoroscope = _o.setHoroscope; var toggleShowScope = function (scope) { switch (scope) { case "decadal": setShowDecadal(!showDecadal); break; case "yearly": setShowYearly(!showYearly); break; case "monthly": setShowMonthly(!showMonthly); break; case "daily": setShowDaily(!showDaily); break; case "hourly": setShowShowHourly(!showHourly); break; } }; var toggleActiveHeavenlyStem = function (heavenlyStem) { if (heavenlyStem === activeHeavenlyStem) { setActiveHeavenlyStem(undefined); } else { setActiveHeavenlyStem(heavenlyStem); } }; var dynamic = (0, react_1.useMemo)(function () { if (showHourly) { return { arrowIndex: horoscope === null || horoscope === void 0 ? void 0 : horoscope.hourly.index, arrowScope: "hourly", }; } if (showDaily) { return { arrowIndex: horoscope === null || horoscope === void 0 ? void 0 : horoscope.daily.index, arrowScope: "daily", }; } if (showMonthly) { return { arrowIndex: horoscope === null || horoscope === void 0 ? void 0 : horoscope.monthly.index, arrowScope: "monthly", }; } if (showYearly) { return { arrowIndex: horoscope === null || horoscope === void 0 ? void 0 : horoscope.yearly.index, arrowScope: "yearly", }; } if (showDecadal) { return { arrowIndex: horoscope === null || horoscope === void 0 ? void 0 : horoscope.decadal.index, arrowScope: "decadal", }; } }, [showDecadal, showYearly, showMonthly, showDaily, showHourly, horoscope]); (0, react_1.useEffect)(function () { var _a, _b; setHoroscopeDate((_a = props.horoscopeDate) !== null && _a !== void 0 ? _a : new Date()); setHoroscopeHour((_b = props.horoscopeHour) !== null && _b !== void 0 ? _b : 0); }, [props.horoscopeDate, props.horoscopeHour]); (0, react_1.useEffect)(function () { setHoroscope(horoscopeDate !== null && horoscopeDate !== void 0 ? horoscopeDate : new Date(), horoscopeHour); }, [horoscopeDate, horoscopeHour]); (0, react_1.useEffect)(function () { if (taichiPoint < 0) { setTaichiPalaces(undefined); } else { var palaces = (0, astro_1.getPalaceNames)(taichiPoint); setTaichiPalaces(palaces); } }, [taichiPoint]); var toggleTaichiPoint = function (index) { if (taichiPoint === index) { setTaichiPoint(-1); } else { setTaichiPoint(index); } }; return (react_1.default.createElement("div", { className: (0, classnames_1.default)("iztro-astrolabe", "iztro-astrolabe-theme-default") }, astrolabe === null || astrolabe === void 0 ? void 0 : astrolabe.palaces.map(function (palace) { return (react_1.default.createElement(Izpalace_1.Izpalace, __assign({ key: palace.earthlyBranch, focusedIndex: focusedIndex, onFocused: setFocusedIndex, horoscope: horoscope, showDecadalScope: showDecadal, showYearlyScope: showYearly, showMonthlyScope: showMonthly, showDailyScope: showDaily, showHourlyScope: showHourly, taichiPalace: taichiPalaces === null || taichiPalaces === void 0 ? void 0 : taichiPalaces[palace.index], toggleScope: toggleShowScope, activeHeavenlyStem: activeHeavenlyStem, toggleActiveHeavenlyStem: toggleActiveHeavenlyStem, hoverHeavenlyStem: hoverHeavenlyStem, setHoverHeavenlyStem: setHoverHeavenlyStem, toggleTaichiPoint: toggleTaichiPoint }, palace))); }), react_1.default.createElement(IzpalaceCenter_1.IzpalaceCenter, __assign({ astrolabe: astrolabe, horoscope: horoscope, horoscopeDate: horoscopeDate, horoscopeHour: horoscopeHour, setHoroscopeDate: setHoroscopeDate, setHoroscopeHour: setHoroscopeHour, centerPalaceAlign: props.centerPalaceAlign }, dynamic)))); }; exports.Iztrolabe = Iztrolabe;