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.
package/dist/cjs/index.js CHANGED
@@ -5569,7 +5569,7 @@ const DivWrapperSC$5 = styled.div `
5569
5569
  `;
5570
5570
 
5571
5571
  const FISInputStepper$1 = React.forwardRef((props, ref) => {
5572
- const { min, max, step = 1, defaultValue, sizeInput = "md", StartIcon, EndIcon, negative, positive, message, iconLabel, textLabel = "", onClickIconLabel, disabled, readOnly, onChange, ...rest } = props;
5572
+ const { min, max, step = 1, defaultValue, sizeInput = "md", StartIcon, EndIcon, negative, positive, message, iconLabel, textLabel = "", onClickIconLabel, disabled, readOnly, onChange, className, ...rest } = props;
5573
5573
  const [count, setCount] = React.useState(defaultValue !== undefined ? defaultValue : undefined);
5574
5574
  const handleIncrement = () => {
5575
5575
  setCount((prev) => {
@@ -5600,7 +5600,7 @@ const FISInputStepper$1 = React.forwardRef((props, ref) => {
5600
5600
  "input-text-lg": sizeInput === "lg",
5601
5601
  }), children: [jsxRuntime.jsx(FISIconButton$1, { icon: StartIcon ? StartIcon : jsxRuntime.jsx(DecreIcon, {}), size: sizeInput === "lg" ? "md" : "sm", variant: "tertiary-invisible", onClick: handleDecrement, disabled: min !== undefined && count !== undefined && count <= min, className: classNames("decrement", {
5602
5602
  "disabled-button": disabled || readOnly,
5603
- }) }), jsxRuntime.jsx(InputSC$1, { ...rest, ref: ref, disabled: disabled, readOnly: readOnly, type: "number", value: count !== undefined ? count : "", className: classNames({
5603
+ }) }), jsxRuntime.jsx(InputSC$1, { ...rest, ref: ref, disabled: disabled, readOnly: readOnly, type: "number", value: count !== undefined ? count : "", className: classNames(className, {
5604
5604
  negative: negative,
5605
5605
  "input-text-lg": sizeInput === "lg",
5606
5606
  }), onChange: handleChange }), jsxRuntime.jsx(FISIconButton$1, { icon: EndIcon ? EndIcon : jsxRuntime.jsx(IncreIcon, {}), size: sizeInput === "lg" ? "md" : "sm", variant: "tertiary-invisible", onClick: handleIncrement, disabled: max !== undefined && count !== undefined && count >= max, className: classNames("increment", {
@@ -73068,8 +73068,42 @@ const HiddenDatePickerSC = styled(DatePicker) `
73068
73068
  z-index: -1;
73069
73069
  `;
73070
73070
 
73071
+ dayjs.extend(customParseFormat);
73072
+ const SEPARATORS = ["/", "-", "."];
73073
+ /**
73074
+ * Builds lenient-but-strict parse formats from a display format made only of
73075
+ * D/M/Y tokens and separators, e.g. "MM/YYYY" -> ["MM/YYYY", "M/YYYY",
73076
+ * "MM-YYYY", "M-YYYY", "MM.YYYY", "M.YYYY"].
73077
+ */
73078
+ const buildParseFormats = (format) => {
73079
+ if (!/^[DMY/.\- ]+$/.test(format))
73080
+ return [format];
73081
+ const digitVariants = new Set([format]);
73082
+ [
73083
+ ["DD", "D"],
73084
+ ["MM", "M"],
73085
+ ].forEach(([full, short]) => {
73086
+ Array.from(digitVariants).forEach((f) => {
73087
+ if (f.includes(full))
73088
+ digitVariants.add(f.replace(full, short));
73089
+ });
73090
+ });
73091
+ const result = new Set();
73092
+ digitVariants.forEach((f) => {
73093
+ result.add(f);
73094
+ SEPARATORS.forEach((sep) => result.add(f.replace(/[/.-]/g, sep)));
73095
+ });
73096
+ return Array.from(result);
73097
+ };
73098
+ const parseDateText = (text, formats) => {
73099
+ const trimmed = text.trim();
73100
+ if (!trimmed)
73101
+ return null;
73102
+ const parsed = dayjs(trimmed, formats, true);
73103
+ return parsed.isValid() ? parsed : null;
73104
+ };
73071
73105
  const FISInputDate$1 = React.forwardRef((props, ref) => {
73072
- 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;
73106
+ 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;
73073
73107
  const getDefaultFormat = (pickerMode) => {
73074
73108
  switch (pickerMode) {
73075
73109
  case "year":
@@ -73084,19 +73118,48 @@ const FISInputDate$1 = React.forwardRef((props, ref) => {
73084
73118
  }
73085
73119
  };
73086
73120
  const finalFormat = format || getDefaultFormat(picker);
73121
+ const parseText = (text) => {
73122
+ if (parseInput) {
73123
+ if (!text.trim())
73124
+ return null;
73125
+ const result = parseInput(text);
73126
+ if (!result)
73127
+ return null;
73128
+ const d = dayjs(result);
73129
+ return d.isValid() ? d : null;
73130
+ }
73131
+ const formats = parseFormats
73132
+ ? Array.isArray(parseFormats)
73133
+ ? parseFormats
73134
+ : [parseFormats]
73135
+ : buildParseFormats(finalFormat);
73136
+ return parseDateText(text, formats);
73137
+ };
73087
73138
  const [open, setOpen] = React.useState(false);
73088
73139
  const [inputValue, setInputValue] = React.useState(value ? dayjs(value).format(finalFormat) : "");
73089
73140
  const [dateValue, setDateValue] = React.useState(value ? dayjs(value) : null);
73090
73141
  const inputRef = React.useRef(null);
73091
73142
  const mergeRef = mergeRefs(inputRef, ref);
73092
73143
  // Update internal state when external value changes
73144
+ const dateValueRef = React.useRef(dateValue);
73145
+ dateValueRef.current = dateValue;
73146
+ const formatRef = React.useRef(finalFormat);
73093
73147
  React.useEffect(() => {
73148
+ const formatChanged = formatRef.current !== finalFormat;
73149
+ formatRef.current = finalFormat;
73150
+ const current = dateValueRef.current;
73094
73151
  if (value) {
73095
73152
  const newDayjs = dayjs(value);
73153
+ // Value echoed back from our own onChange while typing: keep the
73154
+ // user's text as-is (it is normalized on blur / Enter).
73155
+ if (!formatChanged && current && current.isSame(newDayjs))
73156
+ return;
73096
73157
  setDateValue(newDayjs);
73097
73158
  setInputValue(newDayjs.format(finalFormat));
73098
73159
  }
73099
73160
  else {
73161
+ if (!formatChanged && !current)
73162
+ return;
73100
73163
  setDateValue(null);
73101
73164
  setInputValue("");
73102
73165
  }
@@ -73122,11 +73185,13 @@ const FISInputDate$1 = React.forwardRef((props, ref) => {
73122
73185
  onChange?.(null);
73123
73186
  return;
73124
73187
  }
73125
- // Try to parse the input as a date
73126
- const parsedDate = dayjs(newInputValue, finalFormat);
73127
- if (parsedDate.isValid()) {
73128
- setDateValue(parsedDate);
73129
- onChange?.(parsedDate.toDate());
73188
+ // Try to parse the input as a date (strict)
73189
+ const parsedDate = parseText(newInputValue);
73190
+ if (parsedDate) {
73191
+ if (!dateValue || !dateValue.isSame(parsedDate)) {
73192
+ setDateValue(parsedDate);
73193
+ onChange?.(parsedDate.toDate());
73194
+ }
73130
73195
  }
73131
73196
  // If invalid format, keep inputValue but don't update dateValue or call onChange
73132
73197
  // This preserves the old date value like Antd DatePicker
@@ -73140,18 +73205,32 @@ const FISInputDate$1 = React.forwardRef((props, ref) => {
73140
73205
  if (inputValue === "") {
73141
73206
  return;
73142
73207
  }
73143
- // On blur, if input format is invalid, revert to the current date value's formatted string
73144
- const parsedDate = dayjs(inputValue, finalFormat);
73145
- if (!parsedDate.isValid() && dateValue) {
73146
- // Revert to formatted current date value
73208
+ normalizeInput();
73209
+ };
73210
+ // Valid text -> normalize to display format; invalid -> revert to the
73211
+ // current value (or clear if there is none).
73212
+ const normalizeInput = () => {
73213
+ if (inputValue === "")
73214
+ return;
73215
+ const parsedDate = parseText(inputValue);
73216
+ if (parsedDate) {
73217
+ setInputValue(parsedDate.format(finalFormat));
73218
+ }
73219
+ else if (dateValue) {
73147
73220
  setInputValue(dateValue.format(finalFormat));
73148
73221
  }
73149
- else if (!parsedDate.isValid() && !dateValue) {
73150
- // If no current date and invalid input, clear input
73222
+ else {
73151
73223
  setInputValue("");
73152
73224
  }
73153
73225
  };
73154
- return (jsxRuntime.jsxs(DivWrapperSC$2, { className: className, children: [(textLabel || iconLabel) && (jsxRuntime.jsx(FISInputLabel$1, { textLabel: textLabel, required: required, iconLabel: iconLabel, onClickIconLabel: onClickIconLabel })), jsxRuntime.jsxs(DivInputWrapperSC$1, { children: [jsxRuntime.jsx(FISInputField$1, { ...restProps, ref: mergeRef, typeSuffix: "icon", iconSuffix: jsxRuntime.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" }), jsxRuntime.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 && (jsxRuntime.jsx(DivHintWrapperSC$1, { children: jsxRuntime.jsx(SpanHintSC$3, { className: classNames({
73226
+ const handleInputKeyDown = (e) => {
73227
+ if (e.key === "Enter") {
73228
+ normalizeInput();
73229
+ setOpen(false);
73230
+ }
73231
+ originalOnKeyDown?.(e);
73232
+ };
73233
+ return (jsxRuntime.jsxs(DivWrapperSC$2, { className: className, children: [(textLabel || iconLabel) && (jsxRuntime.jsx(FISInputLabel$1, { textLabel: textLabel, required: required, iconLabel: iconLabel, onClickIconLabel: onClickIconLabel })), jsxRuntime.jsxs(DivInputWrapperSC$1, { children: [jsxRuntime.jsx(FISInputField$1, { ...restProps, ref: mergeRef, typeSuffix: "icon", iconSuffix: jsxRuntime.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" }), jsxRuntime.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 && (jsxRuntime.jsx(DivHintWrapperSC$1, { children: jsxRuntime.jsx(SpanHintSC$3, { className: classNames({
73155
73234
  disabled,
73156
73235
  negative,
73157
73236
  positive,