Skip to content

Commit a7d5ef0

Browse files
committed
fix: Clear should also clear selected value
1 parent 31d9690 commit a7d5ef0

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

assets/index.less

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
transition: background 0.3s, border 0.3s;
7676

7777
&:hover {
78-
background: fade(blue, 30%)!important;
78+
background: fade(blue, 30%) !important;
7979
}
8080
}
8181

@@ -221,6 +221,7 @@
221221
// ======================== Input =========================
222222
&-input {
223223
position: relative;
224+
display: inline;
224225
}
225226

226227
&-clear {

examples/basic.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ export default () => {
103103
<h3>Basic</h3>
104104
<Picker<Moment> {...sharedProps} locale={zhCN} />
105105
</div>
106+
<div style={{ margin: '0 8px' }}>
107+
<h3>Uncontrolled</h3>
108+
<Picker<Moment>
109+
generateConfig={momentGenerateConfig}
110+
locale={zhCN}
111+
allowClear
112+
/>
113+
</div>
106114
<div style={{ margin: '0 8px' }}>
107115
<h3>Datetime</h3>
108116
<Picker<Moment>

examples/uncontrolled.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import { Moment } from 'moment';
3+
import Picker from '../src/Picker';
4+
import momentGenerateConfig from '../src/generate/moment';
5+
import zhCN from '../src/locale/zh_CN';
6+
import '../assets/index.less';
7+
8+
export default () => (
9+
<div>
10+
<div style={{ margin: '0 8px' }}>
11+
<h3>Uncontrolled</h3>
12+
<Picker<Moment>
13+
generateConfig={momentGenerateConfig}
14+
locale={zhCN}
15+
picker="week"
16+
allowClear
17+
/>
18+
</div>
19+
</div>
20+
);

src/Picker.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
213213

214214
// =========================== Formatter ===========================
215215
const setSelectedValue = (newDate: DateType | null) => {
216-
setDateText(newDate);
216+
if (!isSameTextDate(textValue, newDate)) {
217+
setDateText(newDate);
218+
}
217219
setInternalSelectedValue(newDate);
218220
};
219221

@@ -238,10 +240,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
238240

239241
// ============================ Trigger ============================
240242
const triggerChange = (newValue: DateType | null) => {
241-
if (!isSameTextDate(textValue, newValue)) {
242-
setDateText(newValue);
243-
}
244-
243+
setSelectedValue(newValue);
245244
setInnerValue(newValue);
246245

247246
if (onChange && !isEqual(generateConfig, mergedValue, newValue)) {

tests/picker.spec.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,18 @@ describe('Basic', () => {
172172

173173
describe('typing to change value', () => {
174174
[
175-
{ name: 'basic', value: '2000-11-11' },
176-
{ name: 'week', picker: 'week', value: '2000-45th' },
177-
].forEach(({ name, picker, value }) => {
175+
{
176+
name: 'basic',
177+
value: '2000-11-11',
178+
selected: '.rc-picker-date-panel-cell-selected',
179+
},
180+
{
181+
name: 'week',
182+
picker: 'week',
183+
value: '2000-45th',
184+
selected: '.rc-picker-week-panel-row-selected',
185+
},
186+
].forEach(({ name, picker, value, selected }) => {
178187
it(name, () => {
179188
const onChange = jest.fn();
180189
const wrapper = mount(
@@ -207,10 +216,14 @@ describe('Basic', () => {
207216
expect(
208217
isSame(onChange.mock.calls[0][0], '2000-11-11', picker as any),
209218
).toBeTruthy();
219+
expect(wrapper.find(selected).length).toBeTruthy();
210220
onChange.mockReset();
211221

212222
wrapper.clearValue();
213223
expect(onChange).toHaveBeenCalledWith(null, '');
224+
225+
wrapper.openPicker();
226+
expect(wrapper.find(selected).length).toBeFalsy();
214227
});
215228
});
216229
});

0 commit comments

Comments
 (0)