@trackunit/react-form-components 0.0.404-alpha-4aa8d638390.0 → 0.0.408

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/index.cjs.js CHANGED
@@ -592,6 +592,48 @@ const CheckboxField = React.forwardRef((_a, ref) => {
592
592
  return (jsxRuntime.jsx(FormGroup, { className: "flex flex-col gap-1", dataTestId: dataTestId && `${dataTestId}-FormGroup`, disabled: rest.disabled, helpAddon: helpAddon, helpText: helpText, htmlFor: htmlForId, label: label, required: rest.required, tip: tip, children: jsxRuntime.jsx(Checkbox, Object.assign({ checked: checked, className: className, dataTestId: dataTestId, id: htmlForId, label: checkboxLabel, onChange: onChange, ref: ref }, rest)) }));
593
593
  });
594
594
 
595
+ /**
596
+ * Validates if the given value is a valid hex color.
597
+ *
598
+ * @param value - The string value to be validated.
599
+ * @returns True if the value is a valid hex color, otherwise false.
600
+ */
601
+ const isValidHEXColor = (value) => {
602
+ const hexRegex = /^#([0-9A-F]{6})$/i;
603
+ return hexRegex.test(value);
604
+ };
605
+ /**
606
+ * The ColorField component is used to enter color.
607
+ * ColorField validates that user enters a valid color address.
608
+ *
609
+ */
610
+ const ColorField = React.forwardRef((_a, ref) => {
611
+ var { label, id, tip, helpText, errorMessage, helpAddon, className, defaultValue, dataTestId, value: propValue, onChange, isInvalid = false } = _a, rest = __rest(_a, ["label", "id", "tip", "helpText", "errorMessage", "helpAddon", "className", "defaultValue", "dataTestId", "value", "onChange", "isInvalid"]);
612
+ const htmlForId = id ? id : "colorField-" + uuid.v4();
613
+ const innerRef = React__default["default"].useRef(null);
614
+ // eslint-disable-next-line local-rules/no-typescript-assertion
615
+ React__default["default"].useImperativeHandle(ref, () => innerRef.current, []);
616
+ // Internal state for color value
617
+ const [value, setValue] = React.useState(propValue || defaultValue || "");
618
+ const handleChange = React.useCallback((event) => {
619
+ const newValue = event.target.value;
620
+ setValue(newValue);
621
+ if (onChange) {
622
+ onChange(event);
623
+ }
624
+ }, [onChange]);
625
+ const renderAsInvalid = !!errorMessage || (value && typeof value === "string" && !isValidHEXColor(value)) || isInvalid;
626
+ return (jsxRuntime.jsx(FormGroup, { dataTestId: dataTestId && `${dataTestId}-FormGroup`, disabled: rest.disabled, helpAddon: helpAddon, helpText: (renderAsInvalid && errorMessage) || helpText, htmlFor: htmlForId, isInvalid: renderAsInvalid, label: label, required: rest.required, tip: tip, children: jsxRuntime.jsxs("div", { className: cvaInput({
627
+ disabled: false,
628
+ invalid: false,
629
+ className,
630
+ }), "data-testid": dataTestId && `${dataTestId}-container`, children: [jsxRuntime.jsx("input", { "aria-labelledby": htmlForId + "-label-text", className: cvaInputField({ disabled: false }), "data-testid": dataTestId, onChange: handleChange, type: "text", value: value }), jsxRuntime.jsx("input", { "aria-labelledby": htmlForId + "-label", className: "mr-1 h-[25px] w-[25px] self-center bg-inherit", "data-testid": dataTestId, defaultValue: defaultValue, id: htmlForId, onChange: handleChange, ref: innerRef, type: "color", value: value }), jsxRuntime.jsx(reactComponents.IconButton, { className: "mr-1 self-center", icon: jsxRuntime.jsx(reactComponents.Icon, { name: "Pencil", type: "outline" }), onClick: () => {
631
+ if (innerRef === null || innerRef === void 0 ? void 0 : innerRef.current) {
632
+ innerRef.current.click();
633
+ }
634
+ }, variant: "ghost-neutral" })] }) }));
635
+ });
636
+
595
637
  /**
596
638
  * A wrapper around BaseInput with a pop-up day picker.
597
639
  *
@@ -2325,6 +2367,7 @@ exports.ActionButton = ActionButton;
2325
2367
  exports.BaseInput = BaseInput;
2326
2368
  exports.Checkbox = Checkbox;
2327
2369
  exports.CheckboxField = CheckboxField;
2370
+ exports.ColorField = ColorField;
2328
2371
  exports.CreatableSelect = CreatableSelect;
2329
2372
  exports.CreatableSelectField = CreatableSelectField;
2330
2373
  exports.DateField = DateField;
@@ -2391,6 +2434,7 @@ exports.getPhoneNumberWithPlus = getPhoneNumberWithPlus;
2391
2434
  exports.isInvalidCountryCode = isInvalidCountryCode;
2392
2435
  exports.isInvalidPhoneNumber = isInvalidPhoneNumber;
2393
2436
  exports.isMultiValue = isMultiValue;
2437
+ exports.isValidHEXColor = isValidHEXColor;
2394
2438
  exports.parseSchedule = parseSchedule;
2395
2439
  exports.serializeSchedule = serializeSchedule;
2396
2440
  exports.useCustomComponents = useCustomComponents;
package/index.esm.js CHANGED
@@ -4,7 +4,7 @@ import { IconButton, Icon, Tooltip, Heading, Text, MenuItem, Tag, Spinner } from
4
4
  import { useCopyToClipboard } from 'react-use';
5
5
  import { cvaMerge } from '@trackunit/css-class-variance-utilities';
6
6
  import * as React from 'react';
7
- import React__default, { forwardRef, useState, useRef, useCallback, cloneElement, useEffect, useMemo, useImperativeHandle } from 'react';
7
+ import React__default, { forwardRef, useState, useCallback, useRef, cloneElement, useEffect, useMemo, useImperativeHandle } from 'react';
8
8
  import { v4 } from 'uuid';
9
9
  import { format } from 'date-fns';
10
10
  import parsePhoneNumberFromString, { isSupportedCountry, getCountries, getCountryCallingCode, AsYouType, parseIncompletePhoneNumber, isValidPhoneNumber } from 'libphonenumber-js';
@@ -562,6 +562,48 @@ const CheckboxField = forwardRef((_a, ref) => {
562
562
  return (jsx(FormGroup, { className: "flex flex-col gap-1", dataTestId: dataTestId && `${dataTestId}-FormGroup`, disabled: rest.disabled, helpAddon: helpAddon, helpText: helpText, htmlFor: htmlForId, label: label, required: rest.required, tip: tip, children: jsx(Checkbox, Object.assign({ checked: checked, className: className, dataTestId: dataTestId, id: htmlForId, label: checkboxLabel, onChange: onChange, ref: ref }, rest)) }));
563
563
  });
564
564
 
565
+ /**
566
+ * Validates if the given value is a valid hex color.
567
+ *
568
+ * @param value - The string value to be validated.
569
+ * @returns True if the value is a valid hex color, otherwise false.
570
+ */
571
+ const isValidHEXColor = (value) => {
572
+ const hexRegex = /^#([0-9A-F]{6})$/i;
573
+ return hexRegex.test(value);
574
+ };
575
+ /**
576
+ * The ColorField component is used to enter color.
577
+ * ColorField validates that user enters a valid color address.
578
+ *
579
+ */
580
+ const ColorField = forwardRef((_a, ref) => {
581
+ var { label, id, tip, helpText, errorMessage, helpAddon, className, defaultValue, dataTestId, value: propValue, onChange, isInvalid = false } = _a, rest = __rest(_a, ["label", "id", "tip", "helpText", "errorMessage", "helpAddon", "className", "defaultValue", "dataTestId", "value", "onChange", "isInvalid"]);
582
+ const htmlForId = id ? id : "colorField-" + v4();
583
+ const innerRef = React__default.useRef(null);
584
+ // eslint-disable-next-line local-rules/no-typescript-assertion
585
+ React__default.useImperativeHandle(ref, () => innerRef.current, []);
586
+ // Internal state for color value
587
+ const [value, setValue] = useState(propValue || defaultValue || "");
588
+ const handleChange = useCallback((event) => {
589
+ const newValue = event.target.value;
590
+ setValue(newValue);
591
+ if (onChange) {
592
+ onChange(event);
593
+ }
594
+ }, [onChange]);
595
+ const renderAsInvalid = !!errorMessage || (value && typeof value === "string" && !isValidHEXColor(value)) || isInvalid;
596
+ return (jsx(FormGroup, { dataTestId: dataTestId && `${dataTestId}-FormGroup`, disabled: rest.disabled, helpAddon: helpAddon, helpText: (renderAsInvalid && errorMessage) || helpText, htmlFor: htmlForId, isInvalid: renderAsInvalid, label: label, required: rest.required, tip: tip, children: jsxs("div", { className: cvaInput({
597
+ disabled: false,
598
+ invalid: false,
599
+ className,
600
+ }), "data-testid": dataTestId && `${dataTestId}-container`, children: [jsx("input", { "aria-labelledby": htmlForId + "-label-text", className: cvaInputField({ disabled: false }), "data-testid": dataTestId, onChange: handleChange, type: "text", value: value }), jsx("input", { "aria-labelledby": htmlForId + "-label", className: "mr-1 h-[25px] w-[25px] self-center bg-inherit", "data-testid": dataTestId, defaultValue: defaultValue, id: htmlForId, onChange: handleChange, ref: innerRef, type: "color", value: value }), jsx(IconButton, { className: "mr-1 self-center", icon: jsx(Icon, { name: "Pencil", type: "outline" }), onClick: () => {
601
+ if (innerRef === null || innerRef === void 0 ? void 0 : innerRef.current) {
602
+ innerRef.current.click();
603
+ }
604
+ }, variant: "ghost-neutral" })] }) }));
605
+ });
606
+
565
607
  /**
566
608
  * A wrapper around BaseInput with a pop-up day picker.
567
609
  *
@@ -2287,4 +2329,4 @@ const useZodValidators = () => {
2287
2329
  */
