From 8c0e3fbe73492fba5b5d9335917ad919dbf8f3d2 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Sat, 23 May 2020 03:39:12 +0800 Subject: [PATCH 01/17] do not filter disabled entity --- src/hooks/useKeyValueMapping.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index 98955901..b0481c3e 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -46,11 +46,6 @@ export default function useKeyValueMapping( const getEntityByValue = React.useCallback( (value: RawValueType, skipType: SkipType = 'select') => { const dataNode = cacheValueMap.get(value); - - if (isDisabled(dataNode, skipType)) { - return null; - } - return dataNode; }, [cacheValueMap], From c0ff34d67e17e417c500099eb8b9338c51fbdf02 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Sat, 23 May 2020 03:39:56 +0800 Subject: [PATCH 02/17] none --- src/hooks/useKeyValueMapping.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index b0481c3e..73e61626 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -3,10 +3,7 @@ import { FlattenDataNode, Key, RawValueType } from '../interface'; export type SkipType = null | 'select' | 'checkbox'; -export function isDisabled( - dataNode: FlattenDataNode, - skipType: SkipType, -): boolean { +export function isDisabled(dataNode: FlattenDataNode, skipType: SkipType): boolean { if (!dataNode) { return true; } @@ -44,10 +41,7 @@ export default function useKeyValueMapping( ); const getEntityByValue = React.useCallback( - (value: RawValueType, skipType: SkipType = 'select') => { - const dataNode = cacheValueMap.get(value); - return dataNode; - }, + (value: RawValueType, skipType: SkipType = 'select') => cacheValueMap.get(value), [cacheValueMap], ); From a7b060c2f54497c6d7dc2421e22e830f8352cc55 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Sat, 23 May 2020 03:49:43 +0800 Subject: [PATCH 03/17] fix lint --- src/hooks/useKeyValueMapping.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index 73e61626..181161b8 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -40,10 +40,9 @@ export default function useKeyValueMapping( [cacheKeyMap], ); - const getEntityByValue = React.useCallback( - (value: RawValueType, skipType: SkipType = 'select') => cacheValueMap.get(value), - [cacheValueMap], - ); + const getEntityByValue = React.useCallback((value: RawValueType) => cacheValueMap.get(value), [ + cacheValueMap, + ]); return [getEntityByKey, getEntityByValue]; } From f7126fdc25a21068343ea395898a523d4a1316f9 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 25 May 2020 15:10:13 +0800 Subject: [PATCH 04/17] update --- src/hooks/useKeyValueMapping.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index 181161b8..dd950c85 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -28,15 +28,7 @@ export default function useKeyValueMapping( (value: RawValueType, skipType?: SkipType) => FlattenDataNode, ] { const getEntityByKey = React.useCallback( - (key: Key, skipType: SkipType = 'select') => { - const dataNode = cacheKeyMap.get(key); - - if (isDisabled(dataNode, skipType)) { - return null; - } - - return dataNode; - }, + (key: Key, skipType: SkipType = 'select') =>cacheKeyMap.get(key), [cacheKeyMap], ); From 14f5d1c4634cb405e05871e63f2599fe34132159 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 25 May 2020 15:15:52 +0800 Subject: [PATCH 05/17] fix lint --- src/hooks/useKeyValueMapping.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index dd950c85..cbf8660f 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -27,10 +27,7 @@ export default function useKeyValueMapping( (key: Key, skipType?: SkipType) => FlattenDataNode, (value: RawValueType, skipType?: SkipType) => FlattenDataNode, ] { - const getEntityByKey = React.useCallback( - (key: Key, skipType: SkipType = 'select') =>cacheKeyMap.get(key), - [cacheKeyMap], - ); + const getEntityByKey = React.useCallback((key: Key) => cacheKeyMap.get(key), [cacheKeyMap]); const getEntityByValue = React.useCallback((value: RawValueType) => cacheValueMap.get(value), [ cacheValueMap, From 4c009f03810a16150425834f7dc51bc773510d14 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Thu, 28 May 2020 09:45:37 +0800 Subject: [PATCH 06/17] Comment out isDisabled --- src/hooks/useKeyValueMapping.ts | 32 ++++++++++++++++---------------- tests/utils.spec.js | 24 ++++++++++++------------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index cbf8660f..03f92049 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -3,22 +3,22 @@ import { FlattenDataNode, Key, RawValueType } from '../interface'; export type SkipType = null | 'select' | 'checkbox'; -export function isDisabled(dataNode: FlattenDataNode, skipType: SkipType): boolean { - if (!dataNode) { - return true; - } - - const { disabled, disableCheckbox } = dataNode.data; - - switch (skipType) { - case 'select': - return disabled; - case 'checkbox': - return disabled || disableCheckbox; - } - - return false; -} +// export function isDisabled(dataNode: FlattenDataNode, skipType: SkipType): boolean { +// if (!dataNode) { +// return true; +// } + +// const { disabled, disableCheckbox } = dataNode.data; + +// switch (skipType) { +// case 'select': +// return disabled; +// case 'checkbox': +// return disabled || disableCheckbox; +// } + +// return false; +// } export default function useKeyValueMapping( cacheKeyMap: Map, diff --git a/tests/utils.spec.js b/tests/utils.spec.js index f1c5d07b..d2d69af6 100644 --- a/tests/utils.spec.js +++ b/tests/utils.spec.js @@ -1,5 +1,5 @@ import { isValueDisabled } from '../src/utils/valueUtil'; -import { isDisabled } from '../src/hooks/useKeyValueMapping'; +// import { isDisabled } from '../src/hooks/useKeyValueMapping'; describe('TreeSelect.util', () => { it('isValueDisabled', () => { @@ -12,15 +12,15 @@ describe('TreeSelect.util', () => { expect(isValueDisabled('not-exist', options)).toBeFalsy(); }); - it('isDisabled', () => { - expect(isDisabled({ data: { disabled: true } }, 'select')).toBeTruthy(); - expect( - isDisabled({ data: { disableCheckbox: true } }, 'select'), - ).toBeFalsy(); - expect(isDisabled({ data: { disabled: true } }, 'checkbox')).toBeTruthy(); - expect( - isDisabled({ data: { disableCheckbox: true } }, 'checkbox'), - ).toBeTruthy(); - expect(isDisabled({ data: { disabled: true } }, null)).toBeFalsy(); - }); + // it('isDisabled', () => { + // expect(isDisabled({ data: { disabled: true } }, 'select')).toBeTruthy(); + // expect( + // isDisabled({ data: { disableCheckbox: true } }, 'select'), + // ).toBeFalsy(); + // expect(isDisabled({ data: { disabled: true } }, 'checkbox')).toBeTruthy(); + // expect( + // isDisabled({ data: { disableCheckbox: true } }, 'checkbox'), + // ).toBeTruthy(); + // expect(isDisabled({ data: { disabled: true } }, null)).toBeFalsy(); + // }); }); From c8c0da638081190a367cf8ced2daefcb6085849b Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Thu, 28 May 2020 09:52:55 +0800 Subject: [PATCH 07/17] codecov --- src/OptionList.tsx | 2 +- src/hooks/useKeyValueMapping.ts | 22 +--------------------- src/hooks/useSelectValues.ts | 5 ++--- tests/utils.spec.js | 13 ------------- 4 files changed, 4 insertions(+), 38 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 0ff39c87..e2d81aa8 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -170,7 +170,7 @@ const OptionList: React.RefForwardingComponent { - const entity = getEntityByKey(key, checkable ? 'checkbox' : 'select'); + const entity = getEntityByKey(key); if (entity !== null) { onSelect(entity.data.value, { selected: !checkedKeys.includes(entity.data.value), diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index 03f92049..5d2dcc34 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -3,30 +3,10 @@ import { FlattenDataNode, Key, RawValueType } from '../interface'; export type SkipType = null | 'select' | 'checkbox'; -// export function isDisabled(dataNode: FlattenDataNode, skipType: SkipType): boolean { -// if (!dataNode) { -// return true; -// } - -// const { disabled, disableCheckbox } = dataNode.data; - -// switch (skipType) { -// case 'select': -// return disabled; -// case 'checkbox': -// return disabled || disableCheckbox; -// } - -// return false; -// } - export default function useKeyValueMapping( cacheKeyMap: Map, cacheValueMap: Map, -): [ - (key: Key, skipType?: SkipType) => FlattenDataNode, - (value: RawValueType, skipType?: SkipType) => FlattenDataNode, -] { +): [(key: Key) => FlattenDataNode, (value: RawValueType) => FlattenDataNode] { const getEntityByKey = React.useCallback((key: Key) => cacheKeyMap.get(key), [cacheKeyMap]); const getEntityByValue = React.useCallback((value: RawValueType) => cacheValueMap.get(value), [ diff --git a/src/hooks/useSelectValues.ts b/src/hooks/useSelectValues.ts index 3126296a..f5874dd6 100644 --- a/src/hooks/useSelectValues.ts +++ b/src/hooks/useSelectValues.ts @@ -2,7 +2,6 @@ import React from 'react'; import { DefaultValueType } from 'rc-select/lib/interface/generator'; import { DataEntity } from 'rc-tree/lib/interface'; import { RawValueType, FlattenDataNode, Key, LabelValueType, DataNode } from '../interface'; -import { SkipType } from './useKeyValueMapping'; import { getRawValueLabeled } from '../utils/valueUtil'; import { formatStrategyKeys, CheckedStrategy } from '../utils/strategyUtil'; @@ -12,8 +11,8 @@ interface Config { value: DefaultValueType; showCheckedStrategy: CheckedStrategy; conductKeyEntities: Record; - getEntityByKey: (key: Key, skipType?: SkipType) => FlattenDataNode; - getEntityByValue: (value: RawValueType, skipType?: SkipType) => FlattenDataNode; + getEntityByKey: (key: Key) => FlattenDataNode; + getEntityByValue: (value: RawValueType) => FlattenDataNode; getLabelProp: (node: DataNode) => React.ReactNode; } diff --git a/tests/utils.spec.js b/tests/utils.spec.js index d2d69af6..b9d122a3 100644 --- a/tests/utils.spec.js +++ b/tests/utils.spec.js @@ -1,5 +1,4 @@ import { isValueDisabled } from '../src/utils/valueUtil'; -// import { isDisabled } from '../src/hooks/useKeyValueMapping'; describe('TreeSelect.util', () => { it('isValueDisabled', () => { @@ -11,16 +10,4 @@ describe('TreeSelect.util', () => { expect(isValueDisabled('pass', options)).toBeFalsy(); expect(isValueDisabled('not-exist', options)).toBeFalsy(); }); - - // it('isDisabled', () => { - // expect(isDisabled({ data: { disabled: true } }, 'select')).toBeTruthy(); - // expect( - // isDisabled({ data: { disableCheckbox: true } }, 'select'), - // ).toBeFalsy(); - // expect(isDisabled({ data: { disabled: true } }, 'checkbox')).toBeTruthy(); - // expect( - // isDisabled({ data: { disableCheckbox: true } }, 'checkbox'), - // ).toBeTruthy(); - // expect(isDisabled({ data: { disabled: true } }, null)).toBeFalsy(); - // }); }); From 810ade01251c33b042bab64ca86aabd55bd29f5d Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 1 Jun 2020 18:31:35 +0800 Subject: [PATCH 08/17] disabled: true --- tests/Select.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Select.spec.js b/tests/Select.spec.js index c91ba67d..ba5faf45 100644 --- a/tests/Select.spec.js +++ b/tests/Select.spec.js @@ -150,7 +150,7 @@ describe('TreeSelect.basic', () => { it('can be controlled by value', () => { const treeData = [ { key: '0', value: '0', title: 'label0' }, - { key: '1', value: '1', title: 'label1' }, + { key: '1', value: '1', title: 'label1', disabled: true }, ]; const wrapper = mount(); expect(wrapper.getSelection(0).text()).toEqual('label0'); From 9b090d499fe60c2d3216f68cb79724583d1956f0 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 1 Jun 2020 18:35:46 +0800 Subject: [PATCH 09/17] =?UTF-8?q?=E6=81=A2=E5=A4=8D=20isDisabled=20?= =?UTF-8?q?=E8=AF=B4=E4=B8=8D=E5=AE=9A=E7=94=A8=E5=BE=97=E7=9D=80=EF=BC=8C?= =?UTF-8?q?=E9=A1=BA=E4=BE=BF=E7=9C=8B=E7=9C=8Bcodecov?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useKeyValueMapping.ts | 17 +++++++++++++++++ tests/utils.spec.js | 14 ++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index 5d2dcc34..48614078 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -3,6 +3,23 @@ import { FlattenDataNode, Key, RawValueType } from '../interface'; export type SkipType = null | 'select' | 'checkbox'; +export function isDisabled(dataNode: FlattenDataNode, skipType: SkipType): boolean { + if (!dataNode) { + return true; + } + + const { disabled, disableCheckbox } = dataNode.data; + + switch (skipType) { + case 'select': + return disabled; + case 'checkbox': + return disabled || disableCheckbox; + } + + return false; +} + export default function useKeyValueMapping( cacheKeyMap: Map, cacheValueMap: Map, diff --git a/tests/utils.spec.js b/tests/utils.spec.js index b9d122a3..d178d31a 100644 --- a/tests/utils.spec.js +++ b/tests/utils.spec.js @@ -1,13 +1,19 @@ import { isValueDisabled } from '../src/utils/valueUtil'; +import { isDisabled } from '../src/hooks/useKeyValueMapping'; describe('TreeSelect.util', () => { it('isValueDisabled', () => { - const options = [ - { data: { value: 'disabled', disabled: true } }, - { data: { value: 'pass' } }, - ]; + const options = [{ data: { value: 'disabled', disabled: true } }, { data: { value: 'pass' } }]; expect(isValueDisabled('disabled', options)).toBeTruthy(); expect(isValueDisabled('pass', options)).toBeFalsy(); expect(isValueDisabled('not-exist', options)).toBeFalsy(); }); + + it('isDisabled', () => { + expect(isDisabled({ data: { disabled: true } }, 'select')).toBeTruthy(); + expect(isDisabled({ data: { disableCheckbox: true } }, 'select')).toBeFalsy(); + expect(isDisabled({ data: { disabled: true } }, 'checkbox')).toBeTruthy(); + expect(isDisabled({ data: { disableCheckbox: true } }, 'checkbox')).toBeTruthy(); + expect(isDisabled({ data: { disabled: true } }, null)).toBeFalsy(); + }); }); From 19d82033984ec82457c442ea2b252c18bbe97ca7 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 1 Jun 2020 18:41:55 +0800 Subject: [PATCH 10/17] =?UTF-8?q?Revert=20"=E6=81=A2=E5=A4=8D=20isDisabled?= =?UTF-8?q?=20=E8=AF=B4=E4=B8=8D=E5=AE=9A=E7=94=A8=E5=BE=97=E7=9D=80?= =?UTF-8?q?=EF=BC=8C=E9=A1=BA=E4=BE=BF=E7=9C=8B=E7=9C=8Bcodecov"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 345a5ddfe1e0b538b2e8552f57e5646395d4efe2. --- src/hooks/useKeyValueMapping.ts | 17 ----------------- tests/utils.spec.js | 14 ++++---------- 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index 48614078..5d2dcc34 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -3,23 +3,6 @@ import { FlattenDataNode, Key, RawValueType } from '../interface'; export type SkipType = null | 'select' | 'checkbox'; -export function isDisabled(dataNode: FlattenDataNode, skipType: SkipType): boolean { - if (!dataNode) { - return true; - } - - const { disabled, disableCheckbox } = dataNode.data; - - switch (skipType) { - case 'select': - return disabled; - case 'checkbox': - return disabled || disableCheckbox; - } - - return false; -} - export default function useKeyValueMapping( cacheKeyMap: Map, cacheValueMap: Map, diff --git a/tests/utils.spec.js b/tests/utils.spec.js index d178d31a..b9d122a3 100644 --- a/tests/utils.spec.js +++ b/tests/utils.spec.js @@ -1,19 +1,13 @@ import { isValueDisabled } from '../src/utils/valueUtil'; -import { isDisabled } from '../src/hooks/useKeyValueMapping'; describe('TreeSelect.util', () => { it('isValueDisabled', () => { - const options = [{ data: { value: 'disabled', disabled: true } }, { data: { value: 'pass' } }]; + const options = [ + { data: { value: 'disabled', disabled: true } }, + { data: { value: 'pass' } }, + ]; expect(isValueDisabled('disabled', options)).toBeTruthy(); expect(isValueDisabled('pass', options)).toBeFalsy(); expect(isValueDisabled('not-exist', options)).toBeFalsy(); }); - - it('isDisabled', () => { - expect(isDisabled({ data: { disabled: true } }, 'select')).toBeTruthy(); - expect(isDisabled({ data: { disableCheckbox: true } }, 'select')).toBeFalsy(); - expect(isDisabled({ data: { disabled: true } }, 'checkbox')).toBeTruthy(); - expect(isDisabled({ data: { disableCheckbox: true } }, 'checkbox')).toBeTruthy(); - expect(isDisabled({ data: { disabled: true } }, null)).toBeFalsy(); - }); }); From e639678ee83f56d0de4adea97a0545d44ff7299b Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 1 Jun 2020 19:05:32 +0800 Subject: [PATCH 11/17] update --- tests/Select.tree.spec.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tests/Select.tree.spec.js b/tests/Select.tree.spec.js index a99297b6..89a7d458 100644 --- a/tests/Select.tree.spec.js +++ b/tests/Select.tree.spec.js @@ -11,6 +11,7 @@ describe('TreeSelect.tree', () => { + invalid element @@ -70,11 +71,7 @@ describe('TreeSelect.tree', () => { it('warning if node key are not same as value', () => { resetWarned(); const spy = jest.spyOn(console, 'error').mockImplementation(() => {}); - mount( - , - ); + mount(); expect(spy).toHaveBeenCalledWith( 'Warning: `key` or `value` with TreeNode must be the same or you can remove one of them. key: little, value: ttt.', ); @@ -86,15 +83,10 @@ describe('TreeSelect.tree', () => { const spy = jest.spyOn(console, 'error').mockImplementation(() => {}); mount( , ); - expect(spy).toHaveBeenCalledWith( - 'Warning: Same `value` exist in the tree: ttt', - ); + expect(spy).toHaveBeenCalledWith('Warning: Same `value` exist in the tree: ttt'); spy.mockRestore(); }); From 8ebaf88f5e94aa156f4973c3dc6dcd448877338a Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 1 Jun 2020 19:07:02 +0800 Subject: [PATCH 12/17] update --- src/utils/legacyUtil.tsx | 4 ++-- src/utils/valueUtil.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/legacyUtil.tsx b/src/utils/legacyUtil.tsx index 068ee437..8ebed7ba 100644 --- a/src/utils/legacyUtil.tsx +++ b/src/utils/legacyUtil.tsx @@ -36,7 +36,7 @@ export function convertChildrenToData(nodes: React.ReactNode): DataNode[] { return data; }) - .filter(data => data); + .filter(Boolean); } export function fillLegacyProps(dataNode: DataNode): LegacyDataNode { @@ -105,7 +105,7 @@ export function fillAdditionalInfo( } return null; }) - .filter(node => node); + .filter(Boolean); } if (!nodeList) { diff --git a/src/utils/valueUtil.ts b/src/utils/valueUtil.ts index 1a21a23c..839480a0 100644 --- a/src/utils/valueUtil.ts +++ b/src/utils/valueUtil.ts @@ -147,7 +147,7 @@ export function filterOptions( } return null; }) - .filter(node => node); + .filter(Boolean); } return dig(options); From 87476147635765a9fd8c6742f6f9f397e366b975 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 1 Jun 2020 19:17:52 +0800 Subject: [PATCH 13/17] keyUp --- tests/Select.spec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Select.spec.js b/tests/Select.spec.js index ba5faf45..c3b91998 100644 --- a/tests/Select.spec.js +++ b/tests/Select.spec.js @@ -411,6 +411,14 @@ describe('TreeSelect.basic', () => { wrapper.update(); } + function keyUp(code) { + wrapper + .find('input') + .first() + .simulate('keyUp', { which: code }); + wrapper.update(); + } + function matchValue(value) { expect(onChange).toHaveBeenCalledWith(value, expect.anything(), expect.anything()); onChange.mockReset(); @@ -419,11 +427,15 @@ describe('TreeSelect.basic', () => { wrapper.openSelect(); keyDown(KeyCode.DOWN); + keyUp(KeyCode.DOWN); keyDown(KeyCode.ENTER); + keyUp(KeyCode.ENTER); matchValue(['parent']); keyDown(KeyCode.UP); + keyUp(KeyCode.UP); keyDown(KeyCode.ENTER); + keyUp(KeyCode.ENTER); matchValue(['parent', 'child']); }); }); From 41f2d5cea706df7c0ed6e55ff1c20ab858ea8715 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Fri, 5 Jun 2020 10:32:03 +0800 Subject: [PATCH 14/17] revert some code and add ignoreDisabledCheck --- src/OptionList.tsx | 2 +- src/hooks/useKeyValueMapping.ts | 50 +++++++++++++++++++++++--- src/hooks/useSelectValues.ts | 9 +++-- src/utils/valueUtil.ts | 16 +++------ tests/Select.props.spec.js | 62 ++++++++------------------------- tests/utils.spec.js | 14 +++++--- 6 files changed, 82 insertions(+), 71 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index e2d81aa8..0ff39c87 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -170,7 +170,7 @@ const OptionList: React.RefForwardingComponent { - const entity = getEntityByKey(key); + const entity = getEntityByKey(key, checkable ? 'checkbox' : 'select'); if (entity !== null) { onSelect(entity.data.value, { selected: !checkedKeys.includes(entity.data.value), diff --git a/src/hooks/useKeyValueMapping.ts b/src/hooks/useKeyValueMapping.ts index 5d2dcc34..67f0585d 100644 --- a/src/hooks/useKeyValueMapping.ts +++ b/src/hooks/useKeyValueMapping.ts @@ -3,15 +3,55 @@ import { FlattenDataNode, Key, RawValueType } from '../interface'; export type SkipType = null | 'select' | 'checkbox'; +export function isDisabled(dataNode: FlattenDataNode, skipType: SkipType): boolean { + if (!dataNode) { + return true; + } + + const { disabled, disableCheckbox } = dataNode.data; + + switch (skipType) { + case 'select': + return disabled; + case 'checkbox': + return disabled || disableCheckbox; + } + + return false; +} + export default function useKeyValueMapping( cacheKeyMap: Map, cacheValueMap: Map, -): [(key: Key) => FlattenDataNode, (value: RawValueType) => FlattenDataNode] { - const getEntityByKey = React.useCallback((key: Key) => cacheKeyMap.get(key), [cacheKeyMap]); +): [ + (key: Key, skipType?: SkipType, ignoreDisabledCheck?: boolean) => FlattenDataNode, + (value: RawValueType, skipType?: SkipType, ignoreDisabledCheck?: boolean) => FlattenDataNode, +] { + const getEntityByKey = React.useCallback( + (key: Key, skipType: SkipType = 'select', ignoreDisabledCheck?: boolean) => { + const dataNode = cacheKeyMap.get(key); + + if (!ignoreDisabledCheck && isDisabled(dataNode, skipType)) { + return null; + } + + return dataNode; + }, + [cacheKeyMap], + ); + + const getEntityByValue = React.useCallback( + (value: RawValueType, skipType: SkipType = 'select', ignoreDisabledCheck?: boolean) => { + const dataNode = cacheValueMap.get(value); + + if (!ignoreDisabledCheck && isDisabled(dataNode, skipType)) { + return null; + } - const getEntityByValue = React.useCallback((value: RawValueType) => cacheValueMap.get(value), [ - cacheValueMap, - ]); + return dataNode; + }, + [cacheValueMap], + ); return [getEntityByKey, getEntityByValue]; } diff --git a/src/hooks/useSelectValues.ts b/src/hooks/useSelectValues.ts index f5874dd6..c62ea1d9 100644 --- a/src/hooks/useSelectValues.ts +++ b/src/hooks/useSelectValues.ts @@ -2,6 +2,7 @@ import React from 'react'; import { DefaultValueType } from 'rc-select/lib/interface/generator'; import { DataEntity } from 'rc-tree/lib/interface'; import { RawValueType, FlattenDataNode, Key, LabelValueType, DataNode } from '../interface'; +import { SkipType } from './useKeyValueMapping'; import { getRawValueLabeled } from '../utils/valueUtil'; import { formatStrategyKeys, CheckedStrategy } from '../utils/strategyUtil'; @@ -11,8 +12,12 @@ interface Config { value: DefaultValueType; showCheckedStrategy: CheckedStrategy; conductKeyEntities: Record; - getEntityByKey: (key: Key) => FlattenDataNode; - getEntityByValue: (value: RawValueType) => FlattenDataNode; + getEntityByKey: (key: Key, skipType?: SkipType, ignoreDisabledCheck?: boolean) => FlattenDataNode; + getEntityByValue: ( + value: RawValueType, + skipType?: SkipType, + ignoreDisabledCheck?: boolean, + ) => FlattenDataNode; getLabelProp: (node: DataNode) => React.ReactNode; } diff --git a/src/utils/valueUtil.ts b/src/utils/valueUtil.ts index 839480a0..2edc67ab 100644 --- a/src/utils/valueUtil.ts +++ b/src/utils/valueUtil.ts @@ -20,10 +20,7 @@ export function toArray(value: T | T[]): T[] { return value !== undefined ? [value] : []; } -export function findValueOption( - values: RawValueType[], - options: FlattenDataNode[], -): DataNode[] { +export function findValueOption(values: RawValueType[], options: FlattenDataNode[]): DataNode[] { const optionMap: Map = new Map(); options.forEach(flattenItem => { @@ -34,10 +31,7 @@ export function findValueOption( return values.map(val => fillLegacyProps(optionMap.get(val))); } -export function isValueDisabled( - value: RawValueType, - options: FlattenDataNode[], -): boolean { +export function isValueDisabled(value: RawValueType, options: FlattenDataNode[]): boolean { const option = findValueOption([value], options)[0]; if (option) { return option.disabled; @@ -135,8 +129,7 @@ export function filterOptions( .map(dataNode => { const { children } = dataNode; - const match = - keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode)); + const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode)); const childList = dig(children || [], match); if (match || childList.length) { @@ -159,6 +152,7 @@ export function getRawValueLabeled( getEntityByValue: ( value: RawValueType, skipType?: SkipType, + ignoreDisabledCheck?: boolean, ) => FlattenDataNode, getLabelProp: (node: DataNode) => React.ReactNode, ): LabelValueType[] { @@ -172,7 +166,7 @@ export function getRawValueLabeled( return values.map(val => { const item: LabelValueType = { value: val }; - const entity = getEntityByValue(val); + const entity = getEntityByValue(val, 'select', true); const label = entity ? getLabelProp(entity.data) : val; if (valueMap.has(val)) { diff --git a/tests/Select.props.spec.js b/tests/Select.props.spec.js index 5d730a85..7aa06a37 100644 --- a/tests/Select.props.spec.js +++ b/tests/Select.props.spec.js @@ -2,12 +2,7 @@ import React from 'react'; import { mount } from 'enzyme'; import { TreeNode } from 'rc-tree'; -import TreeSelect, { - SHOW_ALL, - SHOW_CHILD, - SHOW_PARENT, - TreeNode as SelectNode, -} from '../src'; +import TreeSelect, { SHOW_ALL, SHOW_CHILD, SHOW_PARENT, TreeNode as SelectNode } from '../src'; // Promisify timeout to let jest catch works function timeoutPromise(delay = 0) { @@ -21,11 +16,11 @@ describe('TreeSelect.props', () => { const createSelect = (props = {}) => (
- - - + + + - +
); @@ -47,9 +42,7 @@ describe('TreeSelect.props', () => { function filterTreeNode(input, child) { return String(child.props.title).indexOf(input) !== -1; } - const wrapper = mount( - createOpenSelect({ filterTreeNode, showSearch: true }), - ); + const wrapper = mount(createOpenSelect({ filterTreeNode, showSearch: true })); wrapper.search('Title 1'); expect(wrapper.find('List').props().data).toHaveLength(1); @@ -58,9 +51,7 @@ describe('TreeSelect.props', () => { }); it('false', () => { - const wrapper = mount( - createOpenSelect({ filterTreeNode: false, showSearch: true }), - ); + const wrapper = mount(createOpenSelect({ filterTreeNode: false, showSearch: true })); wrapper.search('Title 1'); expect(wrapper.find('List').props().data).toHaveLength(4); }); @@ -122,9 +113,7 @@ describe('TreeSelect.props', () => { placeholder: 'RC Component', }), ); - expect(wrapper.find('.rc-tree-select-selection-placeholder').text()).toBe( - 'RC Component', - ); + expect(wrapper.find('.rc-tree-select-selection-placeholder').text()).toBe('RC Component'); }); // https://github.com/ant-design/ant-design/issues/11746 @@ -132,13 +121,7 @@ describe('TreeSelect.props', () => { const treeData = [{ title: 'aaa', value: '111' }]; const Wrapper = props => (
- +
); const wrapper = mount(); @@ -345,16 +328,8 @@ describe('TreeSelect.props', () => { // Since after click will render new TreeNode // [Legacy] FIXME: This is so hard to test wrapper.selectNode(0); - expect(handleChange).toHaveBeenCalledWith( - arg1, - arg2, - expect.anything(), - ); - const { - triggerNode, - allCheckedNodes, - ...rest - } = handleChange.mock.calls[0][2]; + expect(handleChange).toHaveBeenCalledWith(arg1, arg2, expect.anything()); + const { triggerNode, allCheckedNodes, ...rest } = handleChange.mock.calls[0][2]; expect({ ...rest, triggerNode, allCheckedNodes }).toEqual(arg3); }); }); @@ -448,9 +423,7 @@ describe('TreeSelect.props', () => { wrapper.find('.rc-tree-select-tree-switcher').simulate('click'); return timeoutPromise().then(() => { - expect(handleLoadData).toHaveBeenCalledWith( - expect.objectContaining({ value: '0-0' }), - ); + expect(handleLoadData).toHaveBeenCalledWith(expect.objectContaining({ value: '0-0' })); expect(called).toBe(1); expect(wrapper.find('List').props().data).toHaveLength(2); }); @@ -469,9 +442,7 @@ describe('TreeSelect.props', () => { ); expect(loadData).toHaveBeenCalledTimes(1); - expect(loadData).toHaveBeenCalledWith( - expect.objectContaining({ value: '0-1' }), - ); + expect(loadData).toHaveBeenCalledWith(expect.objectContaining({ value: '0-1' })); }); it('getPopupContainer', () => { @@ -489,12 +460,7 @@ describe('TreeSelect.props', () => { const onChange = jest.fn(); const wrapper = mount(
- +
, diff --git a/tests/utils.spec.js b/tests/utils.spec.js index b9d122a3..d178d31a 100644 --- a/tests/utils.spec.js +++ b/tests/utils.spec.js @@ -1,13 +1,19 @@ import { isValueDisabled } from '../src/utils/valueUtil'; +import { isDisabled } from '../src/hooks/useKeyValueMapping'; describe('TreeSelect.util', () => { it('isValueDisabled', () => { - const options = [ - { data: { value: 'disabled', disabled: true } }, - { data: { value: 'pass' } }, - ]; + const options = [{ data: { value: 'disabled', disabled: true } }, { data: { value: 'pass' } }]; expect(isValueDisabled('disabled', options)).toBeTruthy(); expect(isValueDisabled('pass', options)).toBeFalsy(); expect(isValueDisabled('not-exist', options)).toBeFalsy(); }); + + it('isDisabled', () => { + expect(isDisabled({ data: { disabled: true } }, 'select')).toBeTruthy(); + expect(isDisabled({ data: { disableCheckbox: true } }, 'select')).toBeFalsy(); + expect(isDisabled({ data: { disabled: true } }, 'checkbox')).toBeTruthy(); + expect(isDisabled({ data: { disableCheckbox: true } }, 'checkbox')).toBeTruthy(); + expect(isDisabled({ data: { disabled: true } }, null)).toBeFalsy(); + }); }); From 6d6a52af2550fcbfdedd60f8b288a3cbc9169b1b Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Sun, 7 Jun 2020 22:58:57 +0800 Subject: [PATCH 15/17] ignore package-lock.json --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index dc0bfbfa..db00ceae 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ build lib/* coverage yarn.lock +package-lock.json es/* \ No newline at end of file From 0cbb3912d6babdcb2b1cdb4c27b45a10c39a74c3 Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 8 Jun 2020 14:38:43 +0800 Subject: [PATCH 16/17] revert filterFn and some test case --- src/utils/legacyUtil.tsx | 20 +++++--------------- src/utils/valueUtil.ts | 2 +- tests/Select.props.spec.js | 8 ++++---- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/utils/legacyUtil.tsx b/src/utils/legacyUtil.tsx index 8ebed7ba..db1f625f 100644 --- a/src/utils/legacyUtil.tsx +++ b/src/utils/legacyUtil.tsx @@ -36,7 +36,7 @@ export function convertChildrenToData(nodes: React.ReactNode): DataNode[] { return data; }) - .filter(Boolean); + .filter(data => data); } export function fillLegacyProps(dataNode: DataNode): LegacyDataNode { @@ -79,11 +79,7 @@ export function fillAdditionalInfo( const pos = `${level}-${index}`; const included = checkedValues.includes(dataNode.value); const children = dig(dataNode.children || [], pos, included); - const node = ( - - {children.map(child => child.node)} - - ); + const node = {children.map(child => child.node)}; // Link with trigger node if (triggerValue === dataNode.value) { @@ -105,7 +101,7 @@ export function fillAdditionalInfo( } return null; }) - .filter(Boolean); + .filter(node => node); } if (!nodeList) { @@ -137,10 +133,7 @@ export function fillAdditionalInfo( Object.defineProperty(extra, 'triggerNode', { get() { - warning( - false, - '`triggerNode` is deprecated. Please consider decoupling data with node.', - ); + warning(false, '`triggerNode` is deprecated. Please consider decoupling data with node.'); generateMap(); return triggerNode; @@ -148,10 +141,7 @@ export function fillAdditionalInfo( }); Object.defineProperty(extra, 'allCheckedNodes', { get() { - warning( - false, - '`allCheckedNodes` is deprecated. Please consider decoupling data with node.', - ); + warning(false, '`allCheckedNodes` is deprecated. Please consider decoupling data with node.'); generateMap(); if (showPosition) { diff --git a/src/utils/valueUtil.ts b/src/utils/valueUtil.ts index 2edc67ab..7701a8c4 100644 --- a/src/utils/valueUtil.ts +++ b/src/utils/valueUtil.ts @@ -140,7 +140,7 @@ export function filterOptions( } return null; }) - .filter(Boolean); + .filter(node => node); } return dig(options); diff --git a/tests/Select.props.spec.js b/tests/Select.props.spec.js index 7aa06a37..80c3faf0 100644 --- a/tests/Select.props.spec.js +++ b/tests/Select.props.spec.js @@ -16,11 +16,11 @@ describe('TreeSelect.props', () => { const createSelect = (props = {}) => (
- - - + + + - +
); From f567d87f5db142dc788fec42fa31ef831d5f392f Mon Sep 17 00:00:00 2001 From: yoyo837 Date: Mon, 8 Jun 2020 14:45:52 +0800 Subject: [PATCH 17/17] add test case --- tests/Select.spec.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/Select.spec.js b/tests/Select.spec.js index c3b91998..a5ad656f 100644 --- a/tests/Select.spec.js +++ b/tests/Select.spec.js @@ -128,6 +128,16 @@ describe('TreeSelect.basic', () => { expect(wrapper.getSelection(0).text()).toEqual('label0'); }); + it('sets default value(disabled)', () => { + const wrapper = mount( + , + ); + expect(wrapper.getSelection(0).text()).toEqual('label0'); + }); + it('select value twice', () => { const onChange = jest.fn(); const wrapper = mount( @@ -150,7 +160,7 @@ describe('TreeSelect.basic', () => { it('can be controlled by value', () => { const treeData = [ { key: '0', value: '0', title: 'label0' }, - { key: '1', value: '1', title: 'label1', disabled: true }, + { key: '1', value: '1', title: 'label1' }, ]; const wrapper = mount(); expect(wrapper.getSelection(0).text()).toEqual('label0');