@homecode/ui 4.13.0 → 4.14.0

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.
package/dist/esm/index.js CHANGED
@@ -4,6 +4,7 @@ export { ButtonGroup } from './src/components/ButtonGroup/ButtonGroup.js';
4
4
  export { Calendar } from './src/components/Calendar/Calendar.js';
5
5
  export { Checkbox } from './src/components/Checkbox/Checkbox.js';
6
6
  export { Container } from './src/components/Container/Container.js';
7
+ export { DatePicker } from './src/components/DatePicker/DatePicker.js';
7
8
  export { DateTime, formatDate } from './src/components/DateTime/DateTime.js';
8
9
  export { Draggable } from './src/components/Draggable/Draggable.js';
9
10
  export { Expand } from './src/components/Expand/Expand.js';
@@ -11,17 +11,22 @@ const isWeekend = (day) => {
11
11
  const num = day % 7;
12
12
  return num === 5 || num === 6;
13
13
  };
14
- const getDaysArray = (year, month) => {
15
- const key = `${year}-${month}`;
14
+ const isSameDay = (day1, day2) => {
15
+ return (day1.day === day2.day &&
16
+ day1.month === day2.month &&
17
+ day1.year === day2.year);
18
+ };
19
+ const getDaysArray = (year, month, startOfWeek) => {
20
+ const key = `${year}-${month}-${startOfWeek}`;
16
21
  if (CACHE[key])
17
22
  return CACHE[key];
18
23
  let monthIndex = month - 1; // JavaScript's Date months are 0-indexed.
19
24
  let date = new Date(year, monthIndex, 1);
20
25
  let result = [];
21
- let startDay = date.getDay();
26
+ let startDay = (date.getDay() - startOfWeek + 7) % 7;
22
27
  if (startDay > 0) {
23
28
  let prevMonthLastDay = new Date(year, monthIndex, 0).getDate();
24
- for (let i = 0; i < startDay - 1; i++) {
29
+ for (let i = 0; i < startDay; i++) {
25
30
  result.unshift({
26
31
  day: prevMonthLastDay - i,
27
32
  month: monthIndex,
@@ -44,7 +49,7 @@ const getDaysArray = (year, month) => {
44
49
  for (let i = nextMonthStartDay; i < 7; i++) {
45
50
  result.push({
46
51
  day: nextMonthDay,
47
- month: monthIndex + 2,
52
+ month: monthIndex + 2 > 12 ? 1 : monthIndex + 2,
48
53
  year: monthIndex === 11 ? year + 1 : year,
49
54
  currentMonth: false,
50
55
  });
@@ -55,4 +60,4 @@ const getDaysArray = (year, month) => {
55
60
  };
56
61
  const valueToDate = (day) => new Date(day.year, day.month - 1, day.day ?? 1);
57
62
 
58
- export { getDaysArray, getWeekDaysArray, isWeekend, valueToDate, weekDays };
63
+ export { getDaysArray, getWeekDaysArray, isSameDay, isWeekend, valueToDate, weekDays };
@@ -1,8 +1,8 @@
1
- import { jsx, jsxs } from 'react/jsx-runtime';
1
+ import { jsxs, jsx } from 'react/jsx-runtime';
2
2
  import { useMemo, useState, useCallback } from 'react';
3
3
  import cn from 'classnames';
4
4
  import S from './Calendar.styl.js';
5
- import { valueToDate, getWeekDaysArray, getDaysArray, isWeekend, weekDays } from './Calendar.helpers.js';
5
+ import { valueToDate, getWeekDaysArray, getDaysArray, isWeekend, weekDays, isSameDay } from './Calendar.helpers.js';
6
6
  import { Input } from '../Input/Input.js';
7
7
  import { Select } from '../Select/Select.js';
8
8
 
@@ -21,12 +21,16 @@ const MONTHS = [
21
21
  'December',
22
22
  ];
23
23
  const MIN_YEAR = 1970;
24
- function Calendar({ value, onDayClick, renderWeekDayLabel, renderMonthLabel, renderMonthesLabel, renderYearLabel, startOfWeek, weekendClassName, size, }) {
24
+ const renderDayDefault = (val, props) => {
25
+ const { day, year, month } = val;
26
+ return (jsx("div", { ...props, children: day }, `${year}-${month}-${day}`));
27
+ };
28
+ function Calendar({ value, onDayClick, renderDay, renderWeekDayLabel, renderMonthLabel, renderMonthesLabel, renderYearLabel, startOfWeek = 1, weekendClassName, size, }) {
25
29
  const date = useMemo(() => valueToDate(value), [value]);
26
30
  const [month, setMonth] = useState(date.getMonth() + 1);
27
31
  const [year, setYear] = useState(date.getFullYear());
28
32
  const weekDaysArray = useMemo(() => getWeekDaysArray(startOfWeek), [startOfWeek]);
29
- const daysArray = getDaysArray(year, month);
33
+ const daysArray = useMemo(() => getDaysArray(year, month, startOfWeek), [startOfWeek, month, year]);
30
34
  const monthOptions = useMemo(() => MONTHS.map((m, i) => ({
31
35
  id: i + 1,
32
36
  label: renderMonthLabel?.(i + 1) ?? m,
@@ -41,21 +45,18 @@ function Calendar({ value, onDayClick, renderWeekDayLabel, renderMonthLabel, ren
41
45
  if (year < MIN_YEAR)
42
46
  setYear(MIN_YEAR);
43
47
  };
44
- const renderDay = useCallback((val, weekdayIndex) => {
45
- const { day, year, month, currentMonth } = val;
46
- const isWeekend$1 = isWeekend(weekdayIndex);
47
- const classes = cn(S.day, currentMonth && S.currMonth, isWeekend$1 && weekendClassName);
48
- return (jsx("div", { className: classes, onClick: () => onDayClick?.(val), children: day }, `${year}-${month}-${day}`));
49
- }, []);
50
48
  return (jsxs("div", { className: cn(S.root, S[`size-${size}`]), children: [jsxs("div", { className: S.header, children: [jsx(Input, { className: S.year, type: "number", min: MIN_YEAR,
51
49
  // @ts-ignore
52
- size: size,
53
- // @ts-ignore
54
- label: renderYearLabel?.(year) ?? 'Year', value: year,
55
- // @ts-ignore
56
- onChange: onYearChange, onBlur: onYearBlur }), jsx(Select, { className: S.month, size: size,
50
+ size: size, label: renderYearLabel?.(year) ?? 'Year', value: year,
57
51
  // @ts-ignore
58
- label: renderMonthesLabel?.(month) ?? 'Month', options: monthOptions, value: month, onChange: val => setMonth(val) })] }), jsx("div", { className: S.weekDays, children: weekDaysArray.map((day, weekdayIndex) => (jsx("div", { className: cn(S.day, isWeekend(weekdayIndex) && weekendClassName), children: renderWeekDayLabel?.(day) ?? weekDays[day] }, day))) }), jsx("div", { className: S.days, children: daysArray.map((d, weekdayIndex) => renderDay(d, weekdayIndex)) })] }));
52
+ onChange: onYearChange, onBlur: onYearBlur }), jsx(Select, { className: S.month, size: size, label: renderMonthesLabel?.(month) ?? 'Month', options: monthOptions, value: month, onChange: val => setMonth(val) })] }), jsx("div", { className: S.weekDays, children: weekDaysArray.map((day, weekdayIndex) => (jsx("div", { className: cn(S.day, isWeekend(weekdayIndex) && weekendClassName), children: renderWeekDayLabel?.(day) ?? weekDays[day] }, `weekday-${day}`))) }), jsx("div", { className: S.days, children: daysArray.map((day, weekdayIndex) => {
53
+ const classes = cn(S.day, day.currentMonth && S.currMonth, isWeekend(weekdayIndex) && weekendClassName, isSameDay(day, value) && S.selected);
54
+ const dayProps = {
55
+ className: classes,
56
+ onClick: () => onDayClick?.(day),
57
+ };
58
+ return renderDay?.(day, dayProps) ?? renderDayDefault(day, dayProps);
59
+ }) })] }));
59
60
  }
60
61
 
61
62
  export { Calendar };
@@ -1,7 +1,7 @@
1
1
  import styleInject from '../../../node_modules/style-inject/dist/style-inject.es.js';
2
2
 
3
- var css_248z = ".Calendar_size-s__MFLPb{font-size:.5em}.Calendar_size-m__IGeoI{font-size:.75em}.Calendar_size-l__a2zct{font-size:1em}.Calendar_root__v8Jg6{display:flex;flex-direction:column}.Calendar_header__IHzS-{align-items:center;display:flex;justify-content:space-between}.Calendar_month__gi10Q{width:100%!important}.Calendar_year__UlmLM{min-width:5em}.Calendar_year__UlmLM input{min-width:0;min-width:auto}.Calendar_month__gi10Q{margin-left:var(--indent-s)}.Calendar_weekDays__Pglyw{align-items:center;color:var(--accent-color-alpha-500);display:flex;font-size:.75em;margin-top:var(--indent-s);width:100%}.Calendar_weekDays__Pglyw .Calendar_day__A3NG-{padding:1.5em 0}.Calendar_size-s__MFLPb .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:first-child{border-top-left-radius:4px}.Calendar_size-s__MFLPb .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:last-child{border-top-right-radius:4px}.Calendar_size-m__IGeoI .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:first-child{border-top-left-radius:6px}.Calendar_size-m__IGeoI .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:last-child{border-top-right-radius:6px}.Calendar_size-l__a2zct .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:first-child{border-top-left-radius:8px}.Calendar_size-l__a2zct .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:last-child{border-top-right-radius:8px}.Calendar_days__64U-y{display:flex;flex-wrap:wrap;justify-content:space-between;-webkit-user-select:none;-moz-user-select:none;user-select:none}.Calendar_days__64U-y .Calendar_day__A3NG-:not(.Calendar_currMonth__oL4nl){color:var(--accent-color-alpha-500)}.Calendar_size-s__MFLPb .Calendar_days__64U-y .Calendar_day__A3NG-:last-child{border-bottom-right-radius:4px}.Calendar_size-m__IGeoI .Calendar_days__64U-y .Calendar_day__A3NG-:last-child{border-bottom-right-radius:6px}.Calendar_size-l__a2zct .Calendar_days__64U-y .Calendar_day__A3NG-:last-child{border-bottom-right-radius:8px}.Calendar_day__A3NG-{align-items:center;display:flex;height:2em;justify-content:center;width:14.28571%}.Calendar_day__A3NG-.Calendar_currMonth__oL4nl{font-weight:700}";
4
- var S = {"size-s":"Calendar_size-s__MFLPb","size-m":"Calendar_size-m__IGeoI","size-l":"Calendar_size-l__a2zct","root":"Calendar_root__v8Jg6","header":"Calendar_header__IHzS-","month":"Calendar_month__gi10Q","year":"Calendar_year__UlmLM","weekDays":"Calendar_weekDays__Pglyw","day":"Calendar_day__A3NG-","days":"Calendar_days__64U-y","currMonth":"Calendar_currMonth__oL4nl"};
3
+ var css_248z = ".Calendar_size-s__MFLPb{font-size:.5em}.Calendar_size-m__IGeoI{font-size:.75em}.Calendar_size-l__a2zct{font-size:1em}.Calendar_root__v8Jg6{display:flex;flex-direction:column}.Calendar_header__IHzS-{align-items:center;display:flex;justify-content:space-between}.Calendar_month__gi10Q{width:100%!important}.Calendar_year__UlmLM{min-width:5em}.Calendar_year__UlmLM input{min-width:0;min-width:auto}.Calendar_month__gi10Q{margin-left:var(--indent-s)}.Calendar_weekDays__Pglyw{align-items:center;color:var(--accent-color-alpha-500);display:flex;font-size:.75em;margin-top:var(--indent-s);width:100%}.Calendar_weekDays__Pglyw .Calendar_day__A3NG-{padding:1.5em 0}.Calendar_size-s__MFLPb .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:first-child{border-top-left-radius:4px}.Calendar_size-s__MFLPb .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:last-child{border-top-right-radius:4px}.Calendar_size-m__IGeoI .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:first-child{border-top-left-radius:6px}.Calendar_size-m__IGeoI .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:last-child{border-top-right-radius:6px}.Calendar_size-l__a2zct .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:first-child{border-top-left-radius:8px}.Calendar_size-l__a2zct .Calendar_weekDays__Pglyw .Calendar_day__A3NG-:last-child{border-top-right-radius:8px}.Calendar_days__64U-y{display:flex;flex-wrap:wrap;justify-content:space-between;-webkit-user-select:none;-moz-user-select:none;user-select:none}.Calendar_days__64U-y .Calendar_day__A3NG-:not(.Calendar_currMonth__oL4nl){color:var(--accent-color-alpha-500)}.Calendar_size-s__MFLPb .Calendar_days__64U-y .Calendar_day__A3NG-:last-child{border-bottom-right-radius:4px}.Calendar_size-m__IGeoI .Calendar_days__64U-y .Calendar_day__A3NG-:last-child{border-bottom-right-radius:6px}.Calendar_size-l__a2zct .Calendar_days__64U-y .Calendar_day__A3NG-:last-child{border-bottom-right-radius:8px}.Calendar_day__A3NG-{align-items:center;display:flex;height:2em;justify-content:center;width:14.28571%}.Calendar_day__A3NG-.Calendar_selected__ffB6-{background-color:var(--active-color-alpha-100)!important}.Calendar_day__A3NG-.Calendar_currMonth__oL4nl{font-weight:700}";
4
+ var S = {"size-s":"Calendar_size-s__MFLPb","size-m":"Calendar_size-m__IGeoI","size-l":"Calendar_size-l__a2zct","root":"Calendar_root__v8Jg6","header":"Calendar_header__IHzS-","month":"Calendar_month__gi10Q","year":"Calendar_year__UlmLM","weekDays":"Calendar_weekDays__Pglyw","day":"Calendar_day__A3NG-","days":"Calendar_days__64U-y","currMonth":"Calendar_currMonth__oL4nl","selected":"Calendar_selected__ffB6-"};
5
5
  styleInject(css_248z);
6
6
 
7
7
  export { S as default };
@@ -0,0 +1,21 @@
1
+ import { jsxs, jsx } from 'react/jsx-runtime';
2
+ import { useCallback, createElement } from 'react';
3
+ import cn from 'classnames';
4
+ import { Calendar } from '../Calendar/Calendar.js';
5
+ import { Button } from '../Button/Button.js';
6
+ import S from './DatePicker.styl.js';
7
+
8
+ function DatePicker(props) {
9
+ const { value, onChange, size, calendarProps } = props;
10
+ const isRange = Array.isArray(value);
11
+ const onFirstDateChange = useCallback((val) => {
12
+ onChange(isRange ? [val, value[1]] : val);
13
+ }, [value, onChange, isRange]);
14
+ const renderDay = useCallback((val, { className, ...props }) => {
15
+ const { day, year, month } = val;
16
+ return (createElement(Button, { ...props, variant: "clear", className: cn(className, S.day), size: size, key: `${year}-${month}-${day}` }, day));
17
+ }, [size]);
18
+ return (jsxs("div", { className: S.root, children: [jsx(Calendar, { size: size, ...calendarProps, value: isRange ? value[0] : value, onDayClick: onFirstDateChange, renderDay: renderDay }), isRange && (jsx(Calendar, { size: size, ...calendarProps, value: value[1], onDayClick: val => onChange([value[0], val]), renderDay: renderDay }))] }));
19
+ }
20
+
21
+ export { DatePicker };
@@ -0,0 +1,7 @@
1
+ import styleInject from '../../../node_modules/style-inject/dist/style-inject.es.js';
2
+
3
+ var css_248z = ".DatePicker_root__hh-PP{display:flex;flex-direction:column}.DatePicker_day__kGD0i:hover{background-color:var(--accent-color-alpha-100)}";
4
+ var S = {"root":"DatePicker_root__hh-PP","day":"DatePicker_day__kGD0i"};
5
+ styleInject(css_248z);
6
+
7
+ export { S as default };
@@ -26,6 +26,7 @@ import '../../tools/queryParams.js';
26
26
  import '../Select/Select.styl.js';
27
27
  import '../Checkbox/Checkbox.styl.js';
28
28
  import '../Container/Container.styl.js';
29
+ import '../DatePicker/DatePicker.styl.js';
29
30
  import 'moment';
30
31
  import '../Draggable/Draggable.styl.js';
31
32
  import '../Expand/Expand.styl.js';
@@ -29,6 +29,7 @@ import '../../tools/queryParams.js';
29
29
  import '../Select/Select.styl.js';
30
30
  import '../Checkbox/Checkbox.styl.js';
31
31
  import '../Container/Container.styl.js';
32
+ import '../DatePicker/DatePicker.styl.js';
32
33
  import 'moment';
33
34
  import { Draggable } from '../Draggable/Draggable.js';
34
35
  import '../Expand/Expand.styl.js';
@@ -566,8 +566,9 @@ class Select extends Component {
566
566
  renderOptionsList() {
567
567
  const { size } = this.props;
568
568
  this.isTree = false;
569
+ const optionsList = this.renderOptions();
569
570
  const classes = cn(S.options, S[`size-${size}`], this.isTree && S.isTree);
570
- return (jsxs("div", { ref: this.contentRef, children: [this.renderPresets(), jsx(Scroll, { y: true, offset: { y: { before: 10, after: 10 } }, className: classes, onInnerRef: this.onScrollInnerRef, children: this.renderOptions() }, "items-scroll")] }));
571
+ return (jsxs("div", { ref: this.contentRef, children: [this.renderPresets(), jsx(Scroll, { y: true, offset: { y: { before: 10, after: 10 } }, className: classes, onInnerRef: this.onScrollInnerRef, children: optionsList }, "items-scroll")] }));
571
572
  }
572
573
  render() {
573
574
  const { className, popupProps, size, error, blur } = this.props;
@@ -1,3 +1,3 @@
1
1
  /// <reference types="react" />
2
2
  import * as T from './Calendar.types';
3
- export declare function Calendar({ value, onDayClick, renderWeekDayLabel, renderMonthLabel, renderMonthesLabel, renderYearLabel, startOfWeek, weekendClassName, size, }: T.Props): JSX.Element;
3
+ export declare function Calendar({ value, onDayClick, renderDay, renderWeekDayLabel, renderMonthLabel, renderMonthesLabel, renderYearLabel, startOfWeek, weekendClassName, size, }: T.Props): JSX.Element;
@@ -1,6 +1,8 @@
1
+ import type { Date } from 'uilib/types';
1
2
  import * as T from './Calendar.types';
2
3
  export declare const weekDays: string[];
3
4
  export declare const getWeekDaysArray: (startOfWeek?: number) => any[];
4
5
  export declare const isWeekend: (day: number) => boolean;
5
- export declare const getDaysArray: (year: any, month: any) => any;
6
- export declare const valueToDate: (day: T.Value) => Date;
6
+ export declare const isSameDay: (day1: Date, day2: Date) => boolean;
7
+ export declare const getDaysArray: (year: any, month: any, startOfWeek: any) => T.Day[];
8
+ export declare const valueToDate: (day: Date) => globalThis.Date;
@@ -1,19 +1,21 @@
1
- /// <reference types="react" />
2
- import { Size } from 'uilib/types';
3
- export type Value = {
4
- year: number;
5
- month: number;
6
- day?: number;
1
+ import { ReactNode } from 'react';
2
+ import { Size, Date } from 'uilib/types';
3
+ export type Day = Date & {
7
4
  currentMonth?: boolean;
8
5
  };
6
+ export type DayProps = {
7
+ className: string;
8
+ onClick: () => void;
9
+ };
9
10
  export type Props = {
10
- value: Value;
11
- onDayClick: (value: Value) => void;
11
+ value: Date;
12
+ onDayClick: (value: Date) => void;
12
13
  startOfWeek?: number;
13
14
  size?: Size;
14
- renderWeekDayLabel?: (day: number) => React.ReactNode;
15
- renderYearLabel?: (year: number) => React.ReactNode;
16
- renderMonthesLabel?: (month: number) => React.ReactNode;
17
- renderMonthLabel?: (month: number) => React.ReactNode;
15
+ renderDay?: (day: Day, dayProps: DayProps) => ReactNode;
16
+ renderWeekDayLabel?: (day: number) => ReactNode;
17
+ renderYearLabel?: (year: number) => string;
18
+ renderMonthesLabel?: (month: number) => string;
19
+ renderMonthLabel?: (month: number) => string;
18
20
  weekendClassName?: string;
19
21
  };
@@ -1,4 +1,3 @@
1
- export default function DatePicker({ value, onChange }: {
2
- value: any;
3
- onChange: any;
4
- }): number;
1
+ /// <reference types="react" />
2
+ import * as T from './DatePicker.types';
3
+ export declare function DatePicker(props: T.Props): JSX.Element;
@@ -0,0 +1,9 @@
1
+ import type { Size, Date } from 'uilib/types';
2
+ import type { Props as CalendarProps } from 'uilib/components/Calendar/Calendar.types';
3
+ export type Value = Date | [Date, Date];
4
+ export type Props = {
5
+ value: Value;
6
+ onChange: (value: Value) => void;
7
+ size?: Size;
8
+ calendarProps?: CalendarProps;
9
+ };
@@ -1,3 +1,2 @@
1
- /// <reference types="react" />
2
1
  declare const _default: () => JSX.Element;
3
2
  export default _default;
@@ -73,7 +73,7 @@ export declare class Select extends Component<T.Props, T.State> {
73
73
  getNewSelected(id: any): T.State['selected'];
74
74
  getValue(): any;
75
75
  getInputVal(): T.Value;
76
- getLabel(id: any): any;
76
+ getLabel(id: any): string;
77
77
  getFieldLabel(label: any): any;
78
78
  getSelectedLabel(): string;
79
79
  filterOption({ label }: {
@@ -7,7 +7,7 @@ export type Option = {
7
7
  id: Id;
8
8
  isGroupHeader?: boolean;
9
9
  parentId?: Id;
10
- label: any;
10
+ label: string;
11
11
  sortingKey?: string | number;
12
12
  children?: Option[];
13
13
  render?: (label: string) => string;
@@ -4,6 +4,7 @@ export * from './ButtonGroup/ButtonGroup';
4
4
  export * from './Calendar/Calendar';
5
5
  export * from './Checkbox/Checkbox';
6
6
  export * from './Container/Container';
7
+ export * from './DatePicker/DatePicker';
7
8
  export * from './DateTime/DateTime';
8
9
  export * from './Draggable/Draggable';
9
10
  export * from './Expand/Expand';
@@ -13,3 +13,9 @@ export type FormControl<T> = {
13
13
  name?: string;
14
14
  label?: string;
15
15
  };
16
+ export type Date = {
17
+ year: number;
18
+ month: number;
19
+ day?: number;
20
+ ISO?: string;
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homecode/ui",
3
- "version": "4.13.0",
3
+ "version": "4.14.0",
4
4
  "description": "React UI components library",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",