@canlooks/can-ui 0.0.184 → 0.0.185

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -94,8 +94,7 @@ exports.Autocomplete = (0, react_1.memo)(({ children, loading, options, loadOpti
94
94
  css: popper_style_1.popperStyle,
95
95
  open: open.current,
96
96
  popperRef,
97
- onOpenChange: setOpen,
98
- onPointerDown: e => e.preventDefault()
97
+ onOpenChange: setOpen
99
98
  }), children: renderInput
100
99
  ? renderInput(inputProps)
101
100
  : (0, jsx_runtime_1.jsx)(input_1.Input, { ...inputProps }) }));
@@ -13,7 +13,21 @@ const panelMonth_1 = require("./panelMonth");
13
13
  const Calendar = ({ viewLevel = 'date', showToday = true, dateButtonProps, todayButtonText = '回今天', todayButtonProps, size = 'medium', defaultValue = null, min, max, disabledDates, value, onChange, _defaultNull, ...props }) => {
14
14
  const [innerValue, setInnerValue] = (0, utils_1.useControlled)(defaultValue, value, onChange);
15
15
  const [viewType, setViewType] = (0, utils_1.useDerivedState)(viewLevel);
16
- const [innerD, setInnerD] = (0, utils_1.useDerivedState)(() => innerValue.current ?? (0, dayjs_1.default)().startOf('date'), [innerValue.current]);
16
+ const [innerD, setInnerD] = (0, utils_1.useDerivedState)(() => {
17
+ if (innerValue.current) {
18
+ return innerValue.current;
19
+ }
20
+ const today = (0, dayjs_1.default)().startOf('date');
21
+ const _min = min?.add(1, 'day');
22
+ if (_min?.isAfter(today, 'month')) {
23
+ return _min;
24
+ }
25
+ const _max = max?.subtract(1, 'day');
26
+ if (_max?.isBefore(today, 'month')) {
27
+ return _max;
28
+ }
29
+ return today;
30
+ }, [innerValue.current, min, max]);
17
31
  const commonProps = {
18
32
  value: innerValue.current,
19
33
  setValue: setInnerValue,
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ClickAway = ClickAway;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("../../utils");
6
- function ClickAway({ ref, container = self, eventType = 'click', onClickAway, disabled, children, targets, ...props }) {
6
+ function ClickAway({ ref, container = self, eventType = 'pointerdown', onClickAway, disabled, children, targets, ...props }) {
7
7
  const syncOnClickAway = (0, utils_1.useSync)(onClickAway);
8
8
  const childRef = (0, react_1.useRef)(null);
9
9
  const targetsArr = (0, react_1.useRef)([]);
@@ -2,11 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DateTimePicker = void 0;
4
4
  exports.useDateTimePickerContext = useDateTimePickerContext;
5
- const tslib_1 = require("tslib");
6
5
  const jsx_runtime_1 = require("@emotion/react/jsx-runtime");
7
6
  const react_1 = require("react");
8
7
  const dateTimePicker_style_1 = require("./dateTimePicker.style");
9
- const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
10
8
  const inputBase_1 = require("../inputBase");
11
9
  const utils_1 = require("../../utils");
12
10
  const popper_1 = require("../popper");
@@ -21,9 +19,9 @@ function useDateTimePickerContext() {
21
19
  }
22
20
  exports.DateTimePicker = (0, react_1.memo)(({ inputProps, popperProps, min, max, disabledDates, disabledHours, disabledMinutes, disabledSeconds, format = 'YYYY-MM-DD', defaultValue = null, value, onChange, defaultOpen = false, open, onOpenChange, autoClose = !/[Hms]/.test(format), ...props }) => {
23
21
  const [innerOpen, setInnerOpen] = (0, utils_1.useControlled)(defaultOpen, open, onOpenChange);
24
- const [dateValue, _setDateValue] = (0, utils_1.useControlled)(defaultValue, value, onChange);
25
- const setDateValue = (date) => {
26
- _setDateValue(date);
22
+ const [innerValue, _setInnerValue] = (0, utils_1.useControlled)(defaultValue, value, onChange);
23
+ const setInnerValue = (date) => {
24
+ _setInnerValue(date);
27
25
  autoClose && setInnerOpen(false);
28
26
  };
29
27
  const contextValue = (0, react_1.useMemo)(() => ({
@@ -33,8 +31,6 @@ exports.DateTimePicker = (0, react_1.memo)(({ inputProps, popperProps, min, max,
33
31
  ]);
34
32
  const showCalendar = /[YMD]/.test(format);
35
33
  const showTimer = /[Hms]/.test(format);
36
- // 若外部没有值,内部需要“现在”作为默认值
37
- const innerValue = dateValue.current || (0, dayjs_1.default)();
38
34
  return ((0, jsx_runtime_1.jsx)(popper_1.Popper, { ...(0, utils_1.mergeComponentProps)({
39
35
  css: dateTimePicker_style_1.datePickerPopperStyle,
40
36
  open: innerOpen.current,
@@ -44,21 +40,20 @@ exports.DateTimePicker = (0, react_1.memo)(({ inputProps, popperProps, min, max,
44
40
  content: ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [showCalendar &&
45
41
  (0, jsx_runtime_1.jsx)(calendar_1.Calendar, { viewLevel: format.includes('D') ? 'date'
46
42
  : format.includes('M') ? 'month'
47
- : 'year', _defaultNull: !dateValue.current, value: innerValue, onChange: setDateValue, min: min, max: max, disabledDates: disabledDates, todayButtonText: showTimer ? '现在' : void 0 }), showTimer &&
48
- (0, jsx_runtime_1.jsx)(DateTimePickerContext, { value: contextValue, children: (0, jsx_runtime_1.jsx)(timer_1.Timer, { showHours: format.includes('H'), showMinutes: format.includes('m'), showSeconds: format.includes('s'), value: dateValue.current, onChange: setDateValue }) })] }))
43
+ : 'year', _defaultNull: !innerValue.current, value: innerValue.current, onChange: setInnerValue, min: min, max: max, disabledDates: disabledDates, todayButtonText: showTimer ? '现在' : void 0 }), showTimer &&
44
+ (0, jsx_runtime_1.jsx)(DateTimePickerContext, { value: contextValue, children: (0, jsx_runtime_1.jsx)(timer_1.Timer, { showHours: format.includes('H'), showMinutes: format.includes('m'), showSeconds: format.includes('s'), value: innerValue.current, onChange: setInnerValue }) })] }))
49
45
  }, popperProps, {
50
- onOpenChange: setInnerOpen,
51
- onPointerDown: e => e.preventDefault()
46
+ onOpenChange: setInnerOpen
52
47
  }), children: (0, jsx_runtime_1.jsx)(inputBase_1.InputBase, { ...(0, utils_1.mergeComponentProps)(props, {
53
48
  css: dateTimePicker_style_1.style,
54
49
  className: dateTimePicker_style_1.classes.root,
55
- value: dateValue.current,
50
+ value: innerValue.current,
56
51
  onClear() {
57
- setDateValue(null);
52
+ setInnerValue(null);
58
53
  }
59
- }), "data-focused": innerOpen.current, children: inputBaseProps => (0, jsx_runtime_1.jsxs)("div", { className: dateTimePicker_style_1.classes.container, children: [!dateValue.current
54
+ }), "data-focused": innerOpen.current, children: inputBaseProps => (0, jsx_runtime_1.jsxs)("div", { className: dateTimePicker_style_1.classes.container, children: [!innerValue.current
60
55
  ? (0, jsx_runtime_1.jsx)("div", { className: dateTimePicker_style_1.classes.placeholder, children: props.placeholder ?? '请选择' })
61
- : (0, jsx_runtime_1.jsx)("div", { className: dateTimePicker_style_1.classes.backfill, children: dateValue.current.format(format) }), (0, jsx_runtime_1.jsx)("input", { size: 1, ...(0, utils_1.mergeComponentProps)(inputBaseProps, inputProps), "data-hidden": "true" }), (0, jsx_runtime_1.jsx)("div", { className: dateTimePicker_style_1.classes.dateTimeIcon, children: showTimer && !showCalendar
56
+ : (0, jsx_runtime_1.jsx)("div", { className: dateTimePicker_style_1.classes.backfill, children: innerValue.current.format(format) }), (0, jsx_runtime_1.jsx)("input", { size: 1, ...(0, utils_1.mergeComponentProps)(inputBaseProps, inputProps), "data-hidden": "true" }), (0, jsx_runtime_1.jsx)("div", { className: dateTimePicker_style_1.classes.dateTimeIcon, children: showTimer && !showCalendar
62
57
  ? (0, jsx_runtime_1.jsx)(icon_1.Icon, { icon: faClock_1.faClock })
63
58
  : (0, jsx_runtime_1.jsx)(icon_1.Icon, { icon: faCalendar_1.faCalendar }) })] }) }) }));
64
59
  });
@@ -23,11 +23,10 @@ variant, size, color, disabled, readOnly, autoFocus, ...props }) => {
23
23
  autoFocus,
24
24
  autoClose: false,
25
25
  placeholder: showTimer ? '开始时间' : '开始日期',
26
- max: innerValue.current?.[1] || void 0,
27
26
  open: innerOpen.current === 'start'
28
27
  }
29
28
  : {
30
- autoClose,
29
+ autoClose: false,
31
30
  placeholder: showTimer ? '结束时间' : '结束日期',
32
31
  min: innerValue.current?.[0] || void 0,
33
32
  open: innerOpen.current === 'end'
@@ -37,12 +36,22 @@ variant, size, color, disabled, readOnly, autoFocus, ...props }) => {
37
36
  ? {
38
37
  onOpenChange: open => setInnerOpen(open ? 'start' : 'closed'),
39
38
  value: innerValue.current?.[0],
40
- onChange: value => {
41
- setInnerValue(o => [value, o?.[1] || null]);
42
- if (value && !showTimer) {
39
+ onChange: start => {
40
+ setInnerValue(o => {
41
+ const end = o?.[1]
42
+ ? start?.isAfter(o[1], 'date') ? null : o[1]
43
+ : null;
44
+ return [start, end];
45
+ });
46
+ if (start && !showTimer) {
43
47
  requestAnimationFrame(() => {
44
- endPickerInputRef.current.focus();
45
- innerOpen.current === 'start' && setInnerOpen('end');
48
+ if (!innerValue.current?.[1]) {
49
+ endPickerInputRef.current.focus();
50
+ innerOpen.current === 'start' && setInnerOpen('end');
51
+ }
52
+ else {
53
+ setInnerOpen('closed');
54
+ }
46
55
  });
47
56
  }
48
57
  }
@@ -559,5 +559,8 @@ function Popper({ ref, popperRef, anchorElement, container, effectContainer, con
559
559
  transform: 'scale(1)'
560
560
  },
561
561
  ...props.style
562
- }, "data-open": innerOpen.current, "data-variant": variant, "data-place-a": placeA.current, "data-place-b": placeB.current, "data-animation": animation, onTransitionEnd: onTransitionEnd, children: (0, jsx_runtime_1.jsx)(popperContext_1.PopperContext, { value: contextValue, children: content }) }) }) }), containerEl.current)] }));
562
+ }, "data-open": innerOpen.current, "data-variant": variant, "data-place-a": placeA.current, "data-place-b": placeB.current, "data-animation": animation, onPointerDown: e => {
563
+ props.onPointerDown?.(e);
564
+ e.preventDefault();
565
+ }, onTransitionEnd: onTransitionEnd, children: (0, jsx_runtime_1.jsx)(popperContext_1.PopperContext, { value: contextValue, children: content }) }) }) }), containerEl.current)] }));
563
566
  }
