UNPKG

@amaui/ui-react

Version:
262 lines (261 loc) 16.1 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const date_1 = require("@amaui/date"); const IconMaterialArrowBackIosNewW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialArrowBackIosNewW100")); const IconMaterialArrowForwardIosW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialArrowForwardIosW100")); const CalendarWeek_1 = __importDefault(require("../CalendarWeek")); const CalendarMonth_1 = __importDefault(require("../CalendarMonth")); const IconButton_1 = __importDefault(require("../IconButton")); const Line_1 = __importDefault(require("../Line")); const Tooltip_1 = __importDefault(require("../Tooltip")); const Type_1 = __importDefault(require("../Type")); const Select_1 = __importDefault(require("../Select")); const Button_1 = __importDefault(require("../Button")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { padding: '16px', color: theme.methods.palette.color.value('primary', 10), background: theme.palette.background.default.primary, '& .amaui-Label-text': { whiteSpace: 'nowrap' } }, main: { paddingTop: '4px', overflow: 'auto hidden' }, aside: { width: 'auto', maxWidth: '100%' }, month: { position: 'relative', width: 'auto', minWidth: '100%' }, monthDay: { width: '32px', height: '32px', marginTop: '4px', borderRadius: '50%' }, today: { background: theme.palette.color.primary[40], color: '#fff' }, contentItemsMonth: { position: 'relative', padding: '0px 4px 8px', height: '100px', overflow: 'hidden auto' }, calendarMonth: { '&.amaui-CalendarMonth-root': { width: '100%', height: '100%', minHeight: '0', flex: '0 0 auto', minWidth: 'unset', overflow: 'unset', transition: theme.methods.transitions.make('opacity') }, '& .amaui-CalendarMonth-weeks': { position: 'relative', top: '0', width: 'calc(100% - 2px)', height: 'auto', marginLeft: '1px', gap: '0' }, '& .amaui-CalendarMonth-week': { flex: '1 1 auto' }, '& .amaui-CalendarMonth-day': { height: 'unset', minWidth: '170px', minHeight: '100px', marginTop: '-1px', marginLeft: '-1px', border: `1px solid ${theme.palette.light ? '#dadada' : '#575757'}` }, '& .amaui-CalendarMonth-day-name': { minWidth: '170px' }, '& .amaui-CalendarMonth-day-out': { zIndex: '-1px' } }, dayWrapper: { height: '100%' }, overflowX: { padding: '2px 0', overflow: 'auto hidden' } }), { name: 'amaui-CalendarViews' }); const CalendarViews = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiCalendarViews) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const CalendarMonth = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.CalendarMonth) || CalendarMonth_1.default; }, [theme]); const IconButton = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.IconButton) || IconButton_1.default; }, [theme]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const Tooltip = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Tooltip) || Tooltip_1.default; }, [theme]); const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]); const Button = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Button) || Button_1.default; }, [theme]); const Select = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Select) || Select_1.default; }, [theme]); const CalendarWeek = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.CalendarWeek) || CalendarWeek_1.default; }, [theme]); const { viewDefault, dateDefault, times: timesProps, events, views: viewsProps = ['month', 'week', 'day'], render, onTimeClick, onChangeView: onChangeViewProps, onChangeDate: onChangeDateProps, startHeader, endHeader, startLeft, endLeft, startRight, endRight, noViews, IconPrevious = IconMaterialArrowBackIosNewW100_1.default, IconNext = IconMaterialArrowForwardIosW100_1.default, WeekProps, DayProps, CalendarMonthProps, className } = props, other = __rest(props, ["viewDefault", "dateDefault", "times", "events", "views", "render", "onTimeClick", "onChangeView", "onChangeDate", "startHeader", "endHeader", "startLeft", "endLeft", "startRight", "endRight", "noViews", "IconPrevious", "IconNext", "WeekProps", "DayProps", "CalendarMonthProps", "className"]); const { classes } = useStyle(props); const [now, setNow] = react_1.default.useState(new date_1.AmauiDate()); const [date, setDate] = react_1.default.useState(dateDefault || new date_1.AmauiDate()); const [view, setView] = react_1.default.useState(viewDefault || 'month'); const refs = { interval: react_1.default.useRef(undefined) }; react_1.default.useEffect(() => { // 1 minute refs.interval.current = setInterval(() => { setNow(new date_1.AmauiDate()); }, 60 * 1e3); return () => { clearInterval(refs.interval.current); }; }, []); const times = react_1.default.useMemo(() => { if (events) { return [ { dates: { active: true, values: ((0, utils_1.is)('array', events) ? events : [events]).filter(Boolean) } } ]; } return ((0, utils_1.is)('array', timesProps) ? timesProps : [timesProps]).filter(Boolean); }, [events, timesProps]); const viewOptions = react_1.default.useMemo(() => { return viewsProps === null || viewsProps === void 0 ? void 0 : viewsProps.map(item => ({ name: (0, utils_1.capitalize)(item), value: item })); }, [viewsProps]); const formattedDate = react_1.default.useMemo(() => { if (view === 'day') return (0, date_1.format)(date, `MMMM DD, YYYY`); if (view === 'week') return `${(0, date_1.format)((0, date_1.startOf)(date, 'week'), `MMM DD, YYYY`)} – ${(0, date_1.format)((0, date_1.endOf)(date, 'week'), `MMM DD, YYYY`)}`; if (view === 'month') return (0, date_1.format)(date, `MMMM YYYY`); }, [view, date]); const onChangeView = react_1.default.useCallback((valueNew) => { setView(valueNew); if ((0, utils_1.is)('function', onChangeViewProps)) onChangeViewProps(valueNew); }, [onChangeViewProps]); const onToday = react_1.default.useCallback(() => { const valueNew = new date_1.AmauiDate(); setDate(valueNew); if ((0, utils_1.is)('function', onChangeDateProps)) onChangeDateProps(valueNew); }, [onChangeDateProps]); const onPrevious = react_1.default.useCallback(() => { let valueNew = new date_1.AmauiDate(); setDate(previous => { valueNew = (0, date_1.remove)(1, view, previous); return valueNew; }); if ((0, utils_1.is)('function', onChangeDateProps)) onChangeDateProps(valueNew); }, [view, onChangeDateProps]); const onNext = react_1.default.useCallback(() => { let valueNew = new date_1.AmauiDate(); setDate(previous => { valueNew = (0, date_1.add)(1, view, previous); return valueNew; }); if ((0, utils_1.is)('function', onChangeDateProps)) onChangeDateProps(valueNew); }, [view, onChangeDateProps]); const renderDay = react_1.default.useCallback((valueCalendarMonth, propsDay, day, outside) => { return ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 0.5, direction: 'column', align: 'center', flex: true, fullWidth: true, className: classes.dayWrapper }, { children: [(0, jsx_runtime_1.jsx)(Line, Object.assign({ direction: 'row', justify: 'center', align: 'center', fullWidth: true, className: (0, style_react_1.classNames)([ classes.monthDay, day.today && classes.today ]) }, { children: (0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b2' }, { children: (0, date_1.format)(valueCalendarMonth, 'D') })) })), (0, jsx_runtime_1.jsx)(Line, Object.assign({ gap: 0.5, flex: true, fullWidth: true, className: classes.contentItemsMonth }, { children: (0, utils_1.is)('function', render) && render(valueCalendarMonth, view) }))] }))); }, [render, view]); const renderDayName = react_1.default.useCallback((order) => { const values = { 1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat', 7: 'Sun' }; return values[order]; }, []); const ui = { month: (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(Line, Object.assign({ gap: 0, className: classes.month }, { children: (0, jsx_runtime_1.jsx)(CalendarMonth, Object.assign({ value: now, calendar: date, renderDay: renderDay, renderDayName: renderDayName, onTimeClick: onTimeClick, size: 'large', dayNamesFull: true, noTransition: true, outside: false, DayNameProps: { version: 'l1', weight: 200 } }, CalendarMonthProps, { className: (0, style_react_1.classNames)([ CalendarMonthProps === null || CalendarMonthProps === void 0 ? void 0 : CalendarMonthProps.className, classes.calendarMonth ]) })) })) }), week: ((0, jsx_runtime_1.jsx)(CalendarWeek, Object.assign({ date: date, times: times, events: events, onTimeClick: onTimeClick }, WeekProps))), day: ((0, jsx_runtime_1.jsx)(CalendarWeek, Object.assign({ date: date, times: times, events: events, onTimeClick: onTimeClick, day: true }, WeekProps))) }; const iconProps = { size: 'regular' }; const iconButtonProps = { size: 'regular' }; return ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ ref: ref, flex: true, fullWidth: true, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('CalendarViews', theme) && [ 'amaui-CalendarViews-root' ], className, classes.root ]) }, other, { children: [(0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, fullWidth: true, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('CalendarViews', theme) && [ 'amaui-CalendarViews-header' ] ]) }, { children: [startHeader, (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 2, direction: 'row', wrap: 'wrap', justify: 'space-between', align: 'center', fullWidth: true }, { children: [(0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1.5, direction: 'row', wrap: 'wrap', align: 'center', className: classes.aside }, { children: [startLeft, (0, jsx_runtime_1.jsx)(Button, Object.assign({ color: 'inherit', version: 'outlined', size: 'small', onClick: onToday, selected: now.days === date.days }, { children: "Today" })), (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 0, direction: 'row', align: 'center' }, { children: [(0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ name: `Previous ${view}` }, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onPrevious }, iconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconPrevious, Object.assign({}, iconProps)) })) })), (0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ name: `Next ${view}` }, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onNext }, iconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconNext, Object.assign({}, iconProps)) })) }))] })), (0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'h2', weight: 500, whiteSpace: 'nowrap' }, { children: formattedDate })), endLeft] })), (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1.5, direction: 'row', align: 'center', flexNo: true, className: (0, style_react_1.classNames)([ classes.aside, classes.overflowX ]) }, { children: [startRight, !noViews && ((0, jsx_runtime_1.jsx)(Select, { name: 'View', value: view, onChange: onChangeView, options: viewOptions, size: 'small', MenuProps: { portal: true, size: 'regular' }, WrapperProps: { style: { width: 170, minWidth: 'unset' } }, style: { width: 170, minWidth: 'unset' } })), endRight] }))] })), endHeader] })), (0, jsx_runtime_1.jsx)(Line, Object.assign({ flex: true, fullWidth: true, className: classes.main }, { children: ui[view] }))] }))); }); CalendarViews.displayName = 'amaui-CalendarViews'; exports.default = CalendarViews;