All files / src/components/pages/create-group/fields/date-field index.tsx

0% Statements 0/55
0% Branches 0/1
0% Functions 0/1
0% Lines 0/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
'use client';

import { AnyFieldApi } from '@tanstack/react-form';
import clsx from 'clsx';

import { Icon } from '@/components/icon';
import { DatePickerModal } from '@/components/pages/create-group/modals/date-picker-modal';
import { Hint, Label } from '@/components/ui';
import { useModal } from '@/components/ui';
import { formatDateTime } from '@/lib/formatDateTime';

interface Props {
  field: AnyFieldApi;
}

export const GroupDateField = ({ field }: Props) => {
  const { open } = useModal();

  const hasValue = Boolean(field.state.value);
  const formattedDate = formatDateTime(field.state.value);
  const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid;

  const onInputClick = () => {
    open(<DatePickerModal dateField={field} />);
  };

  return (
    <div className='mt-6 flex w-full flex-col gap-1'>
      <Label htmlFor='create-group-date' required onClick={onInputClick}>
        모임 날짜
      </Label>
      <button
        className='bg-mono-white focus:border-mint-500 relative cursor-pointer rounded-2xl border border-gray-300 p-4 pl-11 focus:outline-none'
        type='button'
        onClick={onInputClick}
      >
        <Icon
          id='calendar-1'
          width={20}
          className='pointer-events-none absolute top-0 left-4 flex h-full items-center text-gray-500'
          height={20}
        />
        <p
          className={clsx(
            'text-text-md-medium text-left text-gray-500',
            hasValue && 'text-gray-800',
          )}
        >
          {hasValue ? formattedDate : '날짜와 시간을 선택해주세요'}
        </p>
      </button>
      {isInvalid && <Hint className='mt-0.5' message={field.state.meta.errors[0].message} />}
    </div>
  );
};