Skip to content

Commit ec11736

Browse files
mofojedmattrunyon
andauthored
fix: A few small cleanups for DateTimeInput (#2062)
- Don't allow an undefined to be returned - Export more types --------- Co-authored-by: Matthew Runyon <mattrunyonstuff@gmail.com>
1 parent 3f45f07 commit ec11736

2 files changed

Lines changed: 59 additions & 58 deletions

File tree

packages/components/src/DateTimeInput.tsx

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const DATE_VALUE_STRING = '2022-01-01';
1818
const DEFAULT_VALUE_STRING = `${DATE_VALUE_STRING} 00:00:00.000000000`;
1919
const FULL_DATE_FORMAT = 'YYYY-MM-DD HH:MM:SS.SSSSSSSSS';
2020

21-
type DateTimeInputProps = {
21+
export type DateTimeInputProps = {
2222
className?: string;
23-
onChange?: (value?: string) => void;
23+
onChange?: (value: string) => void;
2424
defaultValue?: string;
2525
onFocus?: () => void;
2626
onBlur?: () => void;
@@ -43,64 +43,65 @@ function removeSeparators(value: string): string {
4343

4444
const EXAMPLES = [addSeparators(DEFAULT_VALUE_STRING)];
4545

46-
const DateTimeInput = React.forwardRef<HTMLInputElement, DateTimeInputProps>(
47-
(props: DateTimeInputProps, ref) => {
48-
const {
49-
className = '',
50-
onChange = () => undefined,
51-
defaultValue = '',
52-
onFocus = () => undefined,
53-
onBlur = () => undefined,
54-
onSubmit,
55-
'data-testid': dataTestId,
56-
} = props;
57-
const [value, setValue] = useState(
58-
defaultValue.length > 0 ? addSeparators(defaultValue) : ''
59-
);
60-
const [selection, setSelection] = useState<SelectionSegment>();
46+
export const DateTimeInput = React.forwardRef<
47+
HTMLInputElement,
48+
DateTimeInputProps
49+
>((props, ref) => {
50+
const {
51+
className = '',
52+
onChange = () => undefined,
53+
defaultValue = '',
54+
onFocus = () => undefined,
55+
onBlur = () => undefined,
56+
onSubmit,
57+
'data-testid': dataTestId,
58+
} = props;
59+
const [value, setValue] = useState(
60+
defaultValue.length > 0 ? addSeparators(defaultValue) : ''
61+
);
62+
const [selection, setSelection] = useState<SelectionSegment>();
6163

62-
const handleChange = useCallback(
63-
(newValue: string): void => {
64-
log.debug('handleChange', newValue);
65-
setValue(newValue);
66-
onChange(fixIncompleteValue(removeSeparators(newValue)));
67-
},
68-
[onChange]
69-
);
64+
const handleChange = useCallback(
65+
(newValue: string): void => {
66+
log.debug('handleChange', newValue);
67+
setValue(newValue);
68+
onChange(fixIncompleteValue(removeSeparators(newValue)));
69+
},
70+
[onChange]
71+
);
7072

71-
const handleBlur = useCallback((): void => {
72-
const prevValue = removeSeparators(value);
73-
const fixedValue = fixIncompleteValue(prevValue);
74-
// Update the value displayed in the input
75-
// onChange with the fixed value already triggered in handleChange
76-
if (fixedValue !== prevValue) {
77-
setValue(addSeparators(fixedValue));
78-
}
79-
onBlur();
80-
}, [value, onBlur]);
73+
const handleBlur = useCallback((): void => {
74+
const prevValue = removeSeparators(value);
75+
const fixedValue = fixIncompleteValue(prevValue);
76+
// Update the value displayed in the input
77+
// onChange with the fixed value already triggered in handleChange
78+
if (fixedValue !== prevValue) {
79+
setValue(addSeparators(fixedValue));
80+
}
81+
onBlur();
82+
}, [value, onBlur]);
8183

82-
return (
83-
<div className="d-flex flex-row align-items-center">
84-
<MaskedInput
85-
ref={ref}
86-
className={classNames(className)}
87-
example={EXAMPLES}
88-
getNextSegmentValue={getNextSegmentValue}
89-
onChange={handleChange}
90-
onSelect={setSelection}
91-
onSubmit={onSubmit}
92-
pattern={FULL_DATE_PATTERN}
93-
placeholder={FULL_DATE_FORMAT}
94-
selection={selection}
95-
value={value}
96-
onFocus={onFocus}
97-
onBlur={handleBlur}
98-
data-testid={dataTestId}
99-
/>
100-
</div>
101-
);
102-
}
103-
);
84+
return (
85+
<div className="d-flex flex-row align-items-center">
86+
<MaskedInput
87+
ref={ref}
88+
className={classNames(className)}
89+
example={EXAMPLES}
90+
getNextSegmentValue={getNextSegmentValue}
91+
onChange={handleChange}
92+
onSelect={setSelection}
93+
onSubmit={onSubmit}
94+
pattern={FULL_DATE_PATTERN}
95+
placeholder={FULL_DATE_FORMAT}
96+
selection={selection}
97+
value={value}
98+
onFocus={onFocus}
99+
onBlur={handleBlur}
100+
data-testid={dataTestId}
101+
/>
102+
</div>
103+
);
104+
});
104105

105106
DateTimeInput.displayName = 'DateTimeInput';
106107

packages/components/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export { default as Checkbox } from './Checkbox';
1212
export { default as ComboBox } from './ComboBox';
1313
export { default as CopyButton } from './CopyButton';
1414
export { default as CustomTimeSelect } from './CustomTimeSelect';
15-
export { default as DateTimeInput } from './DateTimeInput';
15+
export * from './DateTimeInput';
1616
export { default as DateInput } from './DateInput';
1717
export { default as DebouncedSearchInput } from './DebouncedSearchInput';
1818
export * from './dialogs';

0 commit comments

Comments
 (0)