2288
2330
  setupLibraryTranslations();
2289
2331
 
2290
- export { ActionButton, BaseInput, Checkbox, CheckboxField, CreatableSelect, CreatableSelectField, DateField, DateInput, DropZone, EMAIL_REGEX, EmailField, EmailInput, FormFieldSelectAdapter, FormGroup, Label, MultiSelectMenuItem, NumberField, NumberInput, OptionCard, PasswordField, PasswordInput, PhoneField, PhoneFieldWithController, PhoneInput, RadioGroup, RadioItem, Schedule, ScheduleVariant, Search, Select, SelectField, SingleSelectMenuItem, TextArea, TextAreaField, TextField, TextInput, TimeRange, TimeRangeField, Toggle, UploadField, UploadInput, UrlField, UrlInput, checkIfPhoneNumberHasPlus, countryCodeToFlagEmoji, cvaActionButton, cvaActionContainer, cvaInput, cvaInputAddon, cvaInputAddonAfter, cvaInputAddonBefore, cvaInputBase, cvaInputBaseDisabled, cvaInputBaseInvalid, cvaInputField, cvaInputPrefix, cvaInputSuffix, cvaSelect, cvaSelectCounter, cvaSelectDynamicTagContainer, cvaSelectIcon, cvaSelectMenu, cvaSelectMenuList, cvaSelectPrefix, cvaSelectXIcon, getCountryAbbreviation, getOrderedOptions, getPhoneNumberWithPlus, isInvalidCountryCode, isInvalidPhoneNumber, isMultiValue, parseSchedule, serializeSchedule, useCustomComponents, useGetPhoneValidationRules, usePhoneInput, useZodValidators, validateEmailAddress, validatePhoneNumber, weekDay };
2332
+ export { ActionButton, BaseInput, Checkbox, CheckboxField, ColorField, CreatableSelect, CreatableSelectField, DateField, DateInput, DropZone, EMAIL_REGEX, EmailField, EmailInput, FormFieldSelectAdapter, FormGroup, Label, MultiSelectMenuItem, NumberField, NumberInput, OptionCard, PasswordField, PasswordInput, PhoneField, PhoneFieldWithController, PhoneInput, RadioGroup, RadioItem, Schedule, ScheduleVariant, Search, Select, SelectField, SingleSelectMenuItem, TextArea, TextAreaField, TextField, TextInput, TimeRange, TimeRangeField, Toggle, UploadField, UploadInput, UrlField, UrlInput, checkIfPhoneNumberHasPlus, countryCodeToFlagEmoji, cvaActionButton, cvaActionContainer, cvaInput, cvaInputAddon, cvaInputAddonAfter, cvaInputAddonBefore, cvaInputBase, cvaInputBaseDisabled, cvaInputBaseInvalid, cvaInputField, cvaInputPrefix, cvaInputSuffix, cvaSelect, cvaSelectCounter, cvaSelectDynamicTagContainer, cvaSelectIcon, cvaSelectMenu, cvaSelectMenuList, cvaSelectPrefix, cvaSelectXIcon, getCountryAbbreviation, getOrderedOptions, getPhoneNumberWithPlus, isInvalidCountryCode, isInvalidPhoneNumber, isMultiValue, isValidHEXColor, parseSchedule, serializeSchedule, useCustomComponents, useGetPhoneValidationRules, usePhoneInput, useZodValidators, validateEmailAddress, validatePhoneNumber, weekDay };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-form-components",
3
- "version": "0.0.404-alpha-4aa8d638390.0",
3
+ "version": "0.0.408",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -0,0 +1,24 @@
1
+ import React from "react";
2
+ import { BaseInputProps } from "../BaseInput";
3
+ import { FormGroupProps } from "../FormGroup/FormGroup";
4
+ type FormGroupExposedProps = Pick<FormGroupProps, "label" | "tip" | "helpText" | "helpAddon">;
5
+ /**
6
+ * Validates if the given value is a valid hex color.
7
+ *
8
+ * @param value - The string value to be validated.
9
+ * @returns True if the value is a valid hex color, otherwise false.
10
+ */
11
+ export declare const isValidHEXColor: (value: string) => boolean;
12
+ export interface ColorFieldProps extends FormGroupExposedProps, BaseInputProps {
13
+ /**
14
+ * If a value is set, the field is rendered in its invalid state.
15
+ */
16
+ errorMessage?: string;
17
+ }
18
+ /**
19
+ * The ColorField component is used to enter color.
20
+ * ColorField validates that user enters a valid color address.
21
+ *
22
+ */
23
+ export declare const ColorField: React.ForwardRefExoticComponent<ColorFieldProps & React.RefAttributes<HTMLInputElement>>;
24
+ export {};
package/src/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./components/ActionButton";
2
2
  export * from "./components/BaseInput";
3
3
  export * from "./components/Checkbox";
4
4
  export * from "./components/CheckboxField/CheckboxField";
5
+ export * from "./components/ColorField/ColorField";
5
6
  export * from "./components/DateField/DateField";
6
7
  export * from "./components/DateInput/DateInput";
7
8
  export * from "./components/DropZone/DropZone";