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

Allowed for multiple time formats in the header time input #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ API
| showMinute | Boolean | true | whether show minute |
| showSecond | Boolean | true | whether show second |
| format | String | - | moment format |
| validFormats | Array of string | - | alternative moment formats that the user can enter: use this to support 1pm, 12:30, in addition to the default format |
| disabledHours | Function | - | disabled hour options |
| disabledMinutes | Function | - | disabled minute options |
| disabledSeconds | Function | - | disabled second options |
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ declare module "rc-time-picker" {
showMinute?: boolean;
showSecond?: boolean;
format?: string;
validFormats?: string[];
disabledHours?: () => number[];
disabledMinutes?: (hour: number) => number[];
disabledSeconds?: (hour: number, minute: number) => number[];
29 changes: 21 additions & 8 deletions src/Header.jsx
Original file line number Diff line number Diff line change
@@ -47,13 +47,14 @@ class Header extends Component {
}
}

onInputChange = event => {
onInputChange = (event) => {
const str = event.target.value;
this.setState({
str,
});
const {
format,
format: propFormat,
validFormats,
hourOptions,
minuteOptions,
secondOptions,
@@ -66,17 +67,29 @@ class Header extends Component {
if (str) {
const { value: originalValue } = this.props;
const value = this.getProtoValue().clone();

const format =
validFormats?.find((format) => {
const result = moment(str, format, true).isValid();
return result;
}) || propFormat;

if (!format) {
this.setState({
invalid: true,
});

return;
}

const parsed = moment(str, format, true);
if (!parsed.isValid()) {
this.setState({
invalid: true,
});
return;
}
value
.hour(parsed.hour())
.minute(parsed.minute())
.second(parsed.second());
value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());

// if time value not allowed, response warning.
if (
@@ -130,7 +143,7 @@ class Header extends Component {
});
};

onKeyDown = e => {
onKeyDown = (e) => {
const { onEsc, onKeyDown } = this.props;
if (e.keyCode === 27) {
onEsc();
@@ -151,7 +164,7 @@ class Header extends Component {
return (
<input
className={classNames(`${prefixCls}-input`, invalidClass)}
ref={ref => {
ref={(ref) => {
this.refInput = ref;
}}
onKeyDown={this.onKeyDown}
2 changes: 2 additions & 0 deletions src/Panel.jsx
Original file line number Diff line number Diff line change
@@ -109,6 +109,7 @@ class Panel extends Component {
showMinute,
showSecond,
format,
validFormats,
defaultOpenValue,
clearText,
onEsc,
@@ -160,6 +161,7 @@ class Panel extends Component {
currentSelectPanel={currentSelectPanel}
onEsc={onEsc}
format={format}
validFormats={validFormats}
placeholder={placeholder}
hourOptions={hourOptions}
minuteOptions={minuteOptions}
2 changes: 2 additions & 0 deletions src/TimePicker.jsx
Original file line number Diff line number Diff line change
@@ -153,6 +153,7 @@ class Picker extends Component {
minuteStep,
secondStep,
clearIcon,
validFormats,
} = this.props;
const { value } = this.state;
return (
@@ -183,6 +184,7 @@ class Picker extends Component {
focusOnOpen={focusOnOpen}
onKeyDown={onKeyDown}
clearIcon={clearIcon}
validFormats={validFormats}
/>
);
}