@@ -84,8 +84,7 @@ multiple = false, renderBackfill, _internalProps: { labelKey, optionsMap, innerV
84
84
  }, popperProps, {
85
85
  popperRef,
86
86
  onOpenChange: openChangeHandler,
87
- onOpenChangeEnd: openChangeEnd,
88
- onPointerDown: e => e.preventDefault()
87
+ onOpenChangeEnd: openChangeEnd
89
88
  }), children: (0, jsx_runtime_1.jsx)(inputBase_1.InputBase, { clearable: multiple, ...(0, utils_1.mergeComponentProps)(props, {
90
89
  css: selectBase_style_1.style,
91
90
  className: selectBase_style_1.classes.root,
@@ -91,8 +91,7 @@ export const Autocomplete = memo(({ children, loading, options, loadOptions, pri
91
91
  css: popperStyle,
92
92
  open: open.current,
93
93
  popperRef,
94
- onOpenChange: setOpen,
95
- onPointerDown: e => e.preventDefault()
94
+ onOpenChange: setOpen
96
95
  }), children: renderInput
97
96
  ? renderInput(inputProps)
98
97
  : _jsx(Input, { ...inputProps }) }));
@@ -9,7 +9,21 @@ import { PanelMonth } from './panelMonth.js';
9
9
  export const Calendar = ({ viewLevel = 'date', showToday = true, dateButtonProps, todayButtonText = '回今天', todayButtonProps, size = 'medium', defaultValue = null, min, max, disabledDates, value, onChange, _defaultNull, ...props }) => {
10
10
  const [innerValue, setInnerValue] = useControlled(defaultValue, value, onChange);
11
11
  const [viewType, setViewType] = useDerivedState(viewLevel);
12
- const [innerD, setInnerD] = useDerivedState(() => innerValue.current ?? dayjs().startOf('date'), [innerValue.current]);
12
+ const [innerD, setInnerD] = useDerivedState(() => {
13
+ if (innerValue.current) {
14
+ return innerValue.current;
15
+ }
16
+ const today = dayjs().startOf('date');
17
+ const _min = min?.add(1, 'day');
18
+ if (_min?.isAfter(today, 'month')) {
19
+ return _min;
20
+ }
21
+ const _max = max?.subtract(1, 'day');
22
+ if (_max?.isBefore(today, 'month')) {
23
+ return _max;
24
+ }
25
+ return today;
26
+ }, [innerValue.current, min, max]);
13
27
  const commonProps = {
14
28
  value: innerValue.current,
15
29
  setValue: setInnerValue,
@@ -1,6 +1,6 @@
1
1
  import { cloneElement, isValidElement, useEffect, useRef } from 'react';
2
2
  import { cloneRef, isChildOf, toArray, useSync } from '../../utils/index.js';
3
- export function ClickAway({ ref, container = self, eventType = 'click', onClickAway, disabled, children, targets, ...props }) {
3
+ export function ClickAway({ ref, container = self, eventType = 'pointerdown', onClickAway, disabled, children, targets, ...props }) {
4
4
  const syncOnClickAway = useSync(onClickAway);
5
5
  const childRef = useRef(null);
6
6
  const targetsArr = useRef([]);
@@ -1,7 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "@emotion/react/jsx-runtime";
2
2
  import { createContext, memo, useContext, useMemo } from 'react';
3
3
  import { classes, datePickerPopperStyle, style } from './dateTimePicker.style.js';
4
- import dayjs from 'dayjs';
5
4
  import { InputBase } from '../inputBase/index.js';
6
5
  import { mergeComponentProps, useControlled } from '../../utils/index.js';
7
6
  import { Popper } from '../popper/index.js';
@@ -16,9 +15,9 @@ export function useDateTimePickerContext() {
16
15
  }
17
16
  export const DateTimePicker = memo(({ inputProps, popperProps, min, max, disabledDates, disabledHours, disabledMinutes, disabledSeconds, format = 'YYYY-MM-DD', defaultValue = null, value, onChange, defaultOpen = false, open, onOpenChange, autoClose = !/[Hms]/.test(format), ...props }) => {
18
17
  const [innerOpen, setInnerOpen] = useControlled(defaultOpen, open, onOpenChange);
19
- const [dateValue, _setDateValue] = useControlled(defaultValue, value, onChange);
20
- const setDateValue = (date) => {
21
- _setDateValue(date);
18
+ const [innerValue, _setInnerValue] = useControlled(defaultValue, value, onChange);
19
+ const setInnerValue = (date) => {
20
+ _setInnerValue(date);
22
21
  autoClose && setInnerOpen(false);
23
22
  };
24
23
  const contextValue = useMemo(() => ({
@@ -28,8 +27,6 @@ export const DateTimePicker = memo(({ inputProps, popperProps, min, max, disable
28
27
  ]);
29
28
  const showCalendar = /[YMD]/.test(format);
30
29
  const showTimer = /[Hms]/.test(format);
31
- // 若外部没有值,内部需要“现在”作为默认值
32
- const innerValue = dateValue.current || dayjs();
33
30
  return (_jsx(Popper, { ...mergeComponentProps({
34
31
  css: datePickerPopperStyle,
35
32
  open: innerOpen.current,
@@ -39,21 +36,20 @@ export const DateTimePicker = memo(({ inputProps, popperProps, min, max, disable
39
36
  content: (_jsxs(_Fragment, { children: [showCalendar &&
40
37
  _jsx(Calendar, { viewLevel: format.includes('D') ? 'date'
41
38
  : format.includes('M') ? 'month'
42
- : 'year', _defaultNull: !dateValue.current, value: innerValue, onChange: setDateValue, min: min, max: max, disabledDates: disabledDates, todayButtonText: showTimer ? '现在' : void 0 }), showTimer &&
43
- _jsx(DateTimePickerContext, { value: contextValue, children: _jsx(Timer, { showHours: format.includes('H'), showMinutes: format.includes('m'), showSeconds: format.includes('s'), value: dateValue.current, onChange: setDateValue }) })] }))
39
+ : 'year', _defaultNull: !innerValue.current, value: innerValue.current, onChange: setInnerValue, min: min, max: max, disabledDates: disabledDates, todayButtonText: showTimer ? '现在' : void 0 }), showTimer &&
40
+ _jsx(DateTimePickerContext, { value: contextValue, children: _jsx(Timer, { showHours: format.includes('H'), showMinutes: format.includes('m'), showSeconds: format.includes('s'), value: innerValue.current, onChange: setInnerValue }) })] }))
44
41
  }, popperProps, {
45
- onOpenChange: setInnerOpen,
46
- onPointerDown: e => e.preventDefault()
42
+ onOpenChange: setInnerOpen
47
43
  }), children: _jsx(InputBase, { ...mergeComponentProps(props, {
48
44
  css: style,
49
45
  className: classes.root,
50
- value: dateValue.current,
46
+ value: innerValue.current,
51
47
  onClear() {
52
- setDateValue(null);
48
+ setInnerValue(null);
53
49
  }
54
- }), "data-focused": innerOpen.current, children: inputBaseProps => _jsxs("div", { className: classes.container, children: [!dateValue.current
50
+ }), "data-focused": innerOpen.current, children: inputBaseProps => _jsxs("div", { className: classes.container, children: [!innerValue.current
55
51
  ? _jsx("div", { className: classes.placeholder, children: props.placeholder ?? '请选择' })
56
- : _jsx("div", { className: classes.backfill, children: dateValue.current.format(format) }), _jsx("input", { size: 1, ...mergeComponentProps(inputBaseProps, inputProps), "data-hidden": "true" }), _jsx("div", { className: classes.dateTimeIcon, children: showTimer && !showCalendar
52
+ : _jsx("div", { className: classes.backfill, children: innerValue.current.format(format) }), _jsx("input", { size: 1, ...mergeComponentProps(inputBaseProps, inputProps), "data-hidden": "true" }), _jsx("div", { className: classes.dateTimeIcon, children: showTimer && !showCalendar
57
53
  ? _jsx(Icon, { icon: faClock })
58
54
  : _jsx(Icon, { icon: faCalendar }) })] }) }) }));
59
55
  });
@@ -20,11 +20,10 @@ variant, size, color, disabled, readOnly, autoFocus, ...props }) => {
20
20
  autoFocus,
21
21
  autoClose: false,
22
22
  placeholder: showTimer ? '开始时间' : '开始日期',
23
- max: innerValue.current?.[1] || void 0,
24
23
  open: innerOpen.current === 'start'
25
24
  }
26
25
  : {
27
- autoClose,
26
+ autoClose: false,
28
27
  placeholder: showTimer ? '结束时间' : '结束日期',
29
28
  min: innerValue.current?.[0] || void 0,
30
29
  open: innerOpen.current === 'end'
@@ -34,12 +33,22 @@ variant, size, color, disabled, readOnly, autoFocus, ...props }) => {
34
33
  ? {
35
34
  onOpenChange: open => setInnerOpen(open ? 'start' : 'closed'),
36
35
  value: innerValue.current?.[0],
37
- onChange: value => {
38
- setInnerValue(o => [value, o?.[1] || null]);
39
- if (value && !showTimer) {
36
+ onChange: start => {
37
+ setInnerValue(o => {
38
+ const end = o?.[1]
39
+ ? start?.isAfter(o[1], 'date') ? null : o[1]
40
+ : null;
41
+ return [start, end];
42
+ });
43
+ if (start && !showTimer) {
40
44
  requestAnimationFrame(() => {
41
- endPickerInputRef.current.focus();
42
- innerOpen.current === 'start' && setInnerOpen('end');
45
+ if (!innerValue.current?.[1]) {
46
+ endPickerInputRef.current.focus();
47
+ innerOpen.current === 'start' && setInnerOpen('end');
48
+ }
49
+ else {
50
+ setInnerOpen('closed');
51
+ }
43
52
  });
44
53
  }
45
54
  }
@@ -556,5 +556,8 @@ export function Popper({ ref, popperRef, anchorElement, container, effectContain
556
556
  transform: 'scale(1)'
557
557
  },
558
558
  ...props.style
559
- }, "data-open": innerOpen.current, "data-variant": variant, "data-place-a": placeA.current, "data-place-b": placeB.current, "data-animation": animation, onTransitionEnd: onTransitionEnd, children: _jsx(PopperContext, { value: contextValue, children: content }) }) }) }), containerEl.current)] }));
559
+ }, "data-open": innerOpen.current, "data-variant": variant, "data-place-a": placeA.current, "data-place-b": placeB.current, "data-animation": animation, onPointerDown: e => {
560
+ props.onPointerDown?.(e);
561
+ e.preventDefault();
562
+ }, onTransitionEnd: onTransitionEnd, children: _jsx(PopperContext, { value: contextValue, children: content }) }) }) }), containerEl.current)] }));
560
563
  }
@@ -81,8 +81,7 @@ multiple = false, renderBackfill, _internalProps: { labelKey, optionsMap, innerV
81
81
  }, popperProps, {
82
82
  popperRef,
83
83
  onOpenChange: openChangeHandler,
84
- onOpenChangeEnd: openChangeEnd,
85
- onPointerDown: e => e.preventDefault()
84
+ onOpenChangeEnd: openChangeEnd
86
85
  }), children: _jsx(InputBase, { clearable: multiple, ...mergeComponentProps(props, {
87
86
  css: style,
88
87
  className: classes.root,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canlooks/can-ui",
3
- "version": "0.0.184",
3
+ "version": "0.0.185",
4
4
  "author": "C.CanLiang <canlooks@gmail.com>",
5
5
  "description": "My ui framework",
6
6
  "license": "MIT",