diff --git a/example/index.tsx b/example/index.tsx index eeae802..01f70e5 100644 --- a/example/index.tsx +++ b/example/index.tsx @@ -54,8 +54,8 @@ const App = () => { const { colorMode, toggleColorMode } = useColorMode(); const [date, setDate] = useState(demoDate); const [selectedDates, setSelectedDates] = useState([ - new Date(), - new Date(), + // new Date(), + // new Date(), ]); const [firstDayOfWeek, setFirstDayOfWeek] = useState(1); const [isSingleChecked, setSingleCheck] = useState(true); @@ -103,6 +103,14 @@ const App = () => { flexDirection={['column', 'column', 'row']} > + + Placeholder + { + + + <> +
+ Using placeholder on button trigger + +
+
+ Don't fill missing end range + +
+ +
+ Using placeholder on input + +
+ +
+ Don't fill missing end range + +
+ +
+
+ @@ -394,6 +467,7 @@ const App = () => { dayNames: 'abcdefg'.split(''), // length of 7 monthNames: 'ABCDEFGHIJKL'.split(''), // length of 12 }} + placeholder='Click to select a range date' /> diff --git a/src/range.tsx b/src/range.tsx index af6dd72..26ed5e7 100644 --- a/src/range.tsx +++ b/src/range.tsx @@ -96,6 +96,8 @@ interface RangeProps extends DatepickerProps { name?: string; usePortal?: boolean; portalRef?: React.MutableRefObject; + placeholder?: string; + fillEmpty?: boolean; } export type RangeDatepickerProps = RangeProps & VariantProps; @@ -108,7 +110,8 @@ const DefaultConfigs: Required = { monthsToDisplay: 2, }; -const defaultProps = { +const defaultProps: Partial = { + fillEmpty: true, defaultIsOpen: false, closeOnSelect: true, triggerVariant: 'default' as const, @@ -132,6 +135,8 @@ export const RangeDatepicker: React.FC = (props) => { disabled, children, triggerVariant, + placeholder, + fillEmpty } = mergedProps; // chakra popover utils @@ -186,13 +191,20 @@ export const RangeDatepicker: React.FC = (props) => { } }; - // eventually we want to allow user to freely type their own input and parse the input - let intVal = selectedDates[0] - ? `${format(selectedDates[0], datepickerConfigs.dateFormat)}` - : `${datepickerConfigs.dateFormat}`; - intVal += selectedDates[1] - ? ` - ${format(selectedDates[1], datepickerConfigs.dateFormat)}` - : ` - ${datepickerConfigs.dateFormat}`; + let intVal = null; + + if (selectedDates?.filter(x => x).length) { + // eventually we want to allow user to freely type their own input and parse the input + intVal = selectedDates[0] + ? `${format(selectedDates[0], datepickerConfigs.dateFormat)}` + : `${datepickerConfigs.dateFormat}`; + + intVal += selectedDates[1] + ? ` - ${format(selectedDates[1], datepickerConfigs.dateFormat)}` + : fillEmpty + ? ` - ${datepickerConfigs.dateFormat}` + : ''; + } const PopoverContentWrapper = usePortal ? Portal : React.Fragment; @@ -217,7 +229,10 @@ export const RangeDatepicker: React.FC = (props) => { disabled={disabled} {...propsConfigs?.triggerBtnProps} > - {intVal} + { + // Use placehold or empty string as default value + intVal ?? placeholder + } ) : null} @@ -237,6 +252,7 @@ export const RangeDatepicker: React.FC = (props) => { paddingRight={'2.5rem'} isDisabled={disabled} name={name} + placeholder={placeholder} value={intVal} onChange={(e) => e.target.value} {...propsConfigs?.inputProps}