@sqrzro/ui 4.0.0-alpha.57 → 4.0.0-alpha.59

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.
@@ -2,12 +2,12 @@
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { filterNull } from '@sqrzro/utility';
4
4
  import Button from '../../../components/buttons/Button';
5
+ import usePopover from '../../../hooks/usePopover';
5
6
  import { useClassNames } from '../../../styles/context';
6
7
  import tw from '../../../styles/classnames/utility/tw';
7
8
  import Action from '../../utility/Action';
8
9
  import Popover from '../../utility/Popover';
9
10
  import MenuItem from '../MenuItem';
10
- import usePopover from '../../../hooks/usePopover';
11
11
  function Menu({ actions, align, classNameProps, classNames, data, isDisabled, label, vAlign, }) {
12
12
  const componentClassNames = useClassNames('menu', { props: classNameProps }, classNames);
13
13
  const { controlProps, ref, targetProps } = usePopover();
@@ -0,0 +1,11 @@
1
+ import { InputProps } from '../../interfaces';
2
+ export interface BaseCalendarInputComponentProps {
3
+ isOptional?: boolean;
4
+ isPanelOnly?: boolean;
5
+ }
6
+ interface BaseCalendarInputProps extends InputProps<Date | [Date, Date] | null>, BaseCalendarInputComponentProps {
7
+ children: React.ReactElement;
8
+ onClear?: () => void;
9
+ }
10
+ declare function BaseCalendarInput({ children, hasError, isDisabled, isOptional, isPanelOnly, name, onClear, value, }: BaseCalendarInputProps): React.ReactElement;
11
+ export default BaseCalendarInput;
@@ -0,0 +1,22 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useEffect, useState } from 'react';
4
+ import Popover from '../../../components/utility/Popover';
5
+ import usePopover from '../../../hooks/usePopover';
6
+ import formatDateValue from '../../utility/format-date-value';
7
+ import StaticTextInput from '../StaticTextInput';
8
+ function BaseCalendarInput({ children, hasError, isDisabled, isOptional, isPanelOnly, name, onClear, value, }) {
9
+ const [oldValue, setOldValue] = useState(value);
10
+ const { controlProps, ref, targetProps } = usePopover();
11
+ if (isPanelOnly) {
12
+ return children;
13
+ }
14
+ useEffect(() => {
15
+ if ((value && !oldValue) || (value && oldValue)) {
16
+ controlProps.onClick();
17
+ }
18
+ setOldValue(value);
19
+ }, [value]);
20
+ return (_jsxs("div", { ref: ref, className: "relative", children: [_jsx(StaticTextInput, { hasError: hasError, isDisabled: isDisabled, label: formatDateValue(value), name: name, isOptional: isOptional, onClear: onClear, onClick: controlProps.onClick, value: value?.toString() }), _jsx(Popover, { ...targetProps, align: "center", children: children })] }));
21
+ }
22
+ export default BaseCalendarInput;
@@ -0,0 +1,6 @@
1
+ import { InputProps } from '../../interfaces';
2
+ import { BaseCalendarInputComponentProps } from '../BaseCalendarInput';
3
+ export type CalendarInputComponentProps = BaseCalendarInputComponentProps;
4
+ export type CalendarInputProps = InputProps<Date | null> & CalendarInputComponentProps;
5
+ declare function CalendarInput({ name, onChange, value, ...baseProps }: CalendarInputProps): React.ReactElement;
6
+ export default CalendarInput;
@@ -0,0 +1,14 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import Calendar from '../../../components/utility/Calendar';
4
+ import BaseCalendarInput from '../BaseCalendarInput';
5
+ function CalendarInput({ name, onChange, value, ...baseProps }) {
6
+ function handleChange(eventValue) {
7
+ onChange?.({ target: { name, value: eventValue } });
8
+ }
9
+ function handleClear() {
10
+ onChange?.({ target: { name, value: null } });
11
+ }
12
+ return (_jsx(BaseCalendarInput, { ...baseProps, name: name, onClear: handleClear, value: value, children: _jsx(Calendar, { onChange: handleChange, value: value }) }));
13
+ }
14
+ export default CalendarInput;
@@ -20,7 +20,8 @@ export interface FormFieldProps<T, V extends T> extends ClassNameProps<FormField
20
20
  isOptional?: boolean;
