UNPKG

@lowdefy/blocks-antd

Version:

Lowdefy Ant Design Blocks

101 lines (97 loc) 4.45 kB
/* Copyright 2020-2026 Lowdefy, Inc Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useState } from 'react'; import { DatePicker } from 'antd'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc.js'; import weekOfYear from 'dayjs/plugin/weekOfYear.js'; import advancedFormat from 'dayjs/plugin/advancedFormat.js'; import { type } from '@lowdefy/helpers'; import { withBlockDefaults } from '@lowdefy/block-utils'; import Label from '../Label/Label.js'; import withTheme from '../withTheme.js'; import disabledDate from '../../disabledDate.js'; dayjs.extend(utc); dayjs.extend(weekOfYear); dayjs.extend(advancedFormat); const WeekSelector = ({ blockId, classNames = {}, components: { Icon, Link }, events, loading, methods, properties, required, styles = {}, validation, value })=>{ const [elementId] = useState((0 | Math.random() * 9e2) + 1e2); return /*#__PURE__*/ React.createElement(Label, { blockId: blockId, classNames: classNames, components: { Icon, Link }, events: events, properties: { title: properties.title, size: properties.size, ...properties.label }, validation: validation, required: required, styles: styles, content: { content: ()=>/*#__PURE__*/ React.createElement("div", { style: { width: '100%' } }, /*#__PURE__*/ React.createElement("div", { id: `${blockId}_${elementId}_popup` }), /*#__PURE__*/ React.createElement(DatePicker, { id: `${blockId}_input`, picker: "week", allowClear: properties.allowClear !== false, autoFocus: properties.autoFocus, variant: properties.bordered === false ? 'borderless' : properties.variant, className: classNames.element, style: { width: '100%', ...styles.element }, disabled: properties.disabled || loading, disabledDate: disabledDate(properties.disabledDates), format: properties.format ?? 'YYYY-wo', getPopupContainer: ()=>document.getElementById(`${blockId}_${elementId}_popup`), placeholder: properties.placeholder ?? 'Select Week', size: properties.size, status: validation.status, suffixIcon: /*#__PURE__*/ React.createElement(Icon, { blockId: `${blockId}_suffixIcon`, classNames: { element: classNames.suffixIcon }, events: events, properties: properties.suffixIcon ?? 'AiOutlineCalendar', styles: { element: styles.suffixIcon } }), onChange: (newVal)=>{ // Wrap with our dayjs — antd v6's internal dayjs may lack the utc plugin. const d = newVal ? dayjs(newVal) : null; const val = !d ? null : dayjs.utc(d.add(d.utcOffset(), 'minutes')).startOf('week').toDate(); methods.setValue(val); methods.triggerEvent({ name: 'onChange', event: { value: val } }); }, value: value && type.isDate(value) ? dayjs.utc(value).startOf('week') : null })) } }); }; export default withTheme('DatePicker', withBlockDefaults(WeekSelector));