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

feat: support ctrl n/p on mac #650

Merged
merged 5 commits into from
Aug 13, 2021
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
Next Next commit
feat: support ctrl n/p on mac
  • Loading branch information
wendellhu committed Aug 11, 2021
commit 87885dca519c5b3870cb9ef0afe0f903e8ada1cc
13 changes: 11 additions & 2 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
} from './interface';
import type { RawValueType, FlattenOptionsType } from './interface/generator';
import { fillFieldNames } from './utils/valueUtil';
import { isPlatformMac } from './utils/platformUtil';

export interface OptionListProps<OptionsType extends object[]> {
prefixCls: string;
Expand Down Expand Up @@ -183,16 +184,24 @@ const OptionList: React.RefForwardingComponent<
// ========================= Keyboard =========================
React.useImperativeHandle(ref, () => ({
onKeyDown: (event) => {
const { which } = event;
const { which, ctrlKey } = event;
switch (which) {
// >>> Arrow keys
// >>> Arrow keys & ctrl + n/p on Mac
case KeyCode.N:
case KeyCode.P:
case KeyCode.UP:
case KeyCode.DOWN: {
let offset = 0;
if (which === KeyCode.UP) {
offset = -1;
} else if (which === KeyCode.DOWN) {
offset = 1;
} else if (isPlatformMac() && ctrlKey) {
afc163 marked this conversation as resolved.
Show resolved Hide resolved
if (which === KeyCode.N) {
offset = 1;
} else if (which === KeyCode.P) {
offset = -1;
}
}

if (offset !== 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/platformUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isPlatformMac(): boolean {
return /mac\sos/i.test(navigator.appVersion);
wzhudev marked this conversation as resolved.
Show resolved Hide resolved
}
105 changes: 52 additions & 53 deletions tests/OptionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { mount } from 'enzyme';
import KeyCode from 'rc-util/lib/KeyCode';
import { act } from 'react-dom/test-utils';
import React from 'react';
import OptionList, { OptionListProps, RefOptionListProps } from '../src/OptionList';
import type { OptionListProps, RefOptionListProps } from '../src/OptionList';
import OptionList from '../src/OptionList';
import { injectRunAllTimers } from './utils/common';
import { OptionsType } from '../src/interface';
import type { OptionsType } from '../src/interface';
import { flattenOptions } from '../src/utils/valueUtil';

describe('OptionList', () => {
Expand Down Expand Up @@ -103,6 +104,40 @@ describe('OptionList', () => {
);
});

// this won't pass on test environment which is usually running on Linux
// you can test it with a Mac
xit('special key operation on Mac', () => {
const onActiveValue = jest.fn();
const listRef = React.createRef<RefOptionListProps>();
mount(
generateList({
options: [{ value: '1' }, { value: '2' }],
onActiveValue,
ref: listRef,
}),
);

onActiveValue.mockReset();
act(() => {
listRef.current.onKeyDown({ which: KeyCode.N, ctrlKey: true } as any);
});
expect(onActiveValue).toHaveBeenCalledWith(
'2',
expect.anything(),
expect.objectContaining({ source: 'keyboard' }),
);

onActiveValue.mockReset();
act(() => {
listRef.current.onKeyDown({ which: KeyCode.P, ctrlKey: true } as any);
});
expect(onActiveValue).toHaveBeenCalledWith(
'1',
expect.anything(),
expect.objectContaining({ source: 'keyboard' }),
);
});

it('hover to active', () => {
const onActiveValue = jest.fn();
const wrapper = mount(
Expand All @@ -113,10 +148,7 @@ describe('OptionList', () => {
);

onActiveValue.mockReset();
wrapper
.find('.rc-select-item-option')
.last()
.simulate('mouseMove');
wrapper.find('.rc-select-item-option').last().simulate('mouseMove');
expect(onActiveValue).toHaveBeenCalledWith(
'2',
expect.anything(),
Expand All @@ -125,10 +157,7 @@ describe('OptionList', () => {

// Same item not repeat trigger
onActiveValue.mockReset();
wrapper
.find('.rc-select-item-option')
.last()
.simulate('mouseMove');
wrapper.find('.rc-select-item-option').last().simulate('mouseMove');
expect(onActiveValue).not.toHaveBeenCalled();
});

Expand All @@ -140,29 +169,24 @@ describe('OptionList', () => {
);

const preventDefault = jest.fn();
wrapper
.find('.rc-select-item-option')
.last()
.simulate('mouseDown', {
preventDefault,
});
wrapper.find('.rc-select-item-option').last().simulate('mouseDown', {
preventDefault,
});

expect(preventDefault).toHaveBeenCalled();
});

it('Data attributes should be set correct', () => {
const wrapper = mount(
generateList({
options: [{ value: '1', label: 'my-label' }, { value: '2', 'data-num': '123' }],
options: [
{ value: '1', label: 'my-label' },
{ value: '2', 'data-num': '123' },
],
}),
);

expect(
wrapper
.find('.rc-select-item-option')
.last()
.prop('data-num'),
).toBe('123');
expect(wrapper.find('.rc-select-item-option').last().prop('data-num')).toBe('123');
});

it('should render title defaultly', () => {
Expand All @@ -171,12 +195,7 @@ describe('OptionList', () => {
options: [{ value: '1', label: 'my-label' }],
}),
);
expect(
wrapper
.find('.rc-select-item-option')
.first()
.prop('title'),
).toBe('my-label');
expect(wrapper.find('.rc-select-item-option').first().prop('title')).toBe('my-label');
});

it('should render title', () => {
Expand All @@ -185,12 +204,7 @@ describe('OptionList', () => {
options: [{ value: '1', label: 'my-label', title: 'title' }],
}),
);
expect(
wrapper
.find('.rc-select-item-option')
.first()
.prop('title'),
).toBe('title');
expect(wrapper.find('.rc-select-item-option').first().prop('title')).toBe('title');
});

it('should not render title when title is empty string', () => {
Expand All @@ -199,12 +213,7 @@ describe('OptionList', () => {
options: [{ value: '1', label: 'my-label', title: '' }],
}),
);
expect(
wrapper
.find('.rc-select-item-option')
.first()
.prop('title'),
).toBe('');
expect(wrapper.find('.rc-select-item-option').first().prop('title')).toBe('');
});

it('should render title from label when title is undefined', () => {
Expand All @@ -213,12 +222,7 @@ describe('OptionList', () => {
options: [{ value: '1', label: 'my-label', title: undefined }],
}),
);
expect(
wrapper
.find('.rc-select-item-option')
.first()
.prop('title'),
).toBe('my-label');
expect(wrapper.find('.rc-select-item-option').first().prop('title')).toBe('my-label');
});

it('should not render title defaultly when label is ReactNode', () => {
Expand All @@ -227,11 +231,6 @@ describe('OptionList', () => {
options: [{ value: '1', label: <div>label</div> }],
}),
);
expect(
wrapper
.find('.rc-select-item-option')
.first()
.prop('title'),
).toBe(undefined);
expect(wrapper.find('.rc-select-item-option').first().prop('title')).toBe(undefined);
});
});