react-modern-calendar-datepicker icon indicating copy to clipboard operation
react-modern-calendar-datepicker copied to clipboard

Clear Value

Open AImani opened this issue 5 years ago • 4 comments

We need this button (remove icon) for clear selected date

clearDate

AImani avatar Jul 14 '20 23:07 AImani

Hi, It's quite easy to achieve that. You can render a custom input using renderInput prop and use a button in that, and by attaching an event listener to the button, you can clear the state containing the date picker value. Another way is to nest the <DatePicker /> component in a div element with position: relative;, and have the button as absolute in the div.

Kiarash-Z avatar Jul 15 '20 08:07 Kiarash-Z

@Kiarash-Z , there is a problem when the clear button clicked, the datepicker will appear again to select a date and won't disappear until click away. It would be nice to have destroy method to kill datepicker after remove button clicked.

seyyed-sina avatar May 27 '21 16:05 seyyed-sina

@Kiarash-Z , there is a problem when the clear button clicked, the datepicker will appear again to select a date and won't disappear until click away. It would be nice to have destroy method to kill datepicker after remove button clicked.

I tried to handle it via event bubbling but it didn't work.

pooridev avatar Dec 04 '22 09:12 pooridev

I used a code like this and it worked properly.

export default ({ value, onChange, ...otherProps}) => {
	const onClear = useCallback(() => {
		onChange(null);
	}, [onChange]);

	return (
		<div className="relative container">
			<DatePicker
				value={value}
				onChange={onChange}
				{...otherProps} />
			{value ? <CrossIcon onClick={onClear} className="clear-button" /> : null}
		</div>
	);
};
.container {
    .clear-button {
        position: absolute;
        margin-left: 8px;
        left: 0px;
        top: 50%;
        transform: translateY(-50%);

        z-index: 100; // because input's z-index is 100

        color: hsl(0,0%,80%);
        &:hover {
            color: hsl(0, 0%, 60%);
        }
    }
}

mohsenasm avatar Apr 15 '23 13:04 mohsenasm