Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] selectedDateState as optional prop for DatePicker to ease implementation when selectedDateState is handled by ancestor #5

Merged
merged 1 commit into from
Feb 28, 2023

Conversation

gaabgonca
Copy link

Add selectedDateState prop to DatePicker and DatePickerProvider to handle selectedDate state from outside DatePicker component.

The absence of selectedDateState keeps the previously defined behavior, it's entirely optional.

This allows an easier implementation when selectedDateState is being handled by an ancestor component of the DatePicker. For instance, you have a form with a Date field and you need to provide the initial value based on fetched data. Putting that Date inside options prop can get messy and lead to unnecessary rebuilds or inconsistencies.

and DatePickerProvider to handle selectedDate
from outside DatePicker
@gaabgonca gaabgonca changed the title [ADD] selectedDateState as optional prop for DatePicker to ease implementation when selectedDateState is handled by ancestor [ADD] selectedDateState as optional prop for DatePicker to ease implementation when selectedDateState is handled by ancestor Jan 29, 2023
@OMikkel
Copy link
Owner

OMikkel commented Feb 27, 2023

Hey,

Appreciate the pull request, have you tested it?

I am currently living abroad and unable to test this PR.

If it is tested and works as intended I am happy to merge this request 😃

@gaabgonca
Copy link
Author

I'm currently using the changes proposed in a project without any issue

const options = {
  autoHide: true,
  todayBtn: true,
  clearBtn: true,
  maxDate: new Date('2030-01-01'),
  minDate: new Date('2023-01-01'),
  theme: {
    background: 'bg-white',
    todayBtn: '',
    clearBtn: '',
    icons: '',
    text: '',
    disabledText: '',
    input: 'w-72 place-self-end',
    inputIcon: '',
    selected: '',
  },
  icons: {
    prev: () => <span>{'<'}</span>,
    next: () => <span>{'>'}</span>,
  },
  datepickerClassNames: 'relative w-72 top-2 p-t-0',
  language: 'es',
}

export default function CustomDatePicker({}) {
  const [currentEvent, setCurrentEvent] = useContext(CurrentEventContext)

  const [show, setShow] = useState<boolean>(false)

  const handleClose = (state: boolean) => {
    setShow(state)
  }

  const setEventDate = (date: Date) => {
    const cleanDateSeconds: number =
      new Date(
        date.getFullYear(),
        date.getMonth(),
        date.getDate(),
        0,
        0,
        0,
        0,
      ).getTime() / 1000

    setCurrentEvent({
      ...currentEvent,
      eventDate: new Timestamp(cleanDateSeconds, 0),
    })
  }

  return (
    <div className="max-w-min">
      <DatePicker
        options={options}
        show={show}
        setShow={handleClose}
        selectedDateState={[currentEvent.eventDate.toDate(), setEventDate]}
      ></DatePicker>
    </div>
  )
}

It looks like this on the initial render with fetched data and behaves as expected upon interaction

image

@OMikkel OMikkel merged commit 5d95694 into OMikkel:master Feb 28, 2023
@OMikkel
Copy link
Owner

OMikkel commented Feb 28, 2023

Looks good the changes have been merged 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants