fis-component 0.2.2 → 0.2.4

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
@@ -64413,7 +64413,7 @@ const DivContainer$2 = styled.div `
64413
64413
  ${getTheme("com/badge/default/color-stroke/neutral")};
64414
64414
 
64415
64415
  &.solid {
64416
- border: ${getTheme("sem/dimension/stroke-width/border/md")} solid ${getTheme("com/badge/solid/background-color/neutral")};
64416
+ border: ${getTheme("com/badge/stroke-width")} solid transparent;
64417
64417
  }
64418
64418
 
64419
64419
  &.ghost {
@@ -64773,8 +64773,7 @@ const DivContainer$2 = styled.div `
64773
64773
 
64774
64774
  &.solid {
64775
64775
  background-color: ${getTheme("com/badge/solid/background-color/disable")};
64776
- border: ${getTheme("com/badge/stroke-width")} solid
64777
- ${getTheme("com/badge/disable/color-stroke")};
64776
+ border: ${getTheme("com/badge/stroke-width")} solid transparent;
64778
64777
  }
64779
64778
  `}
64780
64779
  `;
@@ -73048,8 +73047,42 @@ const HiddenDatePickerSC = styled(DatePicker) `
73048
73047
  z-index: -1;
73049
73048
  `;
73050
73049
 
73050
+ dayjs.extend(customParseFormat);
73051
+ const SEPARATORS = ["/", "-", "."];
73052
+ /**
73053
+ * Builds lenient-but-strict parse formats from a display format made only of
73054
+ * D/M/Y tokens and separators, e.g. "MM/YYYY" -> ["MM/YYYY", "M/YYYY",
73055
+ * "MM-YYYY", "M-YYYY", "MM.YYYY", "M.YYYY"].
73056
+ */
73057
+ const buildParseFormats = (format) => {
73058
+ if (!/^[DMY/.\- ]+$/.test(format))
73059
+ return [format];
73060
+ const digitVariants = new Set([format]);
73061
+ [
73062
+ ["DD", "D"],
73063
+ ["MM", "M"],
73064
+ ].forEach(([full, short]) => {
73065
+ Array.from(digitVariants).forEach((f) => {
73066
+ if (f.includes(full))
73067
+ digitVariants.add(f.replace(full, short));
73068
+ });
73069
+ });
73070
+ const result = new Set();
73071
+ digitVariants.forEach((f) => {
73072
+ result.add(f);
73073
+ SEPARATORS.forEach((sep) => result.add(f.replace(/[/.-]/g, sep)));
73074
+ });
73075
+ return Array.from(result);
73076
+ };
73077
+ const parseDateText = (text, formats) => {
73078
+ const trimmed = text.trim();
73079
+ if (!trimmed)
73080
+ return null;
73081
+ const parsed = dayjs(trimmed, formats, true);
73082
+ return parsed.isValid() ? parsed : null;
73083
+ };
73051
73084
  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;
73085
+ 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
73086
  const getDefaultFormat = (pickerMode) => {
73054
73087
  switch (pickerMode) {
73055
73088
  case "year":
@@ -73064,19 +73097,48 @@ const FISInputDate$1 = forwardRef((props, ref) => {
73064
73097
  }
73065
73098
  };
73066
73099
  const finalFormat = format || getDefaultFormat(picker);
73100
+ const parseText = (text) => {
73101
+ if (parseInput) {
73102
+ if (!text.trim())
73103
+ return null;
73104
+ const result = parseInput(text);
73105
+ if (!result)
73106
+ return null;
73107
+ const d = dayjs(result);
73108
+ return d.isValid() ? d : null;
73109
+ }
73110
+ const formats = parseFormats
73111
+ ? Array.isArray(parseFormats)
73112
+ ? parseFormats
73113
+ : [parseFormats]
73114
+ : buildParseFormats(finalFormat);
73115
+ return parseDateText(text, formats);
73116
+ };
73067
73117
  const [open, setOpen] = useState(false);
73068
73118
  const [inputValue, setInputValue] = useState(value ? dayjs(value).format(finalFormat) : "");
73069
73119
  const [dateValue, setDateValue] = useState(value ? dayjs(value) : null);
73070
73120
  const inputRef = useRef(null);
73071
73121
  const mergeRef = mergeRefs(inputRef, ref);
73072
73122
  // Update internal state when external value changes
73123
+ const dateValueRef = useRef(dateValue);
73124
+ dateValueRef.current = dateValue;
73125
+ const formatRef = useRef(finalFormat);
73073
73126
  useEffect(() => {
73127
+ const formatChanged = formatRef.current !== finalFormat;
73128
+ formatRef.current = finalFormat;
73129
+ const current = dateValueRef.current;
73074
73130
  if (value) {
73075
73131
  const newDayjs = dayjs(value);
73132
+ // Value echoed back from our own onChange while typing: keep the
73133
+ // user's text as-is (it is normalized on blur / Enter).
73134
+ if (!formatChanged && current && current.isSame(newDayjs))
73135
+ return;
73076
73136
  setDateValue(newDayjs);
73077
73137
  setInputValue(newDayjs.format(finalFormat));
73078
73138
  }
73079
73139
  else {
73140
+ if (!formatChanged && !current)
73141
+ return;
73080
73142
  setDateValue(null);
73081
73143
  setInputValue("");
73082
73144
  }
@@ -73102,11 +73164,13 @@ const FISInputDate$1 = forwardRef((props, ref) => {
73102
73164
  onChange?.(null);
73103
73165
  return;
73104
73166
  }
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());
73167
+ // Try to parse the input as a date (strict)
73168
+ const parsedDate = parseText(newInputValue);
73169
+ if (parsedDate) {
73170
+ if (!dateValue || !dateValue.isSame(parsedDate)) {
73171
+ setDateValue(parsedDate);
73172
+ onChange?.(parsedDate.toDate());
73173
+ }
73110
73174
  }
73111
73175
  // If invalid format, keep inputValue but don't update dateValue or call onChange
73112
73176
  // This preserves the old date value like Antd DatePicker
@@ -73120,18 +73184,32 @@ const FISInputDate$1 = forwardRef((props, ref) => {
73120
73184
  if (inputValue === "") {
73121
73185
  return;
73122
73186
  }
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
73187
+ normalizeInput();
73188
+ };
73189
+ // Valid text -> normalize to display format; invalid -> revert to the
73190
+ // current value (or clear if there is none).
73191
+ const normalizeInput = () => {
73192
+ if (inputValue === "")
73193
+ return;
73194
+ const parsedDate = parseText(inputValue);
73195
+ if (parsedDate) {
73196
+ setInputValue(parsedDate.format(finalFormat));
73197
+ }
73198
+ else if (dateValue) {
73127
73199
  setInputValue(dateValue.format(finalFormat));
73128
73200
  }
73129
- else if (!parsedDate.isValid() && !dateValue) {
73130
- // If no current date and invalid input, clear input
73201
+ else {
73131
73202
  setInputValue("");
73132
73203
  }
73133
73204
  };
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({
73205
+ const handleInputKeyDown = (e) => {
73206
+ if (e.key === "Enter") {
73207
+ normalizeInput();
73208
+ setOpen(false);
73209
+ }
73210
+ originalOnKeyDown?.(e);
73211
+ };
73212
+ 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
73213
  disabled,
73136
73214
  negative,
73137
73215
  positive,