All files / src/components/RelativeDateField/hooks useRelativeDateFieldProps.ts

100% Statements 7/7
92.85% Branches 13/14
100% Functions 1/1
100% Lines 7/7

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 757x                                       74x 74x   74x 4x 4x     74x                                                                                            
import React from 'react';
 
import type {TextInputProps} from '@gravity-ui/uikit';
 
import type {CalendarProps} from '../../Calendar';
import type {DateFieldProps} from '../../DateField';
import type {RelativeDateFieldProps} from '../RelativeDateField';
 
import type {RelativeDateFieldState} from './useRelativeDateFieldState';
 
interface RelativeDateProps {
    inputProps: TextInputProps;
    calendarProps: CalendarProps;
    timeInputProps: DateFieldProps;
}
 
export function useRelativeDateFieldProps(
    state: RelativeDateFieldState,
    props: RelativeDateFieldProps,
): RelativeDateProps {
    const [prevCorrectDate, setPrevCorrectDate] = React.useState(state.lastCorrectDate);
    const [focusedDate, setFocusedDate] = React.useState(state.lastCorrectDate);
 
    if (prevCorrectDate !== state.lastCorrectDate) {
        setPrevCorrectDate(state.lastCorrectDate);
        setFocusedDate(state.lastCorrectDate);
    }
 
    return {
        inputProps: {
            size: props.size,
            autoFocus: props.autoFocus,
            value: state.text,
            onUpdate: state.setText,
            disabled: state.disabled,
            hasClear: props.hasClear,
            validationState: state.validationState,
            errorMessage: props.errorMessage,
            errorPlacement: props.errorPlacement,
            label: props.label,
            id: props.id,
            startContent: props.startContent,
            endContent: props.endContent,
            pin: props.pin,
            view: props.view,
            placeholder: props.placeholder,
            onKeyDown: props.onKeyDown,
            onKeyUp: props.onKeyUp,
            onBlur: props.onBlur,
            onFocus: props.onFocus,
            controlProps: {
                'aria-label': props['aria-label'] || undefined,
                'aria-labelledby': props['aria-labelledby'] || undefined,
                'aria-describedby': props['aria-describedby'] || undefined,
                'aria-details': props['aria-details'] || undefined,
                'aria-disabled': state.disabled || undefined,
                readOnly: state.readOnly,
            },
        },
        calendarProps: {
            size: props.size === 's' ? 'm' : props.size,
            readOnly: true,
            value: state.parsedDate,
            focusedValue: focusedDate,
            onFocusUpdate: setFocusedDate,
        },
        timeInputProps: {
            size: props.size,
            readOnly: true,
            value: state.lastCorrectDate,
            format: 'LTS',
        },
    };
}