pesona-ui 1.0.15 → 1.0.17

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/index.cjs.js CHANGED
@@ -8202,6 +8202,9 @@ const Select = React.forwardRef(({ name, label, selectLabel, size = 'md', floati
8202
8202
  setIsOpen(false);
8203
8203
  };
8204
8204
  }, []);
8205
+ const displayText = value
8206
+ ? options.find((option) => option.value === value)?.label
8207
+ : selectLabel || 'Select an option';
8205
8208
  return (React.createElement(React.Fragment, null,
8206
8209
  label && !floatingLabel && (React.createElement("label", { htmlFor: name },
8207
8210
  label,
@@ -8209,10 +8212,8 @@ const Select = React.forwardRef(({ name, label, selectLabel, size = 'md', floati
8209
8212
  required && React.createElement("span", { className: "text-danger" }, "*"))),
8210
8213
  React.createElement("div", { className: `dropdown-select-container ${size}`, ref: dropdownRef },
8211
8214
  React.createElement("div", { className: `dropdown-header ${isOpen ? 'open' : ''} ${disabled ? 'disabled' : ''}`, onClick: () => !disabled && setIsOpen(!isOpen), ref: ref, "aria-haspopup": "listbox", "aria-expanded": isOpen },
8212
- value
8213
- ? options.find((option) => option.value === value)?.label
8214
- : selectLabel || 'Select an option',
8215
- React.createElement(FiChevronDown, null)),
8215
+ React.createElement("span", { className: "label" }, displayText),
8216
+ React.createElement(FiChevronDown, { className: "arrow" })),
8216
8217
  isOpen && (React.createElement("div", { className: `dropdown-options scrollbar ${disabled ? 'disabled' : ''}`, ref: dropdownOptionsRef, role: "listbox" }, options.map((option, index) => (React.createElement("div", { key: `${option.value}-${index}`, className: `option ${value === option.value ? 'selected' : ''}`, onClick: () => !disabled && handleOptionClick(option.value.toString()), role: "option", "aria-selected": value === option.value }, option.label)))))),
8217
8218
  label && floatingLabel && (React.createElement("label", { htmlFor: name },
8218
8219
  label,
@@ -8222,46 +8223,77 @@ const Select = React.forwardRef(({ name, label, selectLabel, size = 'md', floati
8222
8223
  });
8223
8224
  Select.displayName = 'Select';
8224
8225
 
8225
- const DropdownDatePicker = React.forwardRef(({ name, label, floatingLabel, error, ...rest }, ref) => {
8226
- const [locale] = React.useState('en-US'); // Assuming locale is set to 'en-US' for simplicity
8227
- const selectedDate = rest.value ? new Date(rest.value) : new Date();
8226
+ const DropdownDatePicker = React.forwardRef(({ name, label, floatingLabel, locale = 'id', error, value, ...rest }, ref) => {
8227
+ // Helper function to safely parse date
8228
+ const parseDate = (dateString) => {
8229
+ if (!dateString)
8230
+ return new Date();
8231
+ const parsed = new Date(dateString);
8232
+ return isNaN(parsed.getTime()) ? new Date() : parsed;
8233
+ };
8234
+ // Initial date state
8235
+ const initialDate = parseDate(value);
8228
8236
  const [dateValues, setDateValues] = React.useState({
8229
- day: selectedDate.getDate(),
8230
- month: selectedDate.getMonth() + 1,
8231
- year: selectedDate.getFullYear(),
8237
+ day: initialDate.getDate(),
8238
+ month: initialDate.getMonth() + 1,
8239
+ year: initialDate.getFullYear(),
8232
8240
  });
8233
- const dayOptions = generateDayOptions(dateValues.month, dateValues.year);
8234
- const monthOptions = generateMonthOptions(locale);
8235
- const yearOptions = generateYearOptions();
8241
+ // Sync when external value changes
8242
+ React.useEffect(() => {
8243
+ if (value) {
8244
+ const d = parseDate(value);
8245
+ setDateValues({
8246
+ day: d.getDate(),
8247
+ month: d.getMonth() + 1,
8248
+ year: d.getFullYear(),
8249
+ });
8250
+ }
8251
+ }, [value]);
8252
+ // Ensure day is valid for the selected month and year
8253
+ React.useEffect(() => {
8254
+ const maxDay = new Date(dateValues.year, dateValues.month, 0).getDate();
8255
+ if (dateValues.day > maxDay) {
8256
+ setDateValues(prev => ({ ...prev, day: maxDay }));
8257
+ }
8258
+ // eslint-disable-next-line react-hooks/exhaustive-deps
8259
+ }, [dateValues.month, dateValues.year]);
8260
+ // Handle change for day, month, year with validation
8236
8261
  const handleChange = (field, newValue) => {
8237
- const newDateValues = { ...dateValues, [field]: newValue };
8262
+ const numericValue = parseInt(newValue, 10);
8263
+ if (isNaN(numericValue))
8264
+ return; // Guard against invalid input
8265
+ const newDateValues = { ...dateValues, [field]: numericValue };
8238
8266
  setDateValues(newDateValues);
8239
8267
  if (rest.onChange) {
8268
+ const formattedDate = `${newDateValues.year}-${String(newDateValues.month).padStart(2, '0')}-${String(newDateValues.day).padStart(2, '0')}`;
8240
8269
  rest.onChange({
8241
8270
  target: {
8242
8271
  name,
8243
- value: `${newDateValues.year}-${newDateValues.month}-${newDateValues.day}`,
8272
+ value: formattedDate,
8244
8273
  },
8245
8274
  });
8246
8275
  }
8247
8276
  };
8277
+ // Memoized options to prevent unnecessary re-calculations
8278
+ const dayOptions = React.useMemo(() => generateDayOptions(dateValues.month - 1, dateValues.year), [dateValues.month, dateValues.year]);
8279
+ const monthOptions = React.useMemo(() => generateMonthOptions(locale), [locale]);
8280
+ const yearOptions = React.useMemo(() => generateYearOptions(), []);
8281
+ // Label rendering
8282
+ const renderLabel = (React.createElement("label", { htmlFor: name },
8283
+ label,
8284
+ " ",
8285
+ rest.required && React.createElement("span", { className: "text-danger" }, "*")));
8248
8286
  return (React.createElement(React.Fragment, null,
8249
- !floatingLabel && (React.createElement("label", { htmlFor: name },
8250
- label,
8251
- " ",
8252
- rest.required && React.createElement("span", { className: "text-danger" }, "*"))),
8287
+ !floatingLabel && renderLabel,
8253
8288
  React.createElement("div", { className: "row", ref: ref },
8254
8289
  React.createElement("div", { className: "col-md-3" },
8255
- React.createElement(Select, { name: "day", disabled: rest.disabled, options: dayOptions, value: dateValues.day.toString(), onChange: (e) => handleChange('day', e.target.value), role: "day" })),
8290
+ React.createElement(Select, { name: `${name}-day`, disabled: rest.disabled, options: dayOptions, value: dateValues.day.toString(), onChange: (e) => handleChange('day', e.target.value), "aria-label": "Day" })),
8256
8291
  React.createElement("div", { className: "col-md-5" },
8257
- React.createElement(Select, { name: "month", disabled: rest.disabled, options: monthOptions, value: dateValues.month.toString(), onChange: (e) => handleChange('month', e.target.value), role: "month" })),
8292
+ React.createElement(Select, { name: `${name}-month`, disabled: rest.disabled, options: monthOptions, value: dateValues.month.toString(), onChange: (e) => handleChange('month', e.target.value), "aria-label": "Month" })),
8258
8293
  React.createElement("div", { className: "col-md-4" },
8259
- React.createElement(Select, { name: "year", disabled: rest.disabled, options: yearOptions, value: dateValues.year.toString(), onChange: (e) => handleChange('year', e.target.value), role: "year" }))),
8260
- floatingLabel && (React.createElement("label", { htmlFor: name },
8261
- label,
8262
- " ",
8263
- rest.required && React.createElement("span", { className: "text-danger" }, "*"))),
8264
- error && React.createElement("small", { className: "form-message" }, error)));
8294
+ React.createElement(Select, { name: `${name}-year`, disabled: rest.disabled, options: yearOptions, value: dateValues.year.toString(), onChange: (e) => handleChange('year', e.target.value), "aria-label": "Year" }))),
8295
+ floatingLabel && renderLabel,
8296
+ error && React.createElement("small", { className: "form-message text-danger" }, error)));
8265
8297
  });
8266
8298
  DropdownDatePicker.displayName = 'DropdownDatePicker';
8267
8299
 
@@ -8496,17 +8528,16 @@ const Radio = React.forwardRef(({ name, label, options, error, ...rest }, ref) =
8496
8528
  error && React.createElement("small", { className: "form-message" }, error)));
8497
8529
  });
