Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(hoverPlaceholder): optmize logic when value is not empty #109

Merged
merged 4 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

@background-color: rgb(255, 240, 255);

@input-placeholder-color: hsv(0, 0, 75%);

.placeholder(@color: @input-placeholder-color) {
// Firefox
&::-moz-placeholder {
opacity: 1; // Override Firefox's unusual default opacity; see https://github.com/twbs/bootstrap/pull/11526
}

&::placeholder {
color: @color;
}

&:placeholder-shown {
text-overflow: ellipsis;
}
}

.@{prefix-cls} {
display: inline-flex;

Expand Down Expand Up @@ -292,6 +309,13 @@

> input {
width: 100%;
.placeholder();
}

&-placeholder {
> input {
color: @input-placeholder-color;
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
};
const popupPlacement = direction === 'rtl' ? 'bottomRight' : 'bottomLeft';

const [hoverPlaceholder, onEnter, onLeave] = useHoverPlaceholder(placeholder, text, {
const [hoverPlaceholder, onEnter, onLeave, isHover] = useHoverPlaceholder(placeholder, text, {
formatList,
generateConfig,
locale,
Expand Down Expand Up @@ -481,14 +481,20 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
onContextMenu={onContextMenu}
onClick={onClick}
>
<div className={`${prefixCls}-input`} ref={inputDivRef}>
<div
className={classNames(`${prefixCls}-input`, {
[`${prefixCls}-input-placeholder`]: isHover,
})}
ref={inputDivRef}
>
<input
id={id}
tabIndex={tabIndex}
disabled={disabled}
readOnly={inputReadOnly || !typing}
value={text}
value={text && isHover ? hoverPlaceholder : text}
kerm1it marked this conversation as resolved.
Show resolved Hide resolved
onChange={e => {
onLeave(null);
triggerTextChange(e.target.value);
}}
autoFocus={autoFocus}
Expand Down
12 changes: 8 additions & 4 deletions src/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
// ========================== Hover Range ==========================
const [hoverRangedValue, setHoverRangedValue] = useState<RangeValue<DateType>>(null);

const [startPlaceholder, onStartEnter, onStartLeave] = useHoverPlaceholder(
const [startPlaceholder, onStartEnter, onStartLeave, isStartHover] = useHoverPlaceholder(
getValue(placeholder, 0) || '',
startText,
{
Expand All @@ -550,7 +550,7 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
},
);

const [endPlaceholder, onEndEnter, onEndLeave] = useHoverPlaceholder(
const [endPlaceholder, onEndEnter, onEndLeave, isEndHover] = useHoverPlaceholder(
getValue(placeholder, 1) || '',
endText,
{
Expand Down Expand Up @@ -1060,15 +1060,17 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
<div
className={classNames(`${prefixCls}-input`, {
[`${prefixCls}-input-active`]: mergedActivePickerIndex === 0,
[`${prefixCls}-input-placeholder`]: isStartHover,
})}
ref={startInputDivRef}
>
<input
id={id}
disabled={mergedDisabled[0]}
readOnly={inputReadOnly || !startTyping}
value={startText}
value={startText && isStartHover ? startPlaceholder : startText}
onChange={e => {
onStartLeave(null);
triggerStartTextChange(e.target.value);
}}
autoFocus={autoFocus}
Expand All @@ -1085,14 +1087,16 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
<div
className={classNames(`${prefixCls}-input`, {
[`${prefixCls}-input-active`]: mergedActivePickerIndex === 1,
[`${prefixCls}-input-placeholder`]: isEndHover,
})}
ref={endInputDivRef}
>
<input
disabled={mergedDisabled[1]}
readOnly={inputReadOnly || !endTyping}
value={endText}
value={endText && isEndHover ? endPlaceholder : endText}
onChange={e => {
onEndLeave(null);
triggerEndTextChange(e.target.value);
}}
placeholder={endPlaceholder}
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useHoverPlaceholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function useHoverPlaceholder<DateType>(
placeholder: string,
text: string,
{ formatList, generateConfig, locale }: ValueTextConfig<DateType>,
): [string, (date: DateType) => void, (date: DateType) => void] {
): [string, (date: DateType) => void, (date: DateType) => void, boolean] {
const [value, setValue] = useState(null);

const [valueTexts] = useValueTexts(value, {
Expand All @@ -15,14 +15,14 @@ export default function useHoverPlaceholder<DateType>(
});

function onEnter(date: DateType) {
if (!text) {
setValue(date);
}
setValue(date);
}

function onLeave() {
setValue(null);
}

return [(valueTexts && valueTexts[0]) || placeholder, onEnter, onLeave];
const hoverPlaceholder = valueTexts && valueTexts[0];
kerm1it marked this conversation as resolved.
Show resolved Hide resolved

return [hoverPlaceholder || placeholder, onEnter, onLeave, !!hoverPlaceholder];
}
37 changes: 36 additions & 1 deletion tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,46 @@ describe('Picker.Basic', () => {

describe('hover placeholder', () => {
const placeholder = 'custom placeholder';
it('placeholder should be origin value when input value is not empty', () => {
it('when input value is not empty', () => {
const wrapper = mount(
<MomentPicker placeholder={placeholder} open defaultValue={getMoment('2020-07-22')} />,
);
const cell = wrapper.findCell(24);
cell.simulate('mouseEnter');
expect(wrapper.find('input').prop('placeholder')).toBe('2020-07-24');
expect(wrapper.find('input').prop('value')).toBe('2020-07-24');
expect(wrapper.find('.rc-picker-input').hasClass('rc-picker-input-placeholder')).toBeTruthy();

cell.simulate('mouseLeave');
expect(wrapper.find('input').prop('placeholder')).toBe(placeholder);
expect(wrapper.find('input').prop('value')).toBe('2020-07-22');
expect(wrapper.find('.rc-picker-input').hasClass('rc-picker-input-placeholder')).toBeFalsy();
});

it('change value when hovering', () => {
const wrapper = mount(
<MomentPicker placeholder={placeholder} open defaultValue={getMoment('2020-07-22')} />,
);
const cell = wrapper.findCell(24);
cell.simulate('mouseEnter');
expect(wrapper.find('input').prop('placeholder')).toBe('2020-07-24');
expect(wrapper.find('input').prop('value')).toBe('2020-07-24');
expect(wrapper.find('.rc-picker-input').hasClass('rc-picker-input-placeholder')).toBeTruthy();

wrapper.find('input').simulate('change', {
target: {
value: '2020-07-23',
},
});

expect(wrapper.find('input').prop('placeholder')).toBe(placeholder);
expect(wrapper.find('input').prop('value')).toBe('2020-07-23');
expect(wrapper.find('.rc-picker-input').hasClass('rc-picker-input-placeholder')).toBeFalsy();

wrapper.closePicker();
expect(wrapper.find('input').prop('placeholder')).toBe(placeholder);
expect(wrapper.find('input').prop('value')).toBe('2020-07-23');
expect(wrapper.find('.rc-picker-input').hasClass('rc-picker-input-placeholder')).toBeFalsy();
});

it('placeholder should be target date when input value is empty', () => {
Expand All @@ -754,8 +787,10 @@ describe('Picker.Basic', () => {
const cell = wrapper.findCell(24);
cell.simulate('mouseEnter');
expect(wrapper.find('input').prop('placeholder')).toBe('2020-07-24');
expect(wrapper.find('input').prop('value')).toBe('');
cell.simulate('mouseLeave');
expect(wrapper.find('input').prop('placeholder')).toBe(placeholder);
expect(wrapper.find('input').prop('value')).toBe('');
wrapper.closePicker();
});
});
Expand Down
38 changes: 37 additions & 1 deletion tests/range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ describe('Picker.Range', () => {
const placeholder: [string, string] = ['custom placeholder1', 'custom placeholder2'];
const defaultValue: [Moment, Moment] = [getMoment('2020-07-22'), getMoment('2020-08-22')];

it('placeholder should be target date when input value is empty', () => {
it('when input value is empty', () => {
const wrapper = mount(
<MomentRangePicker placeholder={placeholder} defaultValue={defaultValue} />,
);
Expand All @@ -1446,12 +1446,24 @@ describe('Picker.Range', () => {
.first()
.prop('placeholder'),
).toBe('2020-07-24');
expect(
wrapper
.find('input')
.first()
.prop('value'),
).toBe('');
expect(
wrapper
.find('input')
.last()
.prop('placeholder'),
).toBe(placeholder[1]);
expect(
wrapper
.find('input')
.last()
.prop('value'),
).toBe('2020-08-22');
leftCell.simulate('mouseLeave');
expect(
wrapper
Expand All @@ -1465,6 +1477,18 @@ describe('Picker.Range', () => {
.last()
.prop('placeholder'),
).toBe(placeholder[1]);
expect(
wrapper
.find('input')
.first()
.prop('value'),
).toBe('');
expect(
wrapper
.find('input')
.last()
.prop('value'),
).toBe('2020-08-22');
wrapper.closePicker(0);

// right
Expand All @@ -1485,6 +1509,18 @@ describe('Picker.Range', () => {
.first()
.prop('placeholder'),
).toBe(placeholder[0]);
expect(
wrapper
.find('input')
.first()
.prop('value'),
).toBe('2020-07-22');
expect(
wrapper
.find('input')
.last()
.prop('value'),
).toBe('');
rightCell.simulate('mouseLeave');
expect(
wrapper
Expand Down