fis-component 0.2.1 → 0.2.3

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.
@@ -1,5 +1,5 @@
1
1
  import { InputFieldProps } from "../InputField";
2
- import dayjs from "dayjs";
2
+ import dayjs, { Dayjs } from "dayjs";
3
3
  import { InputLabelProps } from "../InputLabel";
4
4
  export type DatePickerMode = "date" | "month" | "year" | "quarter";
5
5
  export interface InputDateProps extends Omit<InputFieldProps, "value" | "onChange">, Partial<InputLabelProps> {
@@ -49,6 +49,18 @@ export interface InputDateProps extends Omit<InputFieldProps, "value" | "onChang
49
49
  };
50
50
  /**Custom input format*/
51
51
  inputReadOnly?: boolean;
52
+ /**
53
+ * Formats accepted when the user types into the input (strict parsing).
54
+ * Defaults to `format` plus lenient variants: single-digit day/month
55
+ * (e.g. `M/YYYY`) and `/`, `-`, `.` as separators.
56
+ */
57
+ parseFormats?: string | string[];
58
+ /**
59
+ * Custom parser for typed text. Return a Date for a valid value,
60
+ * `null` for invalid text. Overrides `parseFormats` when provided.
61
+ */
62
+ parseInput?: (text: string) => Date | null | undefined;
52
63
  }
64
+ export declare const parseDateText: (text: string, formats: string | string[]) => Dayjs | null;
53
65
  declare const FISInputDate: import("react").ForwardRefExoticComponent<InputDateProps & import("react").RefAttributes<HTMLInputElement>>;
54
66
  export default FISInputDate;
@@ -391,6 +391,10 @@ declare const meta: {
391
391
  control: "text";
392
392
  description: string;
393
393
  };
394
+ className: {
395
+ control: "text";
396
+ description: string;
397
+ };
394
398
  };
395
399
  };
396
400
  export default meta;
@@ -402,3 +406,4 @@ export declare const WithNegative: Story;
402
406
  export declare const ReadOnly: Story;
403
407
  export declare const WithPositive: Story;
404
408
  export declare const CustomIcons: Story;
409
+ export declare const CustomClassName: Story;
package/dist/esm/index.js CHANGED
@@ -5549,7 +5549,7 @@ const DivWrapperSC$5 = styled.div `
5549
5549
  `;
5550
5550
 