21
21
  label?: React.ReactNode;
22
22
  onChange?: (event: InputEvent<T>) => void;
23
+ onFieldChange?: (event: InputEvent<T>) => void;
23
24
  render: (props: InputProps<T>) => React.ReactElement | null;
24
25
  }
25
- declare function FormField<T, V extends T>({ action, classNameProps, classNames, details, error, hasAssistiveError, hasAssistiveDetails, hasAssistiveLabel, id, isContentOnly, isDisabled, isOptional, label, name, onChange, onKeyDown, render, value, }: Readonly<FormFieldProps<T, V>>): React.ReactElement | null;
26
+ declare function FormField<T, V extends T>({ action, classNameProps, classNames, details, error, hasAssistiveError, hasAssistiveDetails, hasAssistiveLabel, id, isContentOnly, isDisabled, isOptional, label, name, onChange, onFieldChange, onKeyDown, render, value, }: Readonly<FormFieldProps<T, V>>): React.ReactElement | null;
26
27
  export default FormField;
@@ -11,10 +11,14 @@ function checkHasError(error) {
11
11
  Object.keys(error).length > 0 &&
12
12
  Object.values(error).some((item) => typeof item === 'string'));
13
13
  }
14
- function FormField({ action, classNameProps, classNames, details, error, hasAssistiveError, hasAssistiveDetails, hasAssistiveLabel, id, isContentOnly, isDisabled, isOptional, label, name, onChange, onKeyDown, render, value, }) {
14
+ function FormField({ action, classNameProps, classNames, details, error, hasAssistiveError, hasAssistiveDetails, hasAssistiveLabel, id, isContentOnly, isDisabled, isOptional, label, name, onChange, onFieldChange, onKeyDown, render, value, }) {
15
15
  const componentClassNames = useClassNames('formField', { props: classNameProps, states: { isError: checkHasError(error) } }, classNames);
16
16
  const inputId = id || `ff_${name}`;
17
17
  const [inputError, setInputError] = useState(null);
18
+ function handleChange(event) {
19
+ onFieldChange?.(event);
20
+ onChange?.(event);
21
+ }
18
22
  function handleError(message) {
19
23
  setInputError(message);
20
24
  }
@@ -28,7 +32,7 @@ function FormField({ action, classNameProps, classNames, details, error, hasAssi
28
32
  isOptional,
29
33
  label,
30
34
  name,
31
- onChange,
35
+ onChange: handleChange,
32
36
  onError: handleError,
33
37
  onKeyDown,
34
38
  value,
@@ -1,14 +1,18 @@
1
1
  import type { FormFieldComponentProps } from '../../interfaces';
2
2
  import type { AutocompleteComponentProps } from '../Autocomplete';
3
+ import type { CalendarInputComponentProps } from '../CalendarInput';
3
4
  import type { DropdownComponentProps } from '../Dropdown';
4
5
  import type { PointsInputComponentProps } from '../PointsInput';
5
6
  import type { NumberInputComponentProps } from '../NumberInput';
6
7
  import type { PasswordInputComponentProps } from '../PasswordInput';
8
+ import type { RangeCalendarInputComponentProps } from '../RangeCalendarInput';
7
9
  import type { SwitchComponentProps } from '../Switch';
8
10
  import type { TextAreaComponentProps } from '../TextArea';
9
11
  import type { TextInputComponentProps } from '../TextInput';
10
12
  export type AutocompleteFormFieldProps<T> = FormFieldComponentProps<T | null> & AutocompleteComponentProps<T>;
11
13
  export declare function AutocompleteFormField<T>(props: Readonly<AutocompleteFormFieldProps<T>>): React.ReactElement;
14
+ export type CalendarFormFieldProps = FormFieldComponentProps<Date | null> & CalendarInputComponentProps;
15
+ export declare function CalendarFormField(props: Readonly<CalendarFormFieldProps>): React.ReactElement;
12
16
  export type CSVFormFieldProps = FormFieldComponentProps<string>;
13
17
  export declare function CSVFormField(props: Readonly<CSVFormFieldProps>): React.ReactElement;
14
18
  export type DropdownFormFieldProps<T> = FormFieldComponentProps<T | null> & DropdownComponentProps<T>;
@@ -19,6 +23,8 @@ export type NumberFormFieldProps = FormFieldComponentProps<number> & NumberInput
19
23
  export declare function NumberFormField(props: Readonly<NumberFormFieldProps>): React.ReactElement;
20
24
  export type PasswordFormFieldProps = FormFieldComponentProps<string> & PasswordInputComponentProps;
21
25
  export declare function PasswordFormField(props: Readonly<PasswordFormFieldProps>): React.ReactElement;
26
+ export type RangeCalendarFormFieldProps = FormFieldComponentProps<[Date, Date] | null> & RangeCalendarInputComponentProps;
27
+ export declare function RangeCalendarFormField(props: Readonly<RangeCalendarFormFieldProps>): React.ReactElement;
22
28
  export type SwitchFormFieldProps = FormFieldComponentProps<boolean> & SwitchComponentProps;
23
29
  export declare function SwitchFormField(props: SwitchFormFieldProps): React.ReactElement;
24
30
  export type TextFormFieldProps = FormFieldComponentProps<string> & TextInputComponentProps;
@@ -3,11 +3,13 @@ import { useCallback } from 'react';
3
3
  import extractInputProps from '../../utility/extract-input-props';
4
4
  import Autocomplete from '../Autocomplete';
5
5
  import CSVInput from '../CSVInput';
6
+ import CalendarInput from '../CalendarInput';
6
7
  import Dropdown from '../Dropdown';
7
8
  import FormField from '../FormField';
8
9
  import PointsInput from '../PointsInput';
9
10
  import NumberInput from '../NumberInput';
10
11
  import PasswordInput from '../PasswordInput';
12
+ import RangeCalendarInput from '../RangeCalendarInput';
11
13
  import Switch from '../Switch';
12
14
  import TextArea from '../TextArea';
13
15
  import TextInput from '../TextInput';
@@ -16,6 +18,11 @@ export function AutocompleteFormField(props) {
16
18
  const renderInput = useCallback((renderProps) => (_jsx(Autocomplete, { ...renderProps, ...inputProps })), [inputProps]);
17
19
  return _jsx(FormField, { ...fieldProps, render: renderInput });
18
20
  }
21
+ export function CalendarFormField(props) {
22
+ const { fieldProps, inputProps } = extractInputProps(props);
23
+ const renderInput = useCallback((renderProps) => (_jsx(CalendarInput, { ...renderProps, ...inputProps })), [inputProps]);
24
+ return _jsx(FormField, { ...fieldProps, render: renderInput });
25
+ }
19
26
  export function CSVFormField(props) {
20
27
  const { fieldProps, inputProps } = extractInputProps(props);
21
28
  const renderInput = useCallback((renderProps) => (_jsx(CSVInput, { ...renderProps, ...inputProps })), [inputProps]);
@@ -41,6 +48,11 @@ export function PasswordFormField(props) {
41
48
  const renderInput = useCallback((renderProps) => (_jsx(PasswordInput, { ...renderProps, ...inputProps })), [inputProps]);
42
49
  return _jsx(FormField, { ...fieldProps, render: renderInput });
43
50
  }
51
+ export function RangeCalendarFormField(props) {
52
+ const { fieldProps, inputProps } = extractInputProps(props);
53
+ const renderInput = useCallback((renderProps) => (_jsx(RangeCalendarInput, { ...renderProps, ...inputProps })), [inputProps]);
54
+ return _jsx(FormField, { ...fieldProps, render: renderInput });
55
+ }
44
56
  export function SwitchFormField(props) {
45
57
  const { fieldProps, inputProps } = extractInputProps(props);
46
58
  const renderInput = useCallback((renderProps) => (_jsx(Switch, { ...renderProps, ...inputProps })), [inputProps]);
@@ -1,5 +1,6 @@
1
- import { InputProps } from '../../../forms/interfaces';
2
- interface RangeCalendarInputProps extends InputProps<[Date, Date] | null> {
3
- }
4
- declare function RangeCalendarInput(props: RangeCalendarInputProps): React.ReactElement;
1
+ import { InputProps } from '../../interfaces';
2
+ import { BaseCalendarInputComponentProps } from '../BaseCalendarInput';
3
+ export type RangeCalendarInputComponentProps = BaseCalendarInputComponentProps;
4
+ export type RangeCalendarInputProps = InputProps<[Date, Date] | null> & RangeCalendarInputComponentProps;
5
+ declare function RangeCalendarInput({ name, onChange, value, ...baseProps }: RangeCalendarInputProps): React.ReactElement;
5
6
  export default RangeCalendarInput;
@@ -1,10 +1,14 @@
1
1
  'use client';
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import Calendar from '../../../components/utility/Calendar';
4
- function RangeCalendarInput(props) {
5
- function handleChange(value) {
6
- props.onChange?.({ target: { name: props.name, value } });
4
+ import BaseCalendarInput from '../BaseCalendarInput';
5
+ function RangeCalendarInput({ name, onChange, value, ...baseProps }) {
6
+ function handleChange(eventValue) {
7
+ onChange?.({ target: { name, value: eventValue } });
7
8
  }
8
- return _jsx(Calendar, { isRange: true, onChange: handleChange, value: props.value });
9
+ function handleClear() {
10
+ onChange?.({ target: { name, value: null } });
11
+ }
12
+ return (_jsx(BaseCalendarInput, { ...baseProps, name: name, onClear: handleClear, value: value, children: _jsx(Calendar, { isRange: true, onChange: handleChange, value: value }) }));
9
13
  }
10
14
  export default RangeCalendarInput;
@@ -12,7 +12,7 @@ export interface FieldPropsReturn<Request, K extends keyof Request = keyof Reque
12
12
  isDisabled?: boolean;
13
13
  label?: string;
14
14
  name: string;
15
- onChange: (event: InputEvent<Request[K]>) => void;
15
+ onFieldChange: (event: InputEvent<Request[K]>) => void;
16
16
  value: Request[K];
17
17
  }
18
18
  export interface ToastsArgs {
@@ -173,7 +173,7 @@ function useForm({ defaults = {}, onError, onSubmit, onSuccess, onValidation, re
173
173
  error: getErrorsForField(errors, name),
174
174
  label: getLabel(name, label),
175
175
  name: name,
176
- onChange: handleChange,
176
+ onFieldChange: handleChange,
177
177
  value: data[name],
178
178
  };
179
179
  }
@@ -13,6 +13,7 @@ export function getFieldPropKeys() {
13
13
  'label',
14
14
  'name',
15
15
  'onChange',
16
+ 'onFieldChange',
16
17
  'onKeyDown',
17
18
  'value',
18
19
  ];
@@ -0,0 +1,2 @@
1
+ declare function formatDateValue(date?: Date | [Date, Date] | null): string | null;
2
+ export default formatDateValue;
@@ -0,0 +1,13 @@
1
+ import { formatDate } from '@sqrzro/utility';
2
+ function formatDateValue(date) {
3
+ if (!date) {
4
+ return null;
5
+ }
6
+ const dateArray = Array.isArray(date) ? date : [date];
7
+ const formattedArray = dateArray.map((item) => formatDate(item));
8
+ if (formattedArray[0] === formattedArray[1]) {
9
+ return formattedArray[0];
10
+ }
11
+ return formattedArray.join(' - ');
12
+ }
13
+ export default formatDateValue;
@@ -22,6 +22,16 @@ function getDateState(date, pendingValue, value) {
22
22
  if (date && pendingValue && compareDates(date, pendingValue) === 0) {
23
23
  return { ...defaultState, isSelected: true };
24
24
  }
25
+ // For range, check that both dates are the same
26
+ if (date &&
27
+ !pendingValue &&
28
+ Array.isArray(value) &&
29
+ value[0] &&
30
+ value[1] &&
31
+ compareDates(value[0], value[1]) === 0 &&
32
+ compareDates(date, value[0]) === 0) {
33
+ return { ...defaultState, isSelected: true };
34
+ }
25
35
  // For range, check that the date is selected, and is the first of two selected
26
36
  if (date &&
27
37
  !pendingValue &&
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sqrzro/ui",
3
3
  "type": "module",
4
- "version": "4.0.0-alpha.57",
4
+ "version": "4.0.0-alpha.59",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "license": "ISC",