Skip to content

Commit 87b3c58

Browse files
committed
修复当 value 为 undefined 时抛异常的问题
1 parent 3e6dd9c commit 87b3c58

File tree

8 files changed

+63
-63
lines changed

8 files changed

+63
-63
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": false,
33
"name": "dy-cron-js",
4-
"version": "1.0.7",
4+
"version": "1.0.10",
55
"scripts": {
66
"start": "dumi dev",
77
"docs:build": "dumi build",
@@ -37,7 +37,6 @@
3737
]
3838
},
3939
"dependencies": {
40-
"@types/classnames": "^2.2.11",
4140
"@types/numeral": "^2.0.0",
4241
"antd": "^4.5.0",
4342
"numeral": "^2.0.6"
@@ -54,4 +53,4 @@
5453
"prettier": "^1.19.1",
5554
"yorkie": "^2.0.0"
5655
}
57-
}
56+
}

src/Cron/index.less

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Cron/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import React, { useState } from 'react';
1313
import { Cron } from 'dy-cron-js';
1414

1515
export default () => {
16-
const [cron, setCron] = useState('00 12 11-23/01 * * ?');
16+
const [cron, setCron] = useState(undefined);
1717
return (
1818
<Cron
1919
value={cron}

src/Cron/index.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import styles from './index.less';
32
import { Radio, RadioChangeEvent, Input } from 'antd';
43
import HourInput from '@/CycleInput/HourInput';
54
import DayInput from '@/CycleInput/DayInput';
@@ -28,6 +27,10 @@ const cycles = [
2827
// }
2928

3029
export interface CronProps {
30+
/**
31+
* class
32+
*/
33+
className?: string;
3134
/**
3235
* 是否显示输入框
3336
*/
@@ -51,10 +54,12 @@ export default class Cron extends React.PureComponent<CronProps, CronState> {
5154
constructor(props: CronProps) {
5255
super(props);
5356

54-
const cronParserResult = util.cronParser(props.value);
57+
var cron = props.value || util.generateRandomCron(cycles[0].value);
58+
59+
const cronParserResult = util.cronParser(cron);
5560
this.state = {
5661
cycle: cronParserResult?.cycle || cycles[0].value,
57-
cron: props.value,
62+
cron: cron,
5863
};
5964
}
6065

@@ -123,14 +128,14 @@ export default class Cron extends React.PureComponent<CronProps, CronState> {
123128

124129
render() {
125130
let { cycle, cron } = this.state;
126-
const { displayInput } = this.props;
131+
const { displayInput, className } = this.props;
127132
return (
128-
<div className={styles.container}>
133+
<div className={className}>
129134
{displayInput && (
130135
<Input value={cron} style={{ marginBottom: '10px' }} />
131136
)}
132137
<Radio.Group
133-
className={styles.cycles}
138+
style={{ marginBottom: '8px' }}
134139
value={cycle}
135140
onChange={this.onCycleChanged.bind(this)}
136141
>

src/FormLabel/index.less

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/FormLabel/index.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import styles from './index.less';
32

43
export interface FormLabelProps {
54
label?: string;
@@ -13,16 +12,30 @@ export default class FormLabel extends React.PureComponent<FormLabelProps> {
1312

1413
return (
1514
<div
16-
style={{ display: inline ? 'inline-block' : 'block' }}
17-
className={styles.container}
15+
style={{
16+
marginBottom: '8px',
17+
display: inline ? 'inline-block' : 'block',
18+
}}
1819
>
1920
{label && (
20-
<span className={styles.label}>
21+
<span style={{ fontSize: '14px', color: 'rgba(0, 0, 0, 0.85)' }}>
2122
{label.endsWith(':') ? label : label + ':'}
2223
</span>
2324
)}
24-
<div className={styles.content}>{children}</div>
25-
{suffix && <span className={styles.suffix}>{suffix}</span>}
25+
<div
26+
style={{
27+
display: 'inline-block',
28+
margin: '0px 8px',
29+
minWidth: '84px',
30+
}}
31+
>
32+
{children}
33+
</div>
34+
{suffix && (
35+
<span style={{ fontSize: '14px', color: 'rgba(0, 0, 0, 0.85)' }}>
36+
{suffix}
37+
</span>
38+
)}
2639
</div>
2740
);
2841
}

src/Select/HourSelect.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Select } from 'antd';
22
import React from 'react';
3-
import numeral from 'numeral';
43
import NumberSelect from './NumberSelect';
54

65
export default (props: {

src/util.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ export default {
8181
const { cycle } = value;
8282
if (cycle === 'H') {
8383
const { intervalHours, startHours, startMinutes, endHours } = value;
84-
if (intervalHours && startHours && startMinutes && endHours) {
84+
if (
85+
intervalHours != undefined &&
86+
startHours != undefined &&
87+
startMinutes != undefined &&
88+
endHours != undefined
89+
) {
8590
return `00 ${numeral(startMinutes).format('00')} ${numeral(
8691
startHours,
8792
).format('00')}-${numeral(endHours).format('00')}/${numeral(
@@ -92,30 +97,31 @@ export default {
9297
}
9398
} else {
9499
const { hours, minutes } = value;
95-
if (!hours || !minutes) {
96-
throw new Error('错误的参数');
97-
}
98-
if (cycle === 'D') {
99-
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
100-
'00',
101-
)} * * ?`;
102-
} else if (cycle === 'W') {
103-
const { dayOfWeek } = value;
104-
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
105-
'00',
106-
)} ? * ${dayOfWeek}`;
107-
} else if (cycle === 'M') {
108-
const { dayOfMonth } = value;
109-
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
110-
'00',
111-
)} ${dayOfMonth} * ?`;
112-
} else if (cycle === 'Q') {
113-
const { dayOfMonth, quarters } = value;
114-
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
115-
'00',
116-
)} ${dayOfMonth} ${quarters} ?`;
100+
if (hours != undefined && minutes != undefined) {
101+
if (cycle === 'D') {
102+
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
103+
'00',
104+
)} * * ?`;
105+
} else if (cycle === 'W') {
106+
const { dayOfWeek } = value;
107+
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
108+
'00',
109+
)} ? * ${dayOfWeek}`;
110+
} else if (cycle === 'M') {
111+
const { dayOfMonth } = value;
112+
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
113+
'00',
114+
)} ${dayOfMonth} * ?`;
115+
} else if (cycle === 'Q') {
116+
const { dayOfMonth, quarters } = value;
117+
return `00 ${numeral(minutes).format('00')} ${numeral(hours).format(
118+
'00',
119+
)} ${dayOfMonth} ${quarters} ?`;
120+
} else {
121+
throw new Error('无效的周期');
122+
}
117123
} else {
118-
throw new Error('无效的周期');
124+
throw new Error('错误的参数');
119125
}
120126
}
121127
},

0 commit comments

Comments
 (0)