Skip to content

Commit

Permalink
type: React.Key => SafeKey (#578)
Browse files Browse the repository at this point in the history
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: afc163 <afc163@gmail.com>
  • Loading branch information
dependabot[bot] and afc163 authored Oct 15, 2024
1 parent 80d359d commit 72d007a
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 147 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lint-staged
19 changes: 4 additions & 15 deletions examples/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const errorStyle = {
};

class TreeSelectInput extends Component<{
onChange?: Function;
onChange?: (value: string[]) => void;
style: React.CSSProperties;
}> {
onChange = (value, ...args) => {
Expand Down Expand Up @@ -105,11 +105,7 @@ const Demo = () => {
>
{(control, { errors }) => (
<div>
<TreeSelectInput
{...tProps}
{...control}
style={{ width: 300 }}
/>
<TreeSelectInput {...tProps} {...control} style={{ width: 300 }} />

<p style={errorStyle}>{errors.join(',')}</p>
</div>
Expand All @@ -122,18 +118,11 @@ const Demo = () => {
<div>
<Field
name="select"
rules={[
{ required: true, type: 'array', message: 'select 需要必填' },
]}
rules={[{ required: true, type: 'array', message: 'select 需要必填' }]}
>
{(control, { errors }) => (
<div>
<Select
style={{ width: 200 }}
{...control}
allowClear
mode="multiple"
>
<Select style={{ width: 200 }} {...control} allowClear mode="multiple">
<Option value="jack">jack</Option>
<Option value="lucy">lucy</Option>
<Option value="disabled" disabled>
Expand Down
39 changes: 22 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,54 @@
"compile": "father build",
"lint": "eslint src/ examples/ --ext .tsx,.ts,.jsx,.js",
"now-build": "npm run build",
"prepare": "dumi setup",
"prepare": "husky && dumi setup",
"prepublishOnly": "npm run compile && np --yolo --no-publish --any-branch",
"prettier": "prettier '{src,tests}/**/*.{ts,tsx}' 'tests/**/*.js' --write",
"start": "dumi dev",
"test": "rc-test"
},
"lint-staged": {
"*": "prettier --write --ignore-unknown"
},
"dependencies": {
"@babel/runtime": "^7.25.7",
"classnames": "2.x",
"rc-select": "~14.15.0",
"rc-tree": "~5.9.0",
"rc-select": "~14.15.2",
"rc-tree": "~5.10.0",
"rc-util": "^5.43.0"
},
"devDependencies": {
"@rc-component/father-plugin": "^1.0.0",
"@rc-component/trigger": "^1.5.0",
"@testing-library/react": "^12.0.0",
"@types/jest": "^29.5.12",
"@rc-component/father-plugin": "^1.1.0",
"@rc-component/trigger": "^1.18.3",
"@testing-library/react": "^12.1.5",
"@types/jest": "^29.5.13",
"@types/node": "^22.7.5",
"@types/react": "^16.8.19",
"@types/react-dom": "^18.2.19",
"@types/warning": "^3.0.0",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.1",
"@types/warning": "^3.0.3",
"@umijs/fabric": "^4.0.1",
"cheerio": "1.0.0-rc.12",
"cross-env": "^7.0.3",
"dumi": "^2.4.12",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.8",
"enzyme-to-json": "^3.6.2",
"eslint": "^8.56.0",
"eslint-plugin-jest": "^27.6.0",
"eslint": "^8.57.1",
"eslint-plugin-jest": "^27.9.0",
"eslint-plugin-unicorn": "^56.0.0",
"father": "^4.5.0",
"glob": "^11.0.0",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"np": "^10.0.7",
"prettier": "^3.3.3",
"rc-dialog": "^9.6.0",
"rc-field-form": "^2.4.0",
"rc-test": "^7.1.0",
"rc-test": "^7.1.1",
"rc-virtual-list": "^3.14.8",
"react": "16.x",
"react-dom": "16.x",
"typescript": "^5.6.3",
"cheerio": "1.0.0-rc.12"
"react": "^16.0.0",
"react-dom": "^16.0.0",
"typescript": "^5.6.3"
},
"peerDependencies": {
"react": "*",
Expand Down
18 changes: 9 additions & 9 deletions src/LegacyContext.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import * as React from 'react';
import type { DataEntity, IconType } from 'rc-tree/lib/interface';
import type { Key, LegacyDataNode, RawValueType } from './interface';
import type { LegacyDataNode, SafeKey } from './interface';

interface LegacyContextProps {
checkable: boolean | React.ReactNode;
checkedKeys: Key[];
halfCheckedKeys: Key[];
treeExpandedKeys: Key[];
treeDefaultExpandedKeys: Key[];
onTreeExpand: (keys: Key[]) => void;
checkedKeys: SafeKey[];
halfCheckedKeys: SafeKey[];
treeExpandedKeys: SafeKey[];
treeDefaultExpandedKeys: SafeKey[];
onTreeExpand: (keys: SafeKey[]) => void;
treeDefaultExpandAll: boolean;
treeIcon: IconType;
showTreeIcon: boolean;
switcherIcon: IconType;
treeLine: boolean;
treeNodeFilterProp: string;
treeLoadedKeys: Key[];
treeLoadedKeys: SafeKey[];
treeMotion: any;
loadData: (treeNode: LegacyDataNode) => Promise<unknown>;
onTreeLoad: (loadedKeys: Key[]) => void;
onTreeLoad: (loadedKeys: SafeKey[]) => void;

keyEntities: Record<RawValueType, DataEntity<any>>;
keyEntities: Record<SafeKey, DataEntity<any>>;
}

const LegacySelectContext = React.createContext<LegacyContextProps>(null);
Expand Down
14 changes: 7 additions & 7 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import useMemo from 'rc-util/lib/hooks/useMemo';
import * as React from 'react';
import LegacyContext from './LegacyContext';
import TreeSelectContext from './TreeSelectContext';
import type { Key, TreeDataNode } from './interface';
import type { SafeKey, TreeDataNode } from './interface';
import { getAllKeys, isCheckDisabled } from './utils/valueUtil';

const HIDDEN_STYLE = {
Expand All @@ -23,7 +23,7 @@ const HIDDEN_STYLE = {
};

interface TreeEventInfo {
node: { key: Key };
node: { key: SafeKey };
selected?: boolean;
checked?: boolean;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
);

// ========================== Active ==========================
const [activeKey, setActiveKey] = React.useState<Key>(null);
const [activeKey, setActiveKey] = React.useState<SafeKey>(null);
const activeEntity = keyEntities[activeKey];

// ========================== Values ==========================
Expand Down Expand Up @@ -112,8 +112,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
};

// =========================== Keys ===========================
const [expandedKeys, setExpandedKeys] = React.useState<Key[]>(treeDefaultExpandedKeys);
const [searchExpandedKeys, setSearchExpandedKeys] = React.useState<Key[]>(null);
const [expandedKeys, setExpandedKeys] = React.useState<SafeKey[]>(treeDefaultExpandedKeys);
const [searchExpandedKeys, setSearchExpandedKeys] = React.useState<SafeKey[]>(null);

const mergedExpandedKeys = React.useMemo(() => {
if (treeExpandedKeys) {
Expand All @@ -129,7 +129,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchValue]);

const onInternalExpand = (keys: Key[]) => {
const onInternalExpand = (keys: SafeKey[]) => {
setExpandedKeys(keys);
setSearchExpandedKeys(keys);

Expand All @@ -143,7 +143,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
event.preventDefault();
};

const onInternalSelect = (__: React.Key[], info: TreeEventInfo) => {
const onInternalSelect = (__: SafeKey[], info: TreeEventInfo) => {
const { node } = info;

if (checkable && isCheckDisabled(node)) {
Expand Down
4 changes: 2 additions & 2 deletions src/TreeNode.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* istanbul ignore file */
import type * as React from 'react';
import type { DataNode, Key } from './interface';
import type { DataNode, SafeKey } from './interface';

export interface TreeNodeProps extends Omit<DataNode, 'children'> {
value: Key;
value: SafeKey;
children?: React.ReactNode;
}

Expand Down
Loading

0 comments on commit 72d007a

Please sign in to comment.