Skip to content

Commit be43eb8

Browse files
prop validation added
1 parent 9e9f34b commit be43eb8

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

example/src/App.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ export default class App extends Component {
1919
<div style={{ display: "flex", justifyContent: "center" }}>
2020
<Calendar
2121
visible={visible}
22-
// date={date}
2322
step={step}
2423
startWith={startWith}
2524
startWithDates={startWithDates}
2625
onDayClick={(minDate, maxDate) => {
2726
this.setState({ startWithDates: [minDate, maxDate] });
2827
}}
29-
// baseColor="red"
30-
// fontColor="yellow"
31-
// hoverBackgroundColor="brown"
32-
// hoverFontColor="black"
33-
// disabledColor="orange"
34-
// weekDaysColor="green"
35-
// weekendsDaysColor="black"
36-
type="fix-range"
28+
baseColor="red"
29+
fontColor="white"
30+
hoverBackgroundColor="red"
31+
hoverFontColor="white"
32+
disabledColor="#b9b9b9"
33+
weekDaysColor="#ff7b7b"
34+
weekendsDaysColor="#ffbaba"
35+
type="range"
3736
/>
3837
</div>
3938
</div>

src/index.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,48 @@ class Calendar extends Component {
1919
startWith: "Wed",
2020
activeView: "default",
2121
fixRange: [],
22-
activeMouseEnter: false
22+
activeMouseEnter: false,
23+
invalid: true
2324
};
2425
}
2526

2627
componentDidMount() {
27-
const { date, step, startWith, startWithDates, only, type } = this.props;
28+
const {
29+
date,
30+
step,
31+
startWith,
32+
startWithDates,
33+
only,
34+
type,
35+
visible,
36+
onDayClick
37+
} = this.props;
38+
let invalid = false;
39+
if (!startWithDates) {
40+
console.error(`Prop "startWithDates" is Required`);
41+
invalid = true;
42+
}
43+
if (visible === undefined) {
44+
console.error(`Prop "visible" is Required`);
45+
invalid = true;
46+
}
47+
if (type === "range" && step === undefined) {
48+
console.error(`Prop "step" is Required`);
49+
invalid = true;
50+
}
51+
if (type === "range" && startWith === undefined) {
52+
console.error(`Prop "startWith" is Required`);
53+
invalid = true;
54+
}
55+
if (!onDayClick) {
56+
console.error(`Prop "onDayClick" is Required`);
57+
invalid = true;
58+
}
59+
console.log("TCL: componentDidMount -> invalid", invalid);
60+
this.setState({ invalid: invalid });
61+
if (invalid) {
62+
return false;
63+
}
2864
const dateInMoment = moment(date);
2965
const month = dateInMoment.format("YYYY-MMM"),
3066
year = dateInMoment.format("YYYY"),
@@ -53,7 +89,14 @@ class Calendar extends Component {
5389

5490
componentDidUpdate(prevProps, prevState) {
5591
if (prevProps !== this.props) {
56-
const { date, step, startWith, startWithDates, type } = this.props;
92+
const {
93+
visible,
94+
date,
95+
step,
96+
startWith,
97+
startWithDates,
98+
type
99+
} = this.props;
57100
const dateInMoment = moment(startWithDates[0]);
58101
const month = dateInMoment.format("YYYY-MMM"),
59102
year = dateInMoment.format("YYYY"),
@@ -269,8 +312,11 @@ class Calendar extends Component {
269312
}
270313
};
271314
render() {
272-
const { visible, only, type } = this.props;
273-
if (!visible) return false;
315+
const { visible, only, type, startWithDates } = this.props;
316+
console.log("TCL: render -> this.state.invalid", this.state.invalid);
317+
if (this.state.invalid) {
318+
return false;
319+
}
274320
const {
275321
activeDateIndex: activeDateArray,
276322
month: monthObj,

0 commit comments

Comments
 (0)