Skip to content

Commit

Permalink
fix(DatePicker2): RangeDate check bug when set disabled, close alibab…
Browse files Browse the repository at this point in the history
…a-fusion#3086

Co-authored-by: 皆虚 <[email protected]>
  • Loading branch information
jerryyxu and 皆虚 authored Jun 10, 2021
1 parent dd93efa commit 91d4b70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/date-picker2/picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function checkDate(value) {
}

function checkRangeDate(value, inputType, disabled, strictly = true) {
const [begin, end] = Array.isArray(value) ? [0, 1].map(i => checkDate(value[i])) : [null, null];
const _disabled = Array.isArray(disabled) ? disabled : [disabled, disabled];
const [begin, end] = Array.isArray(value) ? [0, 1].map((i) => checkDate(value[i])) : [null, null];
const [disabledBegin, disabledEnd] = Array.isArray(disabled) ? disabled : [disabled, disabled];

// 严格模式下
// 如果开始时间在结束时间之后 清空另一个时间
// 如果另一个时间处于禁用状态 清除当前时间
// 严格模式下,不允许开始时间大于结束时间
// 否则,优先清空 beginDate
// 只有在 endDate 被 disabled 且 beginDate 没有被 disabled 的时候才清空 beginDate
if (strictly && begin && end && begin.isAfter(end)) {
return inputType === DATE_INPUT_TYPE.BEGIN && !_disabled[1] ? [begin, null] : [null, end];
return !disabledBegin && disabledEnd ? [null, end] : [begin, null];
}

return [begin, end];
Expand Down Expand Up @@ -200,7 +200,7 @@ class Picker extends React.Component {
}

componentWillUnmount() {
[this.clearTimeoutId, this.timeoutId].forEach(id => id && clearTimeout(id));
[this.clearTimeoutId, this.timeoutId].forEach((id) => id && clearTimeout(id));
}

getInitValue = () => {
Expand Down Expand Up @@ -236,7 +236,7 @@ class Picker extends React.Component {
: checkDate(value);
};

handleInputFocus = inputType => {
handleInputFocus = (inputType) => {
let inputEl = this.dateInput && this.dateInput.input;

if (this.state.isRange) {
Expand All @@ -246,7 +246,7 @@ class Picker extends React.Component {
inputEl && inputEl.focus();
};

handleMouseDown = e => {
handleMouseDown = (e) => {
e.preventDefault();
};

Expand Down Expand Up @@ -305,7 +305,7 @@ class Picker extends React.Component {
});
};

shouldSwitchInput = value => {
shouldSwitchInput = (value) => {
const { inputType, justBeginInput } = this.state;
const idx = justBeginInput ? switchInputType(inputType) : value.indexOf(null);

Expand All @@ -318,7 +318,7 @@ class Picker extends React.Component {
return false;
};

isEnabled = idx => {
isEnabled = (idx) => {
const { disabled } = this.props;

return Array.isArray(disabled)
Expand Down Expand Up @@ -359,7 +359,7 @@ class Picker extends React.Component {
}
};

onKeyDown = e => {
onKeyDown = (e) => {
switch (e.keyCode) {
case KEYCODE.ENTER: {
const { inputValue } = this.state;
Expand All @@ -372,7 +372,7 @@ class Picker extends React.Component {
}
};

onChange = v => {
onChange = (v) => {
const { value } = this.state;
const { format } = this.props;

Expand Down Expand Up @@ -407,7 +407,7 @@ class Picker extends React.Component {
result !== false && this.handleChange(inputValue, 'CLICK_OK');
};

onInputTypeChange = idx => {
onInputTypeChange = (idx) => {
const { inputType, visible } = this.state;

if (idx !== inputType) {
Expand Down Expand Up @@ -497,7 +497,7 @@ class Picker extends React.Component {
}

const visible = 'visible' in this.props ? this.props.visible : this.state.visible;
const allDisabled = isRange && Array.isArray(disabled) ? disabled.every(v => v) : disabled;
const allDisabled = isRange && Array.isArray(disabled) ? disabled.every((v) => v) : disabled;
const sharedProps = {
rtl,
prefix,
Expand All @@ -522,7 +522,7 @@ class Picker extends React.Component {
onInput: handleInput,
readOnly: inputReadOnly,
inputProps: this.props.inputProps,
ref: el => (this.dateInput = el),
ref: (el) => (this.dateInput = el),
};

// 禁用状态下 不允许清空
Expand Down
17 changes: 17 additions & 0 deletions test/date-picker2/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,23 @@ describe('Picker', () => {

assert.deepEqual(getStrValue(), ['', '']);
});

// https://github.com/alibaba-fusion/next/issues/3086
it('fix issue on half disabled & showTime', () => {
wrapper = mount(
<RangePicker showTime disabled={[true, false]} value={['2021-01-12 10:00:00', '2021-01-12 09:00:00']} />
);
assert.deepEqual(getStrValue(), ['2021-01-12 10:00:00', '']);

wrapper.setProps({ disabled: [false, true] });
assert.deepEqual(getStrValue(), ['', '2021-01-12 09:00:00']);

wrapper.setProps({ disabled: true });
assert.deepEqual(getStrValue(), ['2021-01-12 10:00:00', '']);

wrapper.setProps({ disabled: false });
assert.deepEqual(getStrValue(), ['2021-01-12 10:00:00', '']);
});
});
});

Expand Down

0 comments on commit 91d4b70

Please sign in to comment.