8498
8530
 
8499
- const RadioButtonGroup = React.forwardRef(({ label, required, options, error, defaultValue, ...rest }, ref) => {
8500
- // Convert boolean value to string
8501
- const stringDefaultValue = typeof defaultValue === 'boolean' ? (defaultValue ? 'true' : 'false') : defaultValue;
8502
- return (React.createElement("div", null,
8503
- label && (React.createElement("label", null,
8531
+ const RadioButtonGroup = React.forwardRef(({ name, label, size = 'md', options, selectedValue, error, required, ...rest }, ref) => {
8532
+ return (React.createElement(React.Fragment, null,
8533
+ label && (React.createElement("label", { htmlFor: name },
8504
8534
  label,
8535
+ " ",
8505
8536
  required && React.createElement("span", { className: "text-danger" }, "*"))),
8506
- React.createElement("div", { className: "btn-group", "data-toggle": "buttons" }, options.map((option) => (React.createElement("label", { key: `${option.value}`, className: `btn auto btn-default btn-md ${option.value === stringDefaultValue ? 'active' : ''}` },
8507
- React.createElement("input", { type: "radio", ref: ref, value: option.value, defaultChecked: option.value === stringDefaultValue, ...rest }),
8537
+ React.createElement("div", { className: "btn-group", "data-toggle": "buttons" }, options.map((option) => (React.createElement("label", { key: option.value, className: `btn auto btn-default btn-${size} ${option.value === selectedValue ? 'active' : ''}` },
8538
+ React.createElement("input", { type: "radio", id: option.value.toString(), name: name, value: option.value, ref: ref, ...rest }),
8508
8539
  React.createElement("span", null, option.label))))),
8509
- error && React.createElement("small", { className: "form-message" }, error)));
8540
+ error !== undefined && React.createElement("small", { className: "form-message" }, error)));
8510
8541
  });
8511
8542
  RadioButtonGroup.displayName = 'RadioButtonGroup';
8512
8543