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

Control when you want to focus initial date in Calendar #465

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 12 additions & 1 deletion docs/prop-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@
"computed": false
}
},
"shouldFocusDateInitially": {
"type": {
"name": "bool"
},
"required": false,
"description": "Props to disable initial focused date",
"defaultValue": {
"value": "true",
"computed": false
}
},
"utils": {
"type": {
"name": "object"
Expand Down Expand Up @@ -3461,4 +3472,4 @@
}
}
]
}
}
1 change: 1 addition & 0 deletions lib/src/DatePicker/Calendar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface CalendarProps {
renderDay?: RenderDay;
utils?: Utils<MaterialUiPickersDate>;
shouldDisableDate?: (day: MaterialUiPickersDate) => boolean;
shouldFocusDateInitially?: boolean;
}

declare const Calendar: ComponentClass<CalendarProps>;
Expand Down
15 changes: 13 additions & 2 deletions lib/src/DatePicker/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Calendar extends Component {
renderDay: PropTypes.func,
theme: PropTypes.object.isRequired,
shouldDisableDate: PropTypes.func,
shouldFocusDateInitially: PropTypes.bool,
utils: PropTypes.object.isRequired,
allowKeyboardControl: PropTypes.bool,
};
Expand All @@ -40,6 +41,7 @@ export class Calendar extends Component {
renderDay: undefined,
allowKeyboardControl: false,
shouldDisableDate: () => false,
shouldFocusDateInitially: true,
};

