diff --git a/src/components/input/CheckboxInput.tsx b/src/components/input/CheckboxInput.tsx index c3c5be0a..fb0c95c7 100644 --- a/src/components/input/CheckboxInput.tsx +++ b/src/components/input/CheckboxInput.tsx @@ -1,279 +1,87 @@ 'use client'; -import { ChangeEventHandler, FocusEventHandler, ReactNode, useId } from 'react'; - +import { HTMLProps, useEffect, useRef } from 'react'; import { cn } from '@/lib/helper'; -import FieldMessage from '@/components/helper/FieldMessage'; -export interface CheckboxInputProps { - // Basic Props +interface CheckboxInputProps extends HTMLProps { name: string; label?: string; - bottomLabel?: string; - checked?: boolean; - value?: string | number; indeterminate?: boolean; - naked?: boolean; // New prop for checkbox-only mode - - // Styling Props - className?: { + classNames?: { wrapper?: string; - label?: string; + inputWrapper?: string; checkbox?: string; - input?: string; + label?: string; }; - - // State Props isError?: boolean; isValid?: boolean; errorMessage?: string; - disabled?: boolean; - readOnly?: boolean; - required?: boolean; - isLoading?: boolean; - - // Adornment Props - startAdornment?: ReactNode; - endAdornment?: ReactNode; - - // Event Handlers - onChange?: ChangeEventHandler; - onBlur?: FocusEventHandler; - onFocus?: FocusEventHandler; - - // Additional Props - tooltip?: string; - description?: string; - size?: 'sm' | 'md' | 'lg'; - variant?: - | 'default' - | 'primary' - | 'secondary' - | 'success' - | 'warning' - | 'info' - | 'error'; } const CheckboxInput = ({ + indeterminate, name, label, - bottomLabel, - checked = false, - value, - indeterminate = false, - naked = false, className, + classNames, + isValid, isError, errorMessage, - disabled = false, - readOnly = false, - required = false, - isLoading = false, - startAdornment, - endAdornment, - onChange, - onBlur, - onFocus, - tooltip, - description, - size = 'md', - variant = 'default', + ...rest }: CheckboxInputProps) => { - const showErrorMessage = Boolean(isError && errorMessage); - const feedbackMessage = showErrorMessage ? errorMessage : bottomLabel; + const ref = useRef(null!); - // Size classes - const sizeClasses = { - sm: 'checkbox-sm', - md: 'checkbox-md', - lg: 'checkbox-lg', - }; - - // Variant classes - const variantClasses = { - default: '', - primary: 'checkbox-primary', - secondary: 'checkbox-secondary', - success: 'checkbox-success', - warning: 'checkbox-warning', - info: 'checkbox-info', - error: 'checkbox-error', - }; - - // Generate unique ID for accessibility using React's useId hook for SSR compatibility - const generatedId = useId(); - const checkboxId = `checkbox-${name}-${generatedId}`; - - // Naked mode - only checkbox, no wrapper structure - if (naked) { - return ( -
- { - if (input) { - input.indeterminate = indeterminate; - } - }} - /> - - {/* Loading State */} - {isLoading && ( -
- -
- )} -
- ); - } + useEffect(() => { + if (typeof indeterminate === 'boolean') { + ref.current.indeterminate = !rest.checked && indeterminate; + } + }, [ref, indeterminate]); return (
- {/* Label with Tooltip Support */} - {label && ( -
- - )} - - {/* Description */} - {description && ( -

- {description} -

- )} - - {/* Error Message or Bottom Label */} - {!isError && bottomLabel && ( -

{bottomLabel}

- )} - {isError && errorMessage && ( -

{errorMessage}

- )} + {errorMessage && {errorMessage}} ); }; diff --git a/src/components/pages/inventory/movement/form/MovementForm.tsx b/src/components/pages/inventory/movement/form/MovementForm.tsx index 711b7d20..d2c91168 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.tsx +++ b/src/components/pages/inventory/movement/form/MovementForm.tsx @@ -847,7 +847,9 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { selectedProducts.length && formik.values.products?.length > 0 } - onChange={(e) => { + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedProducts( formik.values.products?.map( @@ -858,8 +860,10 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { setSelectedProducts([]); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} /> @@ -890,11 +894,13 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { {type !== 'detail' && ( -
+
{ + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedProducts([ ...selectedProducts, @@ -906,8 +912,10 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { ); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1061,7 +1069,9 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { selectedDeliveries.length && formik.values.deliveries?.length > 0 } - onChange={(e) => { + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedDeliveries( formik.values.deliveries?.map( @@ -1072,8 +1082,10 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { setSelectedDeliveries([]); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1150,11 +1162,13 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { {type !== 'detail' && ( -
+
{ + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedDeliveries([ ...selectedDeliveries, @@ -1168,8 +1182,10 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { ); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index c654f2ba..2735a7e9 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -760,29 +760,30 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { {type !== 'detail' && ( -
- 0 + 0 + } + onChange={( + e: React.ChangeEvent + ) => { + if (e.target.checked) { + setSelectedFeed( + formik.values.feed_data?.map((_, idx) => idx) ?? + [] + ); + } else { + setSelectedFeed([]); } - onChange={(e) => { - if (e.target.checked) { - setSelectedFeed( - formik.values.feed_data?.map( - (_, idx) => idx - ) ?? [] - ); - } else { - setSelectedFeed([]); - } - }} - naked={true} - size='sm' - /> -
+ }} + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} + /> )} @@ -812,11 +813,13 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { {type !== 'detail' && ( -
+
{ + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedFeed([...selectedFeed, idx]); } else { @@ -825,8 +828,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { ); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1007,7 +1012,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { selectedWeight.length && formik.values.body_weight?.length > 0 } - onChange={(e) => { + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedWeight( formik.values.body_weight?.map( @@ -1018,8 +1025,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { setSelectedWeight([]); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1059,11 +1068,13 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { {type !== 'detail' && ( -
+
{ + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedWeight([...selectedWeight, idx]); } else { @@ -1072,8 +1083,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { ); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1248,7 +1261,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { selectedVaccine.length && formik.values.vaccination?.length > 0 } - onChange={(e) => { + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedVaccine( formik.values.vaccination?.map( @@ -1259,8 +1274,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { setSelectedVaccine([]); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1292,11 +1309,13 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { {type !== 'detail' && ( -
+
{ + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedVaccine([...selectedVaccine, idx]); } else { @@ -1305,8 +1324,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { ); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1503,7 +1524,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { selectedMortality.length && formik.values.mortality?.length > 0 } - onChange={(e) => { + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedMortality( formik.values.mortality?.map( @@ -1514,8 +1537,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { setSelectedMortality([]); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />
@@ -1546,11 +1571,13 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { {type !== 'detail' && ( -
+
{ + onChange={( + e: React.ChangeEvent + ) => { if (e.target.checked) { setSelectedMortality([ ...selectedMortality, @@ -1562,8 +1589,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { ); } }} - naked={true} - size='sm' + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} />