Skip to content

Commit 661a7ae

Browse files
committed
kinda add clear value - fix not showing initial value
1 parent 5ca2c1e commit 661a7ae

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

examples/src/components/App.js

Lines changed: 34 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,23 @@ 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+
x={true}
59+
onChange={value => this.setState({ someValue: value })}
60+
/>
61+
<div style={{ paddingTop: 15 }}>
62+
<button onClick={() => this.setState({ someValue: null }) }>
63+
حذف مقدار
64+
</button>
65+
</div>
66+
</Example>
67+
</div>
5068
<div>
5169
<Example
5270
title="ورودی تاریخ و کنترل مقدار"
@@ -102,6 +120,21 @@ const basicDatePickerCode = `render() {
102120
return <DatePicker />;
103121
}`;
104122

123+
124+
const clearDatePickerCode = `render() {
125+
return (
126+
<div>
127+
<DatePicker
128+
value={this.state.value}
129+
onChange={value => this.setState({ value })}
130+
/>
131+
<button onClick={() => this.setState({ value: null }) }>
132+
حذف مقدار
133+
</button>
134+
</div>
135+
);
136+
}`;
137+
105138
const controlledDatePickerCode = `render() {
106139
return (
107140
<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)