Skip to content

Commit 37815a8

Browse files
authored
Merge pull request #25 from pouriaMaleki/master
Fix not showing initial value - fixes #24
2 parents 5ca2c1e + fcc1ad3 commit 37815a8

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

examples/src/components/App.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const generateRandomMoment = () => {
1414

1515
export default class App extends Component {
1616
state = {
17-
value: moment()
17+
value: moment(),
18+
someValue: new moment()
1819
};
1920

2021
render() {
@@ -47,6 +48,22 @@ export default class App extends Component {
4748
<DatePicker />
4849
</Example>
4950
</div>
51+
<div>
52+
<Example
53+
title="ورود تاریخ کنترل شده"
54+
code={clearDatePickerCode}
55+
>
56+
<DatePicker
57+
value={this.state.someValue}
58+
onChange={value => this.setState({ someValue: value })}
59+
/>
60+
<div style={{ paddingTop: 15 }}>
61+
<button onClick={() => this.setState({ someValue: null }) }>
62+
حذف مقدار
63+
</button>
64+
</div>
65+
</Example>
66+
</div>
5067
<div>
5168
<Example
5269
title="ورودی تاریخ و کنترل مقدار"
@@ -102,6 +119,21 @@ const basicDatePickerCode = `render() {
102119
return <DatePicker />;
103120
}`;
104121

122+
123+
const clearDatePickerCode = `render() {
124+
return (
125+
<div>
126+
<DatePicker
127+
value={this.state.value}
128+
onChange={value => this.setState({ value })}
129+
/>
130+
<button onClick={() => this.setState({ value: null }) }>
131+
حذف مقدار
132+
</button>
133+
</div>
134+
);
135+
}`;
136+
105137
const controlledDatePickerCode = `render() {
106138
return (
107139
<div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-persian-datepicker",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Persian calendar and date picker components for React",
55
"main": "lib/index.js",
66
"scripts": {

src/components/DatePicker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export default class DatePicker extends Component {
5454
this.props.onChange(momentValue);
5555
}
5656

57-
const inputValue = momentValue.format(inputFormat);
57+
let inputValue = "";
58+
if (momentValue)
59+
inputValue = momentValue.format(inputFormat);
5860
this.setState({ momentValue, inputValue });
5961
}
6062

@@ -117,7 +119,13 @@ export default class DatePicker extends Component {
117119
}
118120

119121
renderInput() {
120-
const { isOpen, inputValue } = this.state;
122+
let { isOpen, inputValue } = this.state;
123+
124+
if (this.props.value) {
125+
let value = this.props.value;
126+
let inputFormat = this.props.inputFormat;
127+
inputValue = value.format(inputFormat);
128+
}
121129

122130
const className = classnames(this.props.className, {
123131
[outsideClickIgnoreClass]: isOpen

0 commit comments

Comments
 (0)