5551
5551
  const FISInputStepper$1 = forwardRef((props, ref) => {
5552
- const { min, max, step = 1, defaultValue, sizeInput = "md", StartIcon, EndIcon, negative, positive, message, iconLabel, textLabel = "", onClickIconLabel, disabled, readOnly, onChange, ...rest } = props;
5552
+ const { min, max, step = 1, defaultValue, sizeInput = "md", StartIcon, EndIcon, negative, positive, message, iconLabel, textLabel = "", onClickIconLabel, disabled, readOnly, onChange, className, ...rest } = props;
5553
5553
  const [count, setCount] = useState(defaultValue !== undefined ? defaultValue : undefined);
5554
5554
  const handleIncrement = () => {
5555
5555
  setCount((prev) => {
@@ -5580,7 +5580,7 @@ const FISInputStepper$1 = forwardRef((props, ref) => {
5580
5580
  "input-text-lg": sizeInput === "lg",
5581
5581
  }), children: [jsx(FISIconButton$1, { icon: StartIcon ? StartIcon : jsx(DecreIcon, {}), size: sizeInput === "lg" ? "md" : "sm", variant: "tertiary-invisible", onClick: handleDecrement, disabled: min !== undefined && count !== undefined && count <= min, className: classNames("decrement", {
5582
5582
  "disabled-button": disabled || readOnly,
5583
- }) }), jsx(InputSC$1, { ...rest, ref: ref, disabled: disabled, readOnly: readOnly, type: "number", value: count !== undefined ? count : "", className: classNames({
5583
+ }) }), jsx(InputSC$1, { ...rest, ref: ref, disabled: disabled, readOnly: readOnly, type: "number", value: count !== undefined ? count : "", className: classNames(className, {
5584
5584
  negative: negative,
5585
5585
  "input-text-lg": sizeInput === "lg",
5586
5586
  }), onChange: handleChange }), jsx(FISIconButton$1, { icon: EndIcon ? EndIcon : jsx(IncreIcon, {}), size: sizeInput === "lg" ? "md" : "sm", variant: "tertiary-invisible", onClick: handleIncrement, disabled: max !== undefined && count !== undefined && count >= max, className: classNames("increment", {
@@ -73048,8 +73048,42 @@ const HiddenDatePickerSC = styled(DatePicker) `
73048
73048
  z-index: -1;
73049
73049
  `;
73050
73050
 
73051
+ dayjs.extend(customParseFormat);
73052
+ const SEPARATORS = ["/", "-", "."];
73053
+ /**
73054
+ * Builds lenient-but-strict parse formats from a display format made only of
73055
+ * D/M/Y tokens and separators, e.g. "MM/YYYY" -> ["MM/YYYY", "M/YYYY",
73056
+ * "MM-YYYY", "M-YYYY", "MM.YYYY", "M.YYYY"].
73057
+ */
73058
+ const buildParseFormats = (format) => {
73059
+ if (!/^[DMY/.\- ]+$/.test(format))
73060
+ return [format];
73061
+ const digitVariants = new Set([format]);
73062
+ [
73063
+ ["DD", "D"],
73064
+ ["MM", "M"],
73065
+ ].forEach(([full, short]) => {
73066
+ Array.from(digitVariants).forEach((f) => {
73067
+ if (f.includes(full))
73068
+ digitVariants.add(f.replace(full, short));
73069
+ });
73070
+ });
73071
+ const result = new Set();
73072
+ digitVariants.forEach((f) => {
73073
+ result.add(f);
73074
+ SEPARATORS.forEach((sep) => result.add(f.replace(/[/.-]/g, sep)));
73075
+ });
73076
+ return Array.from(result);
73077
+ };
73078
+ const parseDateText = (text, formats) => {
73079
+ const trimmed = text.trim();
73080
+ if (!trimmed)
73081
+ return null;
73082
+ const parsed = dayjs(trimmed, formats, true);
73083
+ return parsed.isValid() ? parsed : null;
73084
+ };
73051
73085
  const FISInputDate$1 = forwardRef((props, ref) => {
73052
- const { className, value, textLabel = "", iconLabel, required, message = "", disabled, negative, positive, format, onClickIconLabel, onChange, getPopupContainer, minDate, maxDate, picker = "date", allowClear = true, showToday = true, showTime = false, autoFocus = false, disabledDate, disabledTime, inputReadOnly = false, onBlur: originalOnBlur, ...restProps } = props;
73086
+ const { className, value, textLabel = "", iconLabel, required, message = "", disabled, negative, positive, format, onClickIconLabel, onChange, getPopupContainer, minDate, maxDate, picker = "date", allowClear = true, showToday = true, showTime = false, autoFocus = false, disabledDate, disabledTime, inputReadOnly = false, onBlur: originalOnBlur, onKeyDown: originalOnKeyDown, parseFormats, parseInput, ...restProps } = props;
73053
73087
  const getDefaultFormat = (pickerMode) => {
73054
73088
  switch (pickerMode) {
73055
73089
  case "year":
@@ -73064,19 +73098,48 @@ const FISInputDate$1 = forwardRef((props, ref) => {
73064
73098
  }
73065
73099
  };
73066
73100
  const finalFormat = format || getDefaultFormat(picker);
73101
+ const parseText = (text) => {
73102
+ if (parseInput) {
73103
+ if (!text.trim())
73104
+ return null;
73105
+ const result = parseInput(text);
73106
+ if (!result)
73107
+ return null;
73108
+ const d = dayjs(result);
73109
+ return d.isValid() ? d : null;
73110
+ }
73111
+ const formats = parseFormats
73112
+ ? Array.isArray(parseFormats)
73113
+ ? parseFormats
73114
+ : [parseFormats]
73115
+ : buildParseFormats(finalFormat);
73116
+ return parseDateText(text, formats);
73117
+ };
73067
73118
  const [open, setOpen] = useState(false);
73068
73119
  const [inputValue, setInputValue] = useState(value ? dayjs(value).format(finalFormat) : "");
73069
73120
  const [dateValue, setDateValue] = useState(value ? dayjs(value) : null);
73070
73121
  const inputRef = useRef(null);
73071
73122
  const mergeRef = mergeRefs(inputRef, ref);
73072
73123
  // Update internal state when external value changes
73124
+ const dateValueRef = useRef(dateValue);
73125
+ dateValueRef.current = dateValue;
73126
+ const formatRef = useRef(finalFormat);
73073
73127
  useEffect(() => {
73128
+ const formatChanged = formatRef.current !== finalFormat;
73129
+ formatRef.current = finalFormat;
73130
+ const current = dateValueRef.current;
73074
73131
  if (value) {
73075
73132
  const newDayjs = dayjs(value);
73133
+ // Value echoed back from our own onChange while typing: keep the
73134
+ // user's text as-is (it is normalized on blur / Enter).
73135
+ if (!formatChanged && current && current.isSame(newDayjs))
73136
+ return;
73076
73137
  setDateValue(newDayjs);
73077
73138
  setInputValue(newDayjs.format(finalFormat));
73078
73139
  }
73079
73140
  else {
73141
+ if (!formatChanged && !current)
73142
+ return;
73080
73143
  setDateValue(null);
73081
73144
  setInputValue("");
73082
73145
  }
@@ -73102,11 +73165,13 @@ const FISInputDate$1 = forwardRef((props, ref) => {
73102
73165
  onChange?.(null);
73103
73166
  return;
73104
73167
  }
73105
- // Try to parse the input as a date
73106
- const parsedDate = dayjs(newInputValue, finalFormat);
73107
- if (parsedDate.isValid()) {
73108
- setDateValue(parsedDate);
73109
- onChange?.(parsedDate.toDate());
73168
+ // Try to parse the input as a date (strict)
73169
+ const parsedDate = parseText(newInputValue);
73170
+ if (parsedDate) {
73171
+ if (!dateValue || !dateValue.isSame(parsedDate)) {
73172
+ setDateValue(parsedDate);
73173
+ onChange?.(parsedDate.toDate());
73174
+ }
73110
73175
  }
73111
73176
  // If invalid format, keep inputValue but don't update dateValue or call onChange
73112
73177
  // This preserves the old date value like Antd DatePicker
@@ -73120,18 +73185,32 @@ const FISInputDate$1 = forwardRef((props, ref) => {
73120
73185
  if (inputValue === "") {
73121
73186
  return;
73122
73187
  }
73123
- // On blur, if input format is invalid, revert to the current date value's formatted string
73124
- const parsedDate = dayjs(inputValue, finalFormat);
73125
- if (!parsedDate.isValid() && dateValue) {
73126
- // Revert to formatted current date value
73188
+ normalizeInput();
73189
+ };
73190
+ // Valid text -> normalize to display format; invalid -> revert to the
73191
+ // current value (or clear if there is none).
73192
+ const normalizeInput = () => {
73193
+ if (inputValue === "")
73194
+ return;
73195
+ const parsedDate = parseText(inputValue);
73196
+ if (parsedDate) {
73197
+ setInputValue(parsedDate.format(finalFormat));
73198
+ }
73199
+ else if (dateValue) {
73127
73200
  setInputValue(dateValue.format(finalFormat));
73128
73201
  }
73129
- else if (!parsedDate.isValid() && !dateValue) {
73130
- // If no current date and invalid input, clear input
73202
+ else {
73131
73203
  setInputValue("");
73132
73204
  }
73133
73205
  };
73134
- return (jsxs(DivWrapperSC$2, { className: className, children: [(textLabel || iconLabel) && (jsx(FISInputLabel$1, { textLabel: textLabel, required: required, iconLabel: iconLabel, onClickIconLabel: onClickIconLabel })), jsxs(DivInputWrapperSC$1, { children: [jsx(FISInputField$1, { ...restProps, ref: mergeRef, typeSuffix: "icon", iconSuffix: jsx(DateIcon, {}), value: inputValue, negative: negative, disabled: disabled, placeholder: restProps.placeholder, readOnly: inputReadOnly, autoFocus: autoFocus, onFocus: () => setOpen(true), onChange: handleInputChange, onBlur: handleInputBlur, onClickSuffix: () => setOpen(true), autoComplete: "off" }), jsx(HiddenDatePickerSC, { open: open, value: dateValue, onChange: (value) => handleChange(value), onOpenChange: handleOpenChange, format: finalFormat, getPopupContainer: getPopupContainer, minDate: minDate, maxDate: maxDate, picker: picker, allowClear: allowClear, showToday: showToday, showTime: showTime, disabledDate: disabledDate, disabledTime: disabledTime })] }), message && (jsx(DivHintWrapperSC$1, { children: jsx(SpanHintSC$3, { className: classNames({
73206
+ const handleInputKeyDown = (e) => {
73207
+ if (e.key === "Enter") {
73208
+ normalizeInput();
73209
+ setOpen(false);
73210
+ }
73211
+ originalOnKeyDown?.(e);
73212
+ };
73213
+ return (jsxs(DivWrapperSC$2, { className: className, children: [(textLabel || iconLabel) && (jsx(FISInputLabel$1, { textLabel: textLabel, required: required, iconLabel: iconLabel, onClickIconLabel: onClickIconLabel })), jsxs(DivInputWrapperSC$1, { children: [jsx(FISInputField$1, { ...restProps, ref: mergeRef, typeSuffix: "icon", iconSuffix: jsx(DateIcon, {}), value: inputValue, negative: negative, disabled: disabled, placeholder: restProps.placeholder, readOnly: inputReadOnly, autoFocus: autoFocus, onFocus: () => setOpen(true), onChange: handleInputChange, onBlur: handleInputBlur, onKeyDown: handleInputKeyDown, onClickSuffix: () => setOpen(true), autoComplete: "off" }), jsx(HiddenDatePickerSC, { open: open, value: dateValue, onChange: (value) => handleChange(value), onOpenChange: handleOpenChange, format: finalFormat, getPopupContainer: getPopupContainer, minDate: minDate, maxDate: maxDate, picker: picker, allowClear: allowClear, showToday: showToday, showTime: showTime, disabledDate: disabledDate, disabledTime: disabledTime })] }), message && (jsx(DivHintWrapperSC$1, { children: jsx(SpanHintSC$3, { className: classNames({
73135
73214
  disabled,
73136
73215
  negative,
73137
73216
  positive,