state = {
Expand All @@ -57,6 +59,11 @@ export class Calendar extends Component {
return null;
}

state = {
currentMonth: this.props.utils.getStartOfMonth(this.props.date),
shouldFocusDate: this.props.shouldFocusDateInitially,
};

componentDidMount() {
const {
date, minDate, maxDate, utils, disableFuture, disablePast,
Expand All @@ -78,6 +85,8 @@ export class Calendar extends Component {
onDateSelect = (day, isFinish = true) => {
const { date, utils } = this.props;

this.setState({ shouldFocusDate: true });

const withHours = utils.setHours(day, utils.getHours(date));
const withMinutes = utils.setMinutes(withHours, utils.getMinutes(date));

Expand Down Expand Up @@ -184,21 +193,23 @@ export class Calendar extends Component {

renderDays = (week) => {
const { date, renderDay, utils } = this.props;
const { currentMonth, shouldFocusDate } = this.state;

const selectedDate = utils.startOfDay(date);
const currentMonthNumber = utils.getMonth(this.state.currentMonth);
const currentMonthNumber = utils.getMonth(currentMonth);
const now = utils.date();

return week.map((day) => {
const disabled = this.shouldDisableDate(day);
const dayInCurrentMonth = utils.getMonth(day) === currentMonthNumber;
const isSelectedDate = utils.isSameDay(selectedDate, day) && shouldFocusDate;

let dayComponent = (
<Day
current={utils.isSameDay(day, now)}
hidden={!dayInCurrentMonth}
disabled={disabled}
selected={utils.isSameDay(selectedDate, day)}
selected={isSelectedDate}
>
{utils.getDayText(day)}
</Day>
Expand Down
1 change: 1 addition & 0 deletions lib/src/DatePicker/DatePicker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface DatePickerProps {
renderDay?: RenderDay;
utils?: Utils<MaterialUiPickersDate>;
shouldDisableDate?: (day: MaterialUiPickersDate) => boolean;
shouldFocusDateInitially?: boolean;
}

declare const DatePicker: ComponentClass<DatePickerProps>;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/DatePicker/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class DatePicker extends PureComponent {
renderDay: PropTypes.func,
utils: PropTypes.object.isRequired,
shouldDisableDate: PropTypes.func,
shouldFocusDateInitially: PropTypes.bool,
allowKeyboardControl: PropTypes.bool,
}

Expand All @@ -40,6 +41,7 @@ export class DatePicker extends PureComponent {
rightArrowIcon: undefined,
renderDay: undefined,
shouldDisableDate: undefined,
shouldFocusDateInitially: true,
}

state = {
Expand Down Expand Up @@ -82,6 +84,7 @@ export class DatePicker extends PureComponent {
renderDay,
utils,
shouldDisableDate,
shouldFocusDateInitially,
allowKeyboardControl,
} = this.props;
const { showYearSelection } = this.state;
Expand Down Expand Up @@ -132,6 +135,7 @@ export class DatePicker extends PureComponent {
renderDay={renderDay}
utils={utils}
shouldDisableDate={shouldDisableDate}
shouldFocusDateInitially={shouldFocusDateInitially}
allowKeyboardControl={allowKeyboardControl}
/>
}
Expand All @@ -141,4 +145,3 @@ export class DatePicker extends PureComponent {
}

export default withUtils()(DatePicker);

1 change: 1 addition & 0 deletions lib/src/DatePicker/DatePickerWrapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface DatePickerWrapperProps extends BasePickerProps, Omit<ModalWrapp
rightArrowIcon?: ReactNode;
renderDay?: RenderDay;
shouldDisableDate?: (day: MaterialUiPickersDate) => boolean;
shouldFocusDateInitially: boolean;
}

declare const DatePickerWrapper: ComponentClass<DatePickerWrapperProps>;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/DatePicker/DatePickerWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const DatePickerWrapper = (props) => {
renderDay,
rightArrowIcon,
shouldDisableDate,
shouldFocusDateInitially,
utils,
value,
...other
} = props;
Expand Down Expand Up @@ -72,6 +74,7 @@ export const DatePickerWrapper = (props) => {
renderDay={renderDay}
rightArrowIcon={rightArrowIcon}
shouldDisableDate={shouldDisableDate}
shouldFocusDateInitially={shouldFocusDateInitially}
/>
</ModalWrapper>
)
Expand Down Expand Up @@ -114,6 +117,7 @@ DatePickerWrapper.propTypes = {
/** Enables keyboard listener for moving between days in calendar */
allowKeyboardControl: PropTypes.bool,
forwardedRef: PropTypes.func,
shouldFocusDateInitially: PropTypes.bool,
};

DatePickerWrapper.defaultProps = {
Expand All @@ -133,6 +137,7 @@ DatePickerWrapper.defaultProps = {
labelFunc: undefined,
shouldDisableDate: undefined,
forwardedRef: undefined,
shouldFocusDateInitially: true,
};

export default React.forwardRef((props, ref) => (
Expand Down
1 change: 1 addition & 0 deletions lib/src/DateTimePicker/DateTimePicker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface DateTimePickerProps {
renderDay?: RenderDay;
utils?: Utils<MaterialUiPickersDate>;
shouldDisableDate?: (day: MaterialUiPickersDate) => boolean;
shouldFocusDateInitially?: boolean;
}

declare const DateTimePicker: ComponentClass<DateTimePickerProps>;
Expand Down
10 changes: 10 additions & 0 deletions lib/src/DateTimePicker/DateTimePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export class DateTimePicker extends Component {
showTabs: PropTypes.bool,
timeIcon: PropTypes.node,
utils: PropTypes.object.isRequired,
ampm: PropTypes.bool,
shouldDisableDate: PropTypes.func,
shouldFocusDateInitially: PropTypes.bool,
animateYearScrolling: PropTypes.bool,
fadeTimeout: PropTypes.number.isRequired,
classes: PropTypes.object.isRequired,
}

static defaultProps = {
Expand All @@ -52,6 +58,8 @@ export class DateTimePicker extends Component {
renderDay: undefined,
rightArrowIcon: undefined,
shouldDisableDate: undefined,
animateYearScrolling: false,
shouldFocusDateInitially: true,
showTabs: true,
timeIcon: undefined,
}
Expand Down Expand Up @@ -119,6 +127,7 @@ export class DateTimePicker extends Component {
utils,
ampm,
shouldDisableDate,
shouldFocusDateInitially,
animateYearScrolling,
fadeTimeout,
classes,
Expand Down Expand Up @@ -175,6 +184,7 @@ export class DateTimePicker extends Component {
renderDay={renderDay}
utils={utils}
shouldDisableDate={shouldDisableDate}
shouldFocusDateInitially={shouldFocusDateInitially}
/>
</View>

Expand Down
1 change: 1 addition & 0 deletions lib/src/DateTimePicker/DateTimePickerWrapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface DateTimePickerWrapperProps extends BasePickerProps, Omit<ModalW
timeIcon?: ReactNode;
renderDay?: RenderDay;
shouldDisableDate?: (day: MaterialUiPickersDate) => boolean;
shouldFocusDateInitially?: boolean;
}

declare const DateTimePickerWrapper: ComponentClass<DateTimePickerWrapperProps>;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/DateTimePicker/DateTimePickerWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DateTimePickerWrapper = (props) => {
renderDay,
ampm,
shouldDisableDate,
shouldFocusDateInitially,
animateYearScrolling,
fadeTimeout,
forwardedRef,
Expand Down Expand Up @@ -83,6 +84,7 @@ export const DateTimePickerWrapper = (props) => {
renderDay={renderDay}
rightArrowIcon={rightArrowIcon}
shouldDisableDate={shouldDisableDate}
shouldFocusDateInitially={shouldFocusDateInitially}
showTabs={showTabs}
timeIcon={timeIcon}
/>
Expand Down Expand Up @@ -138,6 +140,7 @@ DateTimePickerWrapper.propTypes = {
/** Enables keyboard listener for moving between days in calendar */
allowKeyboardControl: PropTypes.bool,
forwardedRef: PropTypes.func,
shouldFocusDateInitially: PropTypes.bool,
};

DateTimePickerWrapper.defaultProps = {
Expand All @@ -161,6 +164,7 @@ DateTimePickerWrapper.defaultProps = {
animateYearScrolling: false,
fadeTimeout: 400,
forwardedRef: undefined,
shouldFocusDateInitially: true,
allowKeyboardControl: true,
};

Expand All @@ -172,4 +176,3 @@ const styles = {

const EnhancedWrapper = withStyles(styles, { name: 'MuiPickerDTPickerModal' })(DateTimePickerWrapper);
export default React.forwardRef((props, ref) => <EnhancedWrapper {...props} forwardedRef={ref} />);

1 change: 1 addition & 0 deletions lib/src/_shared/DateTextField.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface DateTextFieldProps extends Omit<TextFieldProps, 'onChange' | 'v
invalidDateMessage?: React.ReactNode;
clearable?: boolean;
TextFieldComponent?: React.ComponentType<TextFieldProps>;
shouldFocusDateInitially?: boolean;
}

declare const DateTextField: ComponentClass<DateTextFieldProps>;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/_shared/DateTextField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class DateTextField extends PureComponent {
adornmentPosition: PropTypes.oneOf(['start', 'end']),
/** Callback firing when date that applied in the keyboard is invalid */
onError: PropTypes.func,
shouldFocusDateInitially: PropTypes.bool,
}

static defaultProps = {
Expand Down Expand Up @@ -151,6 +152,7 @@ export class DateTextField extends PureComponent {
TextFieldComponent: TextField,
InputAdornmentProps: {},
adornmentPosition: 'end',
shouldFocusDateInitially: true,
}


Expand Down Expand Up @@ -280,6 +282,7 @@ export class DateTextField extends PureComponent {
onClear,
onClick,
TextFieldComponent,
shouldFocusDateInitially,
utils,
value,
...other
Expand Down
3 changes: 3 additions & 0 deletions lib/src/wrappers/ModalWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class ModalWrapper extends PureComponent {
onSetToday: PropTypes.func,
children: PropTypes.node.isRequired,
dialogContentClassName: PropTypes.string,
shouldFocusDateInitially: PropTypes.bool,
isAccepted: PropTypes.bool.isRequired,
}

Expand All @@ -61,6 +62,7 @@ export default class ModalWrapper extends PureComponent {
onOpen: undefined,
onClose: undefined,
onSetToday: undefined,
shouldFocusDateInitially: true,
}

state = {
Expand Down Expand Up @@ -152,6 +154,7 @@ export default class ModalWrapper extends PureComponent {
onOpen,
onClose,
onSetToday,
shouldFocusDateInitially,
isAccepted,
...other
} = this.props;
Expand Down