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

Add autoFocus prop to TimePicker #4124

Merged
merged 1 commit into from
May 21, 2020
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
9 changes: 9 additions & 0 deletions packages/datetime/src/timePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export const TimePrecision = {
export type TimePrecision = typeof TimePrecision[keyof typeof TimePrecision];

export interface ITimePickerProps extends IProps {
/**
* Whether to focus the first input when it opens initially.
* @default false
*/
autoFocus?: boolean;

/**
* Initial time the `TimePicker` will display.
* This should not be set if `value` is set.
Expand Down Expand Up @@ -126,6 +132,7 @@ export interface ITimePickerState {

export class TimePicker extends React.Component<ITimePickerProps, ITimePickerState> {
public static defaultProps: ITimePickerProps = {
autoFocus: false,
disabled: false,
maxTime: getDefaultMaxTime(),
minTime: getDefaultMinTime(),
Expand Down Expand Up @@ -231,6 +238,7 @@ export class TimePicker extends React.Component<ITimePickerProps, ITimePickerSta

private renderInput(className: string, unit: TimeUnit, value: string) {
const isValid = isTimeUnitValid(unit, parseInt(value, 10));
const isHour = unit === TimeUnit.HOUR_12 || unit === TimeUnit.HOUR_24;

return (
<input
Expand All @@ -245,6 +253,7 @@ export class TimePicker extends React.Component<ITimePickerProps, ITimePickerSta
onKeyDown={this.getInputKeyDownHandler(unit)}
value={value}
disabled={this.props.disabled}
autoFocus={isHour && this.props.autoFocus}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { TimePicker, TimePrecision } from "@blueprintjs/datetime";
import { getDefaultMaxTime, getDefaultMinTime } from "@blueprintjs/datetime/lib/esm/common/timeUnit";

export interface ITimePickerExampleState {
autoFocus: boolean;
precision?: TimePrecision;
selectAllOnFocus?: boolean;
showArrowButtons?: boolean;
Expand All @@ -47,6 +48,7 @@ enum MaximumHours {

export class TimePickerExample extends React.PureComponent<IExampleProps, ITimePickerExampleState> {
public state = {
autoFocus: true,
disabled: false,
precision: TimePrecision.MINUTE,
selectAllOnFocus: false,
Expand Down Expand Up @@ -80,6 +82,7 @@ export class TimePickerExample extends React.PureComponent<IExampleProps, ITimeP
/>
<Switch checked={this.state.disabled} label="Disabled" onChange={this.toggleDisabled} />
<Switch checked={this.state.useAmPm} label="Use AM/PM" onChange={this.toggleUseAmPm} />
<Switch checked={this.state.autoFocus} label="Auto focus" onChange={this.toggleAutoFocus} />
<PrecisionSelect value={this.state.precision} onChange={this.handlePrecisionChange} />
<label className={Classes.LABEL}>
Minimum time
Expand Down Expand Up @@ -117,6 +120,10 @@ export class TimePickerExample extends React.PureComponent<IExampleProps, ITimeP
this.setState({ useAmPm: !this.state.useAmPm });
};

private toggleAutoFocus = () => {
this.setState({ autoFocus: !this.state.autoFocus });
};

private changeMinHour = (hour: MinimumHours) => {
let minTime = new Date(1995, 6, 30, hour);

Expand Down