9 |
--------------------------------------------------------------------------------
/docs/demo/hover.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: hover
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/panel.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Panel
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/search.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: search
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/value.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: value
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/rc-form.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: rc-form
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/simple.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: animated
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/visible.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: visible
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/animation.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: animation
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/disabled.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: disabled
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/multiple.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: multiple
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | hero:
3 | title: @rc-component/cascader
4 | description: React cascader Component
5 | ---
6 |
7 |
8 |
--------------------------------------------------------------------------------
/docs/demo/text-trigger.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: text-trigger
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/defaultValue.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: defaultValue
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/popup-render.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: popup-render
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/option-render.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: option-render
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/adjust-overflow.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: adjust-overflow
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/change-on-select.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: change-on-select
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/dynamic-options.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: dynamic-options
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/multiple-search.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: multiple-search
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: ✅ test
2 | on: [push, pull_request]
3 | jobs:
4 | test:
5 | uses: react-component/rc-test/.github/workflows/test-npm.yml@main
6 | secrets: inherit
--------------------------------------------------------------------------------
/docs/demo/custom-arrow-icon.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: custom-arrow-icon
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/docs/demo/custom-field-name.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: custom-field-name
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "endOfLine": "lf",
3 | "semi": true,
4 | "singleQuote": true,
5 | "tabWidth": 2,
6 | "trailingComma": "all",
7 | "printWidth": 100,
8 | "arrowParens": "avoid"
9 | }
--------------------------------------------------------------------------------
/docs/demo/default-expand-single-option.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: default-expand-single-option
3 | nav:
4 | title: Demo
5 | path: /demo
6 | ---
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # top-most EditorConfig file
2 | root = true
3 |
4 | # Unix-style newlines with a newline ending every file
5 | [*.{js,css}]
6 | end_of_line = lf
7 | insert_final_newline = true
8 | indent_style = space
9 | indent_size = 2
10 |
--------------------------------------------------------------------------------
/src/index.tsx:
--------------------------------------------------------------------------------
1 | import Cascader from './Cascader';
2 | import Panel from './Panel';
3 |
4 | export type {
5 | BaseOptionType,
6 | DefaultOptionType,
7 | CascaderProps,
8 | FieldNames,
9 | SearchConfig,
10 | CascaderRef,
11 | } from './Cascader';
12 | export { Panel };
13 |
14 | export default Cascader;
15 |
--------------------------------------------------------------------------------
/examples/utils.tsx:
--------------------------------------------------------------------------------
1 | export interface Option {
2 | code?: string;
3 | name?: string;
4 | nodes?: Option[];
5 | disabled?: boolean;
6 | }
7 |
8 | export interface Option2 {
9 | value?: string;
10 | label?: React.ReactNode;
11 | title?: React.ReactNode;
12 | disabled?: boolean;
13 | disableCheckbox?: boolean;
14 | isLeaf?: boolean;
15 | loading?: boolean;
16 | children?: Option2[];
17 | }
18 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "esnext",
4 | "moduleResolution": "node",
5 | "baseUrl": "./",
6 | "jsx": "react",
7 | "declaration": true,
8 | "skipLibCheck": true,
9 | "strict": true,
10 | "esModuleInterop": true,
11 | "paths": {
12 | "@/*": ["src/*"],
13 | "@@/*": ["src/.umi/*"],
14 | "@rc-component/cascader": ["src/index.ts"]
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.dumirc.ts:
--------------------------------------------------------------------------------
1 | import { defineConfig } from 'dumi';
2 | import path from 'path';
3 |
4 | export default defineConfig({
5 | alias: {
6 | 'rc-cascader$': path.resolve('src'),
7 | 'rc-cascader/es': path.resolve('src'),
8 | },
9 | favicons: ['https://avatars0.githubusercontent.com/u/9441414?s=200&v=4'],
10 | themeConfig: {
11 | name: 'Cascader',
12 | logo: 'https://avatars0.githubusercontent.com/u/9441414?s=200&v=4',
13 | },
14 | });
15 |
--------------------------------------------------------------------------------
/src/OptionList/CacheContent.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 |
3 | export interface CacheContentProps {
4 | children?: React.ReactNode;
5 | open?: boolean;
6 | }
7 |
8 | const CacheContent = React.memo(
9 | ({ children }: CacheContentProps) => children as React.ReactElement,
10 | (_, next) => !next.open,
11 | );
12 |
13 | if (process.env.NODE_ENV !== 'production') {
14 | CacheContent.displayName = 'CacheContent';
15 | }
16 |
17 | export default CacheContent;
18 |
--------------------------------------------------------------------------------
/src/OptionList/index.tsx:
--------------------------------------------------------------------------------
1 | import { useBaseProps } from '@rc-component/select';
2 | import type { RefOptionListProps } from '@rc-component/select/lib/OptionList';
3 | import * as React from 'react';
4 | import RawOptionList from './List';
5 |
6 | const RefOptionList = React.forwardRef((props, ref) => {
7 | const baseProps = useBaseProps();
8 |
9 | // >>>>> Render
10 | return ;
11 | });
12 |
13 | export default RefOptionList;
14 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | const base = require('@umijs/fabric/dist/eslint');
2 |
3 | module.exports = {
4 | ...base,
5 | rules: {
6 | ...base.rules,
7 | 'no-template-curly-in-string': 0,
8 | 'prefer-promise-reject-errors': 0,
9 | 'react/no-array-index-key': 0,
10 | 'react/sort-comp': 0,
11 | '@typescript-eslint/no-explicit-any': 0,
12 | 'jsx-a11y/label-has-associated-control': 0,
13 | 'jsx-a11y/label-has-for': 0,
14 | 'import/no-extraneous-dependencies': 0,
15 | 'no-shadow': 0
16 | },
17 | };
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .storybook
2 | *.iml
3 | *.log
4 | .idea
5 | .ipr
6 | .iws
7 | *~
8 | ~*
9 | *.diff
10 | *.patch
11 | *.bak
12 | .DS_Store
13 | Thumbs.db
14 | .project
15 | .*proj
16 | .svn
17 | *.swp
18 | *.swo
19 | *.pyc
20 | *.pyo
21 | node_modules
22 | .cache
23 | *.css
24 | build
25 | lib
26 | es
27 | yarn.lock
28 | package-lock.json
29 | pnpm-lock.yaml
30 | coverage
31 | # umi
32 | .umi
33 | .umi-production
34 | .umi-test
35 | .env.local
36 | .doc
37 |
38 | # dumi
39 | .dumi/tmp
40 | .dumi/tmp-production
41 | dist
42 | .vscode
43 |
44 | bun.lockb
--------------------------------------------------------------------------------
/update-example.js:
--------------------------------------------------------------------------------
1 | /*
2 | 用于 dumi 改造使用,
3 | 可用于将 examples 的文件批量修改为 demo 引入形式,
4 | 其他项目根据具体情况使用。
5 | */
6 |
7 | const fs = require('fs');
8 | const glob = require('glob');
9 |
10 | const paths = glob.sync('./examples/*.jsx');
11 |
12 | paths.forEach(path => {
13 | const name = path.split('/').pop().split('.')[0];
14 | fs.writeFile(
15 | `./docs/demo/${name}.md`,
16 | `## ${name}
17 |
18 |
19 | `,
20 | 'utf8',
21 | function(error) {
22 | if(error){
23 | console.log(error);
24 | return false;
25 | }
26 | console.log(`${name} 更新成功~`);
27 | }
28 | )
29 | });
30 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: npm
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | time: "21:00"
8 | open-pull-requests-limit: 10
9 | ignore:
10 | - dependency-name: np
11 | versions:
12 | - 7.2.0
13 | - 7.3.0
14 | - 7.4.0
15 | - dependency-name: "@types/react"
16 | versions:
17 | - 17.0.0
18 | - 17.0.1
19 | - 17.0.2
20 | - 17.0.3
21 | - dependency-name: "@types/jest"
22 | versions:
23 | - 26.0.20
24 | - 26.0.21
25 | - 26.0.22
26 | - dependency-name: "@types/react-dom"
27 | versions:
28 | - 17.0.0
29 | - 17.0.1
30 | - 17.0.2
31 | - dependency-name: typescript
32 | versions:
33 | - 4.1.3
34 | - 4.1.4
35 | - 4.1.5
36 | - 4.2.2
37 | - 4.2.3
38 |
--------------------------------------------------------------------------------
/tests/private.spec.tsx:
--------------------------------------------------------------------------------
1 | import { render } from '@testing-library/react';
2 | import React from 'react';
3 | import Cascader from '../src';
4 |
5 | describe('Cascader.Private', () => {
6 | it('popupPrefixCls', () => {
7 | const { container } = render(
8 | ,
26 | );
27 |
28 | expect(container.querySelector('.bamboo-dropdown')).toBeTruthy();
29 | expect(container.querySelector('.little-menus')).toBeTruthy();
30 | });
31 | });
32 |
--------------------------------------------------------------------------------
/tests/setup.js:
--------------------------------------------------------------------------------
1 | global.requestAnimationFrame =
2 | global.requestAnimationFrame ||
3 | function requestAnimationFrame(cb) {
4 | return setTimeout(cb, 0);
5 | };
6 |
7 | window.HTMLElement.prototype.scrollIntoView = jest.fn();
8 |
9 | window.MessageChannel = class {
10 | constructor() {
11 | const createPort = () => {
12 | const port = {
13 | onmessage: null,
14 | postMessage: message => {
15 | setTimeout(() => {
16 | if (port._target && typeof port._target.onmessage === 'function') {
17 | port._target.onmessage({ data: message });
18 | }
19 | }, 0);
20 | },
21 | _target: null,
22 | };
23 | return port;
24 | };
25 |
26 | const port1 = createPort();
27 | const port2 = createPort();
28 | port1._target = port2;
29 | port2._target = port1;
30 | this.port1 = port1;
31 | this.port2 = port2;
32 | }
33 | };
34 |
--------------------------------------------------------------------------------
/src/utils/warningPropsUtil.ts:
--------------------------------------------------------------------------------
1 | import warning from '@rc-component/util/lib/warning';
2 | import type { DefaultOptionType, FieldNames } from '../Cascader';
3 |
4 | // value in Cascader options should not be null
5 | export function warningNullOptions(options: DefaultOptionType[], fieldNames: FieldNames) {
6 | if (options) {
7 | const recursiveOptions = (optionsList: DefaultOptionType[]) => {
8 | for (let i = 0; i < optionsList.length; i++) {
9 | const option = optionsList[i];
10 |
11 | if (option[fieldNames?.value as string] === null) {
12 | warning(false, '`value` in Cascader options should not be `null`.');
13 | return true;
14 | }
15 |
16 | if (
17 | Array.isArray(option[fieldNames?.children as string]) &&
18 | recursiveOptions(option[fieldNames?.children as string])
19 | ) {
20 | return true;
21 | }
22 | }
23 | };
24 |
25 | recursiveOptions(options);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: ant-design # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4 | patreon: # Replace with a single Patreon username
5 | open_collective: ant-design # Replace with a single Open Collective username
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
12 | polar: # Replace with a single Polar username
13 | buy_me_a_coffee: # Replace with a single Buy Me a Coffee username
14 | thanks_dev: # Replace with a single thanks.dev username
15 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
16 |
--------------------------------------------------------------------------------
/.github/workflows/codeql.yml:
--------------------------------------------------------------------------------
1 | name: "CodeQL"
2 |
3 | on:
4 | push:
5 | branches: [ "master" ]
6 | pull_request:
7 | branches: [ "master" ]
8 | schedule:
9 | - cron: "2 15 * * 3"
10 |
11 | jobs:
12 | analyze:
13 | name: Analyze
14 | runs-on: ubuntu-latest
15 | permissions:
16 | actions: read
17 | contents: read
18 | security-events: write
19 |
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | language: [ javascript ]
24 |
25 | steps:
26 | - name: Checkout
27 | uses: actions/checkout@v3
28 |
29 | - name: Initialize CodeQL
30 | uses: github/codeql-action/init@v2
31 | with:
32 | languages: ${{ matrix.language }}
33 | queries: +security-and-quality
34 |
35 | - name: Autobuild
36 | uses: github/codeql-action/autobuild@v2
37 |
38 | - name: Perform CodeQL Analysis
39 | uses: github/codeql-action/analyze@v2
40 | with:
41 | category: "/language:${{ matrix.language }}"
42 |
--------------------------------------------------------------------------------
/src/OptionList/useActive.ts:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import CascaderContext from '../context';
3 | import type { LegacyKey } from '../Cascader';
4 |
5 | /**
6 | * Control the active open options path.
7 | */
8 | const useActive = (
9 | multiple?: boolean,
10 | open?: boolean,
11 | ): [LegacyKey[], (activeValueCells: LegacyKey[]) => void] => {
12 | const { values } = React.useContext(CascaderContext);
13 |
14 | const firstValueCells = values[0];
15 |
16 | // Record current dropdown active options
17 | // This also control the open status
18 | const [activeValueCells, setActiveValueCells] = React.useState([]);
19 |
20 | React.useEffect(
21 | () => {
22 | if (!multiple) {
23 | setActiveValueCells(firstValueCells || []);
24 | }
25 | },
26 | /* eslint-disable react-hooks/exhaustive-deps */
27 | [open, firstValueCells],
28 | /* eslint-enable react-hooks/exhaustive-deps */
29 | );
30 |
31 | return [activeValueCells, setActiveValueCells];
32 | };
33 |
34 | export default useActive;
35 |
--------------------------------------------------------------------------------
/src/OptionList/Checkbox.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import { clsx } from 'clsx';
3 | import CascaderContext from '../context';
4 |
5 | export interface CheckboxProps {
6 | prefixCls: string;
7 | checked?: boolean;
8 | halfChecked?: boolean;
9 | disabled?: boolean;
10 | onClick?: React.MouseEventHandler;
11 | disableCheckbox?: boolean;
12 | }
13 |
14 | export default function Checkbox({
15 | prefixCls,
16 | checked,
17 | halfChecked,
18 | disabled,
19 | onClick,
20 | disableCheckbox,
21 | }: CheckboxProps) {
22 | const { checkable } = React.useContext(CascaderContext);
23 |
24 | const customCheckbox = typeof checkable !== 'boolean' ? checkable : null;
25 |
26 | return (
27 |
35 | {customCheckbox}
36 |
37 | );
38 | }
39 |
--------------------------------------------------------------------------------
/examples/multiple-search.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import '../assets/index.less';
3 | import Cascader from '../src';
4 |
5 | const options = [
6 | {
7 | value: 'zhejiang',
8 | label: 'Zhejiang',
9 | children: [
10 | {
11 | value: 'hangzhou',
12 | label: 'Hangzhou',
13 | children: [
14 | {
15 | value: 'xihu',
16 | label: 'West Lake',
17 | },
18 | {
19 | value: 'xiasha',
20 | label: 'Xia Sha',
21 | },
22 | ],
23 | },
24 | ],
25 | },
26 | {
27 | value: 'jiangsu',
28 | label: 'Jiangsu',
29 | children: [
30 | {
31 | value: 'nanjing',
32 | label: 'Nanjing',
33 | children: [
34 | {
35 | value: 'zhonghuamen',
36 | label: 'Zhong Hua men',
37 | },
38 | ],
39 | },
40 | ],
41 | },
42 | ];
43 |
44 | const Demo = () => {
45 | return ;
46 | };
47 |
48 | export default Demo;
49 |
--------------------------------------------------------------------------------
/src/hooks/useMissingValues.ts:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import type { DefaultOptionType, InternalFieldNames, SingleValueType } from '../Cascader';
3 | import { toPathOptions } from '../utils/treeUtil';
4 |
5 | export type GetMissValues = ReturnType;
6 |
7 | export default function useMissingValues(
8 | options: DefaultOptionType[],
9 | fieldNames: InternalFieldNames,
10 | ) {
11 | return React.useCallback(
12 | (rawValues: SingleValueType[]): [SingleValueType[], SingleValueType[]] => {
13 | const missingValues: SingleValueType[] = [];
14 | const existsValues: SingleValueType[] = [];
15 |
16 | rawValues.forEach(valueCell => {
17 | const pathOptions = toPathOptions(valueCell, options, fieldNames);
18 | if (pathOptions.every(opt => opt.option)) {
19 | existsValues.push(valueCell);
20 | } else {
21 | missingValues.push(valueCell);
22 | }
23 | });
24 |
25 | return [existsValues, missingValues];
26 | },
27 | [options, fieldNames],
28 | );
29 | }
30 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014-present alipay.com
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/src/context.ts:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import type {
3 | CascaderProps,
4 | InternalFieldNames,
5 | DefaultOptionType,
6 | SingleValueType,
7 | } from './Cascader';
8 |
9 | export interface CascaderContextProps {
10 | options: NonNullable;
11 | fieldNames: InternalFieldNames;
12 | values: SingleValueType[];
13 | halfValues: SingleValueType[];
14 | changeOnSelect?: boolean;
15 | onSelect: (valuePath: SingleValueType) => void;
16 | checkable?: boolean | React.ReactNode;
17 | searchOptions: DefaultOptionType[];
18 | popupPrefixCls?: string;
19 | loadData?: (selectOptions: DefaultOptionType[]) => void;
20 | expandTrigger?: 'hover' | 'click';
21 | expandIcon?: React.ReactNode;
22 | loadingIcon?: React.ReactNode;
23 | popupMenuColumnStyle?: React.CSSProperties;
24 | optionRender?: CascaderProps['optionRender'];
25 | classNames?: CascaderProps['classNames'];
26 | styles?: CascaderProps['styles'];
27 | }
28 |
29 | const CascaderContext = React.createContext({} as CascaderContextProps);
30 |
31 | export default CascaderContext;
32 |
--------------------------------------------------------------------------------
/src/hooks/useSearchConfig.ts:
--------------------------------------------------------------------------------
1 | import warning from '@rc-component/util/lib/warning';
2 | import * as React from 'react';
3 | import type { CascaderProps, SearchConfig } from '../Cascader';
4 |
5 | // Convert `showSearch` to unique config
6 | export default function useSearchConfig(showSearch?: CascaderProps['showSearch'], props?: any) {
7 | const { autoClearSearchValue, searchValue, onSearch } = props;
8 | return React.useMemo<[boolean, SearchConfig]>(() => {
9 | if (!showSearch) {
10 | return [false, {}];
11 | }
12 |
13 | let searchConfig: SearchConfig = {
14 | matchInputWidth: true,
15 | limit: 50,
16 | autoClearSearchValue,
17 | searchValue,
18 | onSearch,
19 | };
20 |
21 | if (showSearch && typeof showSearch === 'object') {
22 | searchConfig = {
23 | ...searchConfig,
24 | ...showSearch,
25 | };
26 | }
27 |
28 | if ((searchConfig.limit as number) <= 0) {
29 | searchConfig.limit = false;
30 |
31 | if (process.env.NODE_ENV !== 'production') {
32 | warning(false, "'limit' of showSearch should be positive number or false.");
33 | }
34 | }
35 |
36 | return [true, searchConfig];
37 | }, [showSearch, autoClearSearchValue, searchValue, onSearch]);
38 | }
39 |
--------------------------------------------------------------------------------
/src/hooks/useOptions.ts:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import type { DefaultOptionType } from '..';
3 | import type { InternalFieldNames, SingleValueType, LegacyKey } from '../Cascader';
4 | import useEntities, { type GetEntities } from './useEntities';
5 |
6 | export default function useOptions(
7 | mergedFieldNames: InternalFieldNames,
8 | options?: DefaultOptionType[],
9 | ): [
10 | mergedOptions: DefaultOptionType[],
11 | getPathKeyEntities: GetEntities,
12 | getValueByKeyPath: (pathKeys: LegacyKey[]) => SingleValueType[],
13 | ] {
14 | const mergedOptions = React.useMemo(() => options || [], [options]);
15 |
16 | // Only used in multiple mode, this fn will not call in single mode
17 | const getPathKeyEntities = useEntities(mergedOptions, mergedFieldNames);
18 |
19 | /** Convert path key back to value format */
20 | const getValueByKeyPath = React.useCallback(
21 | (pathKeys: LegacyKey[]): SingleValueType[] => {
22 | const keyPathEntities = getPathKeyEntities();
23 |
24 | return pathKeys.map(pathKey => {
25 | const { nodes } = keyPathEntities[pathKey];
26 |
27 | return nodes.map(node => (node as Record)[mergedFieldNames.value]);
28 | });
29 | },
30 | [getPathKeyEntities, mergedFieldNames],
31 | );
32 |
33 | return [mergedOptions, getPathKeyEntities, getValueByKeyPath];
34 | }
35 |
--------------------------------------------------------------------------------
/examples/disabled.tsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import '../assets/index.less';
3 | import Cascader from '../src';
4 |
5 | const addressOptions = [
6 | {
7 | label: '福建',
8 | value: 'fj',
9 | children: [
10 | {
11 | label: '福州',
12 | value: 'fuzhou',
13 | children: [
14 | {
15 | label: '马尾',
16 | value: 'mawei',
17 | },
18 | ],
19 | },
20 | {
21 | label: '泉州',
22 | value: 'quanzhou',
23 | },
24 | ],
25 | },
26 | {
27 | label: '浙江',
28 | value: 'zj',
29 | children: [
30 | {
31 | label: '杭州',
32 | value: 'hangzhou',
33 | children: [
34 | {
35 | label: '余杭',
36 | value: 'yuhang',
37 | },
38 | ],
39 | },
40 | ],
41 | },
42 | {
43 | label: '北京',
44 | value: 'bj',
45 | children: [
46 | {
47 | label: '朝阳区',
48 | value: 'chaoyang',
49 | },
50 | {
51 | label: '海淀区',
52 | value: 'haidian',
53 | },
54 | ],
55 | },
56 | ];
57 |
58 | const Demo = () => {
59 | const [inputValue] = useState('');
60 |
61 | return (
62 |
63 |
64 |
65 | );
66 | };
67 |
68 | export default Demo;
69 |
--------------------------------------------------------------------------------
/examples/option-render.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import '../assets/index.less';
3 | import Cascader from '../src';
4 |
5 | const addressOptions = [
6 | {
7 | label: '福建',
8 | title: '福建-fj',
9 | value: 'fj',
10 | children: [
11 | {
12 | label: '福州',
13 | value: 'fuzhou',
14 | children: [
15 | {
16 | label: '马尾',
17 | value: 'mawei',
18 | },
19 | ],
20 | },
21 | {
22 | label: '泉州',
23 | value: 'quanzhou',
24 | },
25 | ],
26 | },
27 | {
28 | label: '浙江',
29 | value: 'zj',
30 | title: '浙江-zj',
31 | children: [
32 | {
33 | label: '杭州',
34 | value: 'hangzhou',
35 | children: [
36 | {
37 | label: '余杭',
38 | value: 'yuhang',
39 | },
40 | ],
41 | },
42 | ],
43 | },
44 | {
45 | label: '北京',
46 | value: 'bj',
47 | title: '北京-bj',
48 | children: [
49 | {
50 | label: '朝阳区',
51 | value: 'chaoyang',
52 | },
53 | {
54 | label: '海淀区',
55 | value: 'haidian',
56 | },
57 | ],
58 | },
59 | ];
60 |
61 | const Demo = () => {
62 | return (
63 |
64 | {
67 | return ;
68 | }}
69 | />
70 |
71 | );
72 | };
73 |
74 | export default Demo;
75 |
--------------------------------------------------------------------------------
/src/hooks/useValues.ts:
--------------------------------------------------------------------------------
1 | import type { DataEntity } from '@rc-component/tree/lib/interface';
2 | import { conductCheck } from '@rc-component/tree/lib/utils/conductUtil';
3 | import * as React from 'react';
4 | import type { LegacyKey, SingleValueType } from '../Cascader';
5 | import { toPathKeys } from '../utils/commonUtil';
6 | import type { GetMissValues } from './useMissingValues';
7 |
8 | export default function useValues(
9 | multiple: boolean,
10 | rawValues: SingleValueType[],
11 | getPathKeyEntities: () => Record,
12 | getValueByKeyPath: (pathKeys: LegacyKey[]) => SingleValueType[],
13 | getMissingValues: GetMissValues,
14 | ): [
15 | checkedValues: SingleValueType[],
16 | halfCheckedValues: SingleValueType[],
17 | missingCheckedValues: SingleValueType[],
18 | ] {
19 | // Fill `rawValues` with checked conduction values
20 | return React.useMemo(() => {
21 | const [existValues, missingValues] = getMissingValues(rawValues);
22 |
23 | if (!multiple || !rawValues.length) {
24 | return [existValues, [], missingValues];
25 | }
26 |
27 | const keyPathValues = toPathKeys(existValues);
28 | const keyPathEntities = getPathKeyEntities();
29 |
30 | const { checkedKeys, halfCheckedKeys } = conductCheck(keyPathValues, true, keyPathEntities) as {
31 | checkedKeys: LegacyKey[];
32 | halfCheckedKeys: LegacyKey[];
33 | };
34 |
35 | // Convert key back to value cells
36 | return [getValueByKeyPath(checkedKeys), getValueByKeyPath(halfCheckedKeys), missingValues];
37 | }, [multiple, rawValues, getPathKeyEntities, getValueByKeyPath, getMissingValues]);
38 | }
39 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # History
2 |
3 | ## 0.17.0 / 2018/12/14
4 |
5 | - should close popup on double click when changeOnSelect is set
6 |
7 | ## 0.16.0 / 2018/08/23
8 |
9 | - Add loadingIcon.
10 |
11 | ## 0.15.0 / 2018/08/10
12 |
13 | - Add expandIcon.
14 |
15 | ## 0.14.0 / 2018/07/03
16 |
17 | - Fix typo `filedNames` to `fieldNames`. [ant-design/ant-design#10896](https://github.com/ant-design/ant-design/issues/10896)
18 |
19 | ## 0.13.0 / 2018/04/27
20 |
21 | - Allow to custom the field name of options. [#23](https://github.com/react-component/cascader/pull/23)
22 |
23 | ## 0.12.0
24 |
25 | - Add es build
26 | - support React 16
27 |
28 | ## 0.11.0 / 2017-01-17
29 |
30 | - Add Keyboard support
31 |
32 | ## 0.10.0 / 2016-08-20
33 |
34 | - Add `dropdownMenuColumnStyle`
35 | - Fix `changeOnSelect` when load data dynamicly
36 |
37 | ## 0.9.0 / 2016-02-19
38 |
39 | - support `popupPlacement`
40 |
41 | ## 0.8.0 / 2016-01-26
42 |
43 | - Add prop `changeOnSelect`
44 |
45 | ## 0.7.0 / 2016-01-25
46 |
47 | - Support disabled item
48 |
49 | ## 0.6.1 / 2016-01-18
50 |
51 | - Hide popup menu when there is no options, fix #4
52 |
53 | ## 0.6.0 / 2016-01-05
54 |
55 | - Add prop `disabled`.
56 |
57 | ## 0.5.1 / 2015-12-31
58 |
59 | - Always scroll to show active menu item
60 |
61 | ## 0.5.0 / 2015-12-30
62 |
63 | - Remove `onSelect`
64 | - Add `loadData` for dynamicly changing options
65 |
66 | ## 0.4.0 / 2015-12-29
67 |
68 | - Add prop `popupVisible`.
69 | - `onVisibleChange` => `onPopupVisibleChange`.
70 |
71 | ## 0.3.0 / 2015-12-28
72 |
73 | - Fix value and defaultValue.
74 | - Add Test Cases.
75 |
76 | ## 0.2.0 / 2015-12-25
77 |
78 | - Add prop `expandTrigger`.
79 |
80 | ## 0.1.0 / 2015-12-25
81 |
82 | First release.
83 |
--------------------------------------------------------------------------------
/examples/animation.tsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import '../assets/index.less';
3 | import type { CascaderProps } from '../src';
4 | import Cascader from '../src';
5 | import type { Option2 } from './utils';
6 |
7 | const addressOptions = [
8 | {
9 | label: '福建',
10 | value: 'fj',
11 | children: [
12 | {
13 | label: '福州',
14 | value: 'fuzhou',
15 | children: [
16 | {
17 | label: '马尾',
18 | value: 'mawei',
19 | },
20 | ],
21 | },
22 | {
23 | label: '泉州',
24 | value: 'quanzhou',
25 | },
26 | ],
27 | },
28 | {
29 | label: '浙江',
30 | value: 'zj',
31 | children: [
32 | {
33 | label: '杭州',
34 | value: 'hangzhou',
35 | children: [
36 | {
37 | label: '余杭',
38 | value: 'yuhang',
39 | },
40 | ],
41 | },
42 | ],
43 | },
44 | {
45 | label: '北京',
46 | value: 'bj',
47 | children: [
48 | {
49 | label: '朝阳区',
50 | value: 'chaoyang',
51 | },
52 | {
53 | label: '海淀区',
54 | value: 'haidian',
55 | },
56 | ],
57 | },
58 | ];
59 |
60 | const Demo = () => {
61 | const [inputValue, setInputValue] = useState('');
62 |
63 | const onChange: CascaderProps['onChange'] = (value, selectedOptions) => {
64 | console.log(value, selectedOptions);
65 | setInputValue(selectedOptions.map(o => o.label).join(', '));
66 | };
67 |
68 | return (
69 |
70 |
71 |
72 | );
73 | };
74 |
75 | export default Demo;
76 |
--------------------------------------------------------------------------------
/examples/text-trigger.tsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import '../assets/index.less';
3 | import type { CascaderProps } from '../src';
4 | import Cascader from '../src';
5 | import type { Option2 } from './utils';
6 |
7 | const addressOptions = [
8 | {
9 | label: '福建',
10 | value: 'fj',
11 | children: [
12 | {
13 | label: '福州',
14 | value: 'fuzhou',
15 | children: [
16 | {
17 | label: '马尾',
18 | value: 'mawei',
19 | },
20 | ],
21 | },
22 | {
23 | label: '泉州',
24 | value: 'quanzhou',
25 | },
26 | ],
27 | },
28 | {
29 | label: '浙江',
30 | value: 'zj',
31 | children: [
32 | {
33 | label: '杭州',
34 | value: 'hangzhou',
35 | children: [
36 | {
37 | label: '余杭',
38 | value: 'yuhang',
39 | },
40 | ],
41 | },
42 | ],
43 | },
44 | {
45 | label: '北京',
46 | value: 'bj',
47 | children: [
48 | {
49 | label: '朝阳区',
50 | value: 'chaoyang',
51 | },
52 | {
53 | label: '海淀区',
54 | value: 'haidian',
55 | },
56 | ],
57 | },
58 | ];
59 |
60 | const Demo = () => {
61 | const [inputValue, setInputValue] = useState('');
62 |
63 | const onChange: CascaderProps['onChange'] = (value, selectedOptions) => {
64 | console.log(value, selectedOptions);
65 | setInputValue(selectedOptions.map(o => o.label).join(', '));
66 | };
67 |
68 | return (
69 |
70 | {inputValue}
71 |
72 | 切换地区
73 |
74 |
75 | );
76 | };
77 |
78 | export default Demo;
79 |
--------------------------------------------------------------------------------
/examples/dynamic-options.tsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import '../assets/index.less';
3 | import type { CascaderProps } from '../src';
4 | import Cascader from '../src';
5 | import type { Option2 } from './utils';
6 |
7 | const addressOptions = [
8 | {
9 | label: '福建',
10 | isLeaf: false,
11 | value: 'fj',
12 | },
13 | {
14 | label: '浙江',
15 | isLeaf: false,
16 | value: 'zj',
17 | },
18 | ];
19 |
20 | const Demo = () => {
21 | const [inputValue, setInputValue] = useState('');
22 | const [options, setOptions] = useState(addressOptions);
23 |
24 | const onChange: CascaderProps['onChange'] = (value, selectedOptions) => {
25 | console.log('OnChange:', value, selectedOptions);
26 | setInputValue(selectedOptions.map(o => o.label).join(', '));
27 | };
28 |
29 | const loadData: CascaderProps['loadData'] = selectedOptions => {
30 | console.log('onLoad:', selectedOptions);
31 | const targetOption = selectedOptions[selectedOptions.length - 1];
32 | targetOption.loading = true;
33 | // 动态加载下级数据
34 | setTimeout(() => {
35 | targetOption.loading = false;
36 | targetOption.children = [
37 | {
38 | label: `${targetOption.label}动态加载1`,
39 | value: 'dynamic1',
40 | isLeaf: false,
41 | },
42 | {
43 | label: `${targetOption.label}动态加载2`,
44 | value: 'dynamic2',
45 | },
46 | ];
47 | setOptions([...options]);
48 | }, 500);
49 | };
50 |
51 | return (
52 |
59 |
60 |
61 | );
62 | };
63 |
64 | export default Demo;
65 |
--------------------------------------------------------------------------------
/examples/simple.tsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import '../assets/index.less';
3 | import type { CascaderProps } from '../src';
4 | import Cascader from '../src';
5 | import type { Option2 } from './utils';
6 |
7 | const addressOptions = [
8 | {
9 | label: '福建',
10 | value: 'fj',
11 | "aria-label": '福建',
12 | "aria-labelledby": 'fj',
13 | "data-type": 'fj',
14 | children: [
15 | {
16 | label: '福州',
17 | value: 'fuzhou',
18 | children: [
19 | {
20 | label: '马尾',
21 | value: 'mawei',
22 | },
23 | ],
24 | },
25 | {
26 | label: '泉州',
27 | value: 'quanzhou',
28 | },
29 | ],
30 | },
31 | {
32 | label: '浙江',
33 | value: 'zj',
34 | children: [
35 | {
36 | label: '杭州',
37 | value: 'hangzhou',
38 | children: [
39 | {
40 | label: '余杭',
41 | value: 'yuhang',
42 | },
43 | ],
44 | },
45 | ],
46 | },
47 | {
48 | label: '北京',
49 | value: 'bj',
50 | children: [
51 | {
52 | label: '朝阳区',
53 | value: 'chaoyang',
54 | },
55 | {
56 | label: '海淀区',
57 | value: 'haidian',
58 | disabled: true,
59 | },
60 | ],
61 | },
62 | ];
63 |
64 | const Demo = () => {
65 | const [inputValue, setInputValue] = useState('');
66 |
67 | const onChange: CascaderProps['onChange'] = (value, selectedOptions) => {
68 | console.log(value, selectedOptions);
69 | setInputValue(selectedOptions.map(o => o.label).join(', '));
70 | };
71 |
72 | return (
73 |
74 |
75 |
76 | );
77 | };
78 |
79 | export default Demo;
80 |
--------------------------------------------------------------------------------
/examples/custom-field-name.tsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import '../assets/index.less';
3 | import type { CascaderProps } from '../src';
4 | import Cascader from '../src';
5 | import type { Option } from './utils';
6 |
7 | const addressOptions: Option[] = [
8 | {
9 | name: '福建',
10 | code: 'fj',
11 | nodes: [
12 | {
13 | name: '福州',
14 | code: 'fuzhou',
15 | nodes: [
16 | {
17 | name: '马尾',
18 | code: 'mawei',
19 | },
20 | ],
21 | },
22 | {
23 | name: '泉州',
24 | code: 'quanzhou',
25 | },
26 | ],
27 | },
28 | {
29 | name: '浙江',
30 | code: 'zj',
31 | nodes: [
32 | {
33 | name: '杭州',
34 | code: 'hangzhou',
35 | nodes: [
36 | {
37 | name: '余杭',
38 | code: 'yuhang',
39 | },
40 | ],
41 | },
42 | ],
43 | },
44 | {
45 | name: '北京',
46 | code: 'bj',
47 | nodes: [
48 | {
49 | name: '朝阳区',
50 | code: 'chaoyang',
51 | },
52 | {
53 | name: '海淀区',
54 | code: 'haidian',
55 | disabled: true,
56 | },
57 | ],
58 | },
59 | ];
60 |
61 | const Demo = () => {
62 | const [inputValue, setInputValue] = useState('');
63 |
64 | const onChange: CascaderProps