File size: 4,410 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { FormLabel } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import { FunctionComponent, useRef, useCallback } from 'react';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormTextInput from 'calypso/components/forms/form-text-input';
import { useLocalizedMoment } from 'calypso/components/localized-moment';

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};

type StartOrEnd = 'Start' | 'End';

interface Props {
	startDateValue: string;
	endDateValue: string;
	startLabel: string | null | undefined;
	endLabel: string | null | undefined;
	onInputFocus: ( value: string | null | undefined, startOrEnd: StartOrEnd ) => void;
	onInputBlur: ( value: string | null | undefined, startOrEnd: StartOrEnd ) => void;
	onInputChange: ( value: string | null | undefined, startOrEnd: StartOrEnd ) => void;
}

const DateRangeInputs: FunctionComponent< Props > = ( {
	onInputFocus = noop,
	onInputBlur = noop,
	onInputChange = noop,
	...props
} ) => {
	const uniqueIdRef = useRef( crypto.randomUUID() );
	const uniqueId = uniqueIdRef.current;
	const translate = useTranslate();

	const startDateID = `startDate-${ uniqueId }`;
	const endDateID = `endDate-${ uniqueId }`;

	/**
	 * Handles input focus events with fixed arguments
	 * for consistency via partial application
	 * @param  startOrEnd one of "Start" or "End"
	 * @returns the partially applied function ready to receive event data
	 */
	const handleInputFocus = useCallback(
		( startOrEnd: StartOrEnd ) => ( e: Event ) => {
			const { value } = e.target as HTMLInputElement;
			onInputFocus( value, startOrEnd );
		},
		[ onInputFocus ]
	);

	/**
	 * Handles input blur events with fixed arguments
	 * for consistency via partial application
	 * @param  startOrEnd one of "Start" or "End"
	 * @returns the partially applied function ready to receive event data
	 */
	const handleInputBlur = useCallback(
		( startOrEnd: StartOrEnd ) => ( e: Event ) => {
			const { value } = e.target as HTMLInputElement;
			onInputBlur( value, startOrEnd );
		},
		[ onInputBlur ]
	);

	/**
	 * Handles input change events with fixed arguments
	 * for consistency via partial application
	 * @param  startOrEnd one of "Start" or "End"
	 * @returns the partially applied function ready to receive event data
	 */
	const handleInputChange = useCallback(
		( startOrEnd: StartOrEnd ) => ( e: Event ) => {
			const { value } = e.target as HTMLInputElement;
			onInputChange( value, startOrEnd );
		},
		[ onInputChange ]
	);

	const moment = useLocalizedMoment();

	// => "MM/DD/YYYY" (or locale equivalent)
	const localeDateFormat = moment.localeData().longDateFormat( 'L' );

	// If we haven't received a actual date then don't show anything and utilise the placeholder
	// as it is supposed to be used
	const startValue = props.startDateValue !== localeDateFormat ? props.startDateValue : '';
	const endValue = props.endDateValue !== localeDateFormat ? props.endDateValue : '';

	return (
		<FormFieldset className="date-range__date-inputs">
			<legend className="date-range__date-inputs-legend">Start and End Dates</legend>
			<div className="date-range__date-inputs-inner">
				<div className="date-range__date-input date-range__date-input--from">
					<FormLabel htmlFor={ startDateID }>
						{ props.startLabel ||
							translate( 'From', {
								comment: 'DateRange text input label for the start of the date range',
							} ) }
					</FormLabel>
					<FormTextInput
						id={ startDateID }
						name={ startDateID }
						value={ startValue }
						onChange={ handleInputChange( 'Start' ) }
						onBlur={ handleInputBlur( 'Start' ) }
						onFocus={ handleInputFocus( 'Start' ) }
						placeholder={ localeDateFormat }
					/>
				</div>
				<div className="date-range__date-input date-range__date-input--to">
					<FormLabel htmlFor={ endDateID }>
						{ props.startLabel ||
							translate( 'To', {
								comment: 'DateRange text input label for the end of the date range',
							} ) }
					</FormLabel>
					<FormTextInput
						id={ endDateID }
						name={ endDateID }
						value={ endValue }
						onChange={ handleInputChange( 'End' ) }
						onBlur={ handleInputBlur( 'End' ) }
						onFocus={ handleInputFocus( 'End' ) }
						placeholder={ localeDateFormat }
					/>
				</div>
			</div>
		</FormFieldset>
	);
};

export default DateRangeInputs;