-
Notifications
You must be signed in to change notification settings - Fork 833
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
Improve onError validation #1730
Changes from 1 commit
5563142
70b66d4
e9a0687
4f2207f
868358a
679ea8d
a501984
a1a6b42
5ca8ef2
3af9888
71981e0
cbe5fc4
3eddd8b
f21609c
754a95b
58f12c7
e349496
5bce1e4
e4166a7
860d6f5
c73d4e7
8dedc04
66eb022
af381ef
1201eef
79cea16
806b63c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,37 +1,31 @@ | ||||||
import * as React from 'react'; | ||||||
import { MaterialUiPickersDate } from '../typings/date'; | ||||||
import { BasePickerProps } from '../typings/BasePicker'; | ||||||
import { useUtils } from '../_shared/hooks/useUtils'; | ||||||
import { MobileWrapper } from '../wrappers/MobileWrapper'; | ||||||
import { DateRangeInputProps } from './DateRangePickerInput'; | ||||||
import { parsePickerInputValue } from '../_helpers/date-utils'; | ||||||
import { usePickerState } from '../_shared/hooks/usePickerState'; | ||||||
import { useParsedDate } from '../_shared/hooks/date-helpers-hooks'; | ||||||
import { DesktopPopperWrapper } from '../wrappers/DesktopPopperWrapper'; | ||||||
import { MuiPickersAdapter, useUtils } from '../_shared/hooks/useUtils'; | ||||||
import { makeWrapperComponent } from '../wrappers/makeWrapperComponent'; | ||||||
import { ResponsivePopperWrapper } from '../wrappers/ResponsiveWrapper'; | ||||||
import { defaultMinDate, defaultMaxDate } from '../constants/prop-types'; | ||||||
import { SomeWrapper, ExtendWrapper, StaticWrapper } from '../wrappers/Wrapper'; | ||||||
import { makeValidationHook, ValidationProps } from '../_shared/hooks/useValidation'; | ||||||
import { DateRangePickerView, ExportedDateRangePickerViewProps } from './DateRangePickerView'; | ||||||
import { DateRangePickerInput, ExportedDateRangePickerInputProps } from './DateRangePickerInput'; | ||||||
import { | ||||||
DateRange as DateRangeType, | ||||||
RangeInput, | ||||||
AllSharedDateRangePickerProps, | ||||||
} from './RangeTypes'; | ||||||
|
||||||
export function parseRangeInputValue( | ||||||
now: MaterialUiPickersDate, | ||||||
utils: MuiPickersAdapter, | ||||||
{ value = [null, null], defaultHighlight }: BasePickerProps<RangeInput, DateRange> | ||||||
) { | ||||||
return value.map(date => | ||||||
date === null | ||||||
? null | ||||||
: utils.startOfDay(parsePickerInputValue(now, utils, { value: date, defaultHighlight })) | ||||||
) as DateRangeType; | ||||||
} | ||||||
import { | ||||||
parseRangeInputValue, | ||||||
validateDateRange, | ||||||
DateRangeValidationError, | ||||||
} from '../_helpers/date-utils'; | ||||||
|
||||||
export interface DateRangePickerProps | ||||||
extends ExportedDateRangePickerViewProps, | ||||||
ValidationProps<DateRangeValidationError, DateRangeType>, | ||||||
ExportedDateRangePickerInputProps { | ||||||
/** | ||||||
* Text for start input label and toolbar placeholder | ||||||
|
@@ -45,6 +39,12 @@ export interface DateRangePickerProps | |||||
endText?: React.ReactNode; | ||||||
} | ||||||
|
||||||
export const useDateRangeValidation = makeValidationHook< | ||||||
DateRangeValidationError, | ||||||
DateRangeType, | ||||||
DateRangePickerProps | ||||||
>(validateDateRange); | ||||||
|
||||||
export function makeRangePicker<TWrapper extends SomeWrapper>(Wrapper: TWrapper) { | ||||||
const WrapperComponent = makeWrapperComponent<DateRangeInputProps, RangeInput, DateRange>( | ||||||
Wrapper, | ||||||
|
@@ -56,35 +56,36 @@ export function makeRangePicker<TWrapper extends SomeWrapper>(Wrapper: TWrapper) | |||||
|
||||||
function RangePickerWithStateAndWrapper({ | ||||||
calendars, | ||||||
minDate, | ||||||
maxDate, | ||||||
disablePast, | ||||||
disableFuture, | ||||||
shouldDisableDate, | ||||||
showDaysOutsideCurrentMonth, | ||||||
onMonthChange, | ||||||
disableHighlightToday, | ||||||
reduceAnimations, | ||||||
value, | ||||||
onChange, | ||||||
mask = '__/__/____', | ||||||
startText = 'Start', | ||||||
endText = 'End', | ||||||
inputFormat: passedInputFormat, | ||||||
...restPropsForTextField | ||||||
minDate: __minDate = defaultMinDate, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't it look less clear? Because accidentally type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No specific preference, we could make it the new convention, if we do, could you update all the other components (core, lab, etc.)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably yes :) |
||||||
maxDate: __maxDate = defaultMaxDate, | ||||||
...other | ||||||
}: DateRangePickerProps & AllSharedDateRangePickerProps & ExtendWrapper<TWrapper>) { | ||||||
const utils = useUtils(); | ||||||
const minDate = useParsedDate(__minDate); | ||||||
const maxDate = useParsedDate(__maxDate); | ||||||
const [currentlySelectingRangeEnd, setCurrentlySelectingRangeEnd] = React.useState< | ||||||
'start' | 'end' | ||||||
>('start'); | ||||||
|
||||||
const pickerStateProps = { | ||||||
...restPropsForTextField, | ||||||
...other, | ||||||
value, | ||||||
onChange, | ||||||
inputFormat: passedInputFormat || utils.formats.keyboardDate, | ||||||
}; | ||||||
|
||||||
const restProps = { | ||||||
...other, | ||||||
minDate, | ||||||
maxDate, | ||||||
}; | ||||||
|
||||||
const { pickerProps, inputProps, wrapperProps } = usePickerState<RangeInput, DateRange>( | ||||||
pickerStateProps, | ||||||
{ | ||||||
|
@@ -94,40 +95,31 @@ export function makeRangePicker<TWrapper extends SomeWrapper>(Wrapper: TWrapper) | |||||
} | ||||||
); | ||||||
|
||||||
const validationError = useDateRangeValidation(pickerProps.date, restProps); | ||||||
|
||||||
const DateInputProps = { | ||||||
...inputProps, | ||||||
...restPropsForTextField, | ||||||
...restProps, | ||||||
currentlySelectingRangeEnd, | ||||||
setCurrentlySelectingRangeEnd, | ||||||
startText, | ||||||
endText, | ||||||
mask, | ||||||
validationError, | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<WrapperComponent | ||||||
wrapperProps={wrapperProps} | ||||||
DateInputProps={DateInputProps} | ||||||
{...restPropsForTextField} | ||||||
> | ||||||
<WrapperComponent wrapperProps={wrapperProps} DateInputProps={DateInputProps} {...restProps}> | ||||||
<DateRangePickerView | ||||||
open={wrapperProps.open} | ||||||
DateInputProps={DateInputProps} | ||||||
calendars={calendars} | ||||||
minDate={minDate} | ||||||
maxDate={maxDate} | ||||||
disablePast={disablePast} | ||||||
disableFuture={disableFuture} | ||||||
shouldDisableDate={shouldDisableDate} | ||||||
showDaysOutsideCurrentMonth={showDaysOutsideCurrentMonth} | ||||||
onMonthChange={onMonthChange} | ||||||
disableHighlightToday={disableHighlightToday} | ||||||
reduceAnimations={reduceAnimations} | ||||||
currentlySelectingRangeEnd={currentlySelectingRangeEnd} | ||||||
setCurrentlySelectingRangeEnd={setCurrentlySelectingRangeEnd} | ||||||
startText={startText} | ||||||
endText={endText} | ||||||
{...pickerProps} | ||||||
{...restProps} | ||||||
/> | ||||||
</WrapperComponent> | ||||||
); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer export default to be always the last export. It is more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We systematically use the other way around to 1. increase the code density of the demos, 2. reduce the duplicate of the name of the demo. I think that it's important we still to a single convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but for now pickers are standalone project. And if we are not planning to migrate docs before v5 we could stick to the our internal conventions for now. (before we merge docs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we can break down the dicussion in 3 different topics:
Why? The consistency of the components is one of the important value propositions of the library, I think that steps in this direction improve the DX.
For history, we used to follow the approach of the pull request, it was optimized for reducing the switching cost between class and functional components. With hooks, this requirement is no longer relevant.