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

feat: date-input on-blur #596

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/components/form/date-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ interface InputProp extends InputPropsType {
type: string;
}

export interface DateInputProps extends React.InputHTMLAttributes<HTMLTextAreaElement>, Pick<InputComponent, 'error'> {
export interface DateInputProps
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'value'>,
Pick<InputComponent, 'error'> {
/**
* Input divider
*/
Expand All @@ -39,14 +41,18 @@ export interface DateInputProps extends React.InputHTMLAttributes<HTMLTextAreaEl
* Inputs config
* */
inputs: InputProp[];
/**
* Callback fired when the input is blurred
* */
onBlur?: React.FocusEventHandler<HTMLInputElement>;
/**
* Callback fired when the value changes
* */
onChange(newValue): void;
/**
* The value of the input
* */
value: string;
value?: string | Date | null;
/**
* The props used for wrapper component
* */
Expand All @@ -55,7 +61,7 @@ export interface DateInputProps extends React.InputHTMLAttributes<HTMLTextAreaEl

export const DateInput = React.forwardRef<HTMLDivElement, DateInputProps>(
(
{ inputs, disabled, divider, wrapperProps, value, onChange, className, error }: DateInputProps,
{ inputs, disabled, divider, wrapperProps, value, onChange, onBlur, className, error }: DateInputProps,
ref,
): JSX.Element => {
const theme = useTheme();
Expand Down Expand Up @@ -119,6 +125,17 @@ export const DateInput = React.forwardRef<HTMLDivElement, DateInputProps>(
handleFocusNextInput(value, index);
};

const handleBlur = (e: React.FocusEvent<HTMLInputElement>): void => {
if (
onBlur &&
inputToRef[0].current !== e.relatedTarget &&
inputToRef[1].current !== e.relatedTarget &&
inputToRef[2].current !== e.relatedTarget
) {
onBlur(e);
}
};

return (
<StyledDayPicker className={cx('dateInput', error && 'error', className)} ref={ref} {...wrapperProps}>
<div className='dateInputWrapper'>
Expand All @@ -129,11 +146,11 @@ export const DateInput = React.forwardRef<HTMLDivElement, DateInputProps>(
return (
<div className='dateInputItem' key={`text-input-${input.type}`}>
<TextInput
id={`text-input-${input.type}`}
key={`text-input-${input.type}-input`}
onChange={(e): void => {
handleChange(e, index, input.type);
}}
onBlur={handleBlur}
disabled={disabled}
inputRef={inputToRef[index]}
title={input.title}
Expand Down
7 changes: 5 additions & 2 deletions stories/DateInput/DateInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ export default {
type: 'date',
},
},
onBlur: {
control: { disable: true },
},
},
} as ComponentMeta<typeof DateInput>;

const Template: ComponentStory<typeof DateInput> = (args) => {
const [value, setValue] = React.useState<Date | null>(null);
const [error, setError] = React.useState<string | null>(null);
const [error, setError] = React.useState<string | undefined>(undefined);

const handleChange = (v) => {
if (isValid(v)) {
setError(null);
setError(undefined);
} else {
setError('Invalid date');
}
Expand Down