1 | import React, { useState, useEffect, forwardRef, useImperativeHandle } from 'react';
|
2 | import Popup from '../popup';
|
3 | import { mergeProps } from '../../utils/with-default-props';
|
4 | import { withNativeProps } from '../../utils/native-props';
|
5 | import { usePropsValue } from '../../utils/use-props-value';
|
6 | import CascaderView from '../cascader-view';
|
7 | import { useConfig } from '../config-provider';
|
8 | import { useCascaderValueExtend } from '../cascader-view/use-cascader-value-extend';
|
9 | import { useFieldNames } from '../../hooks';
|
10 | const classPrefix = `adm-cascader`;
|
11 | const defaultProps = {
|
12 | defaultValue: [],
|
13 | destroyOnClose: true,
|
14 | forceRender: false
|
15 | };
|
16 | export const Cascader = forwardRef((p, ref) => {
|
17 | var _a;
|
18 | const {
|
19 | locale
|
20 | } = useConfig();
|
21 | const props = mergeProps(defaultProps, {
|
22 | confirmText: locale.common.confirm,
|
23 | cancelText: locale.common.cancel,
|
24 | placeholder: locale.Cascader.placeholder
|
25 | }, p);
|
26 | const [visible, setVisible] = usePropsValue({
|
27 | value: props.visible,
|
28 | defaultValue: false,
|
29 | onChange: v => {
|
30 | var _a;
|
31 | if (v === false) {
|
32 | (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props);
|
33 | }
|
34 | }
|
35 | });
|
36 | const actions = {
|
37 | toggle: () => {
|
38 | setVisible(v => !v);
|
39 | },
|
40 | open: () => {
|
41 | setVisible(true);
|
42 | },
|
43 | close: () => {
|
44 | setVisible(false);
|
45 | }
|
46 | };
|
47 | useImperativeHandle(ref, () => actions);
|
48 | const [value, setValue] = usePropsValue(Object.assign(Object.assign({}, props), {
|
49 | onChange: val => {
|
50 | var _a;
|
51 | (_a = props.onConfirm) === null || _a === void 0 ? void 0 : _a.call(props, val, generateValueExtend(val));
|
52 | }
|
53 | }));
|
54 | const [, valueName, childrenName] = useFieldNames(props.fieldNames);
|
55 | const generateValueExtend = useCascaderValueExtend(props.options, {
|
56 | valueName,
|
57 | childrenName
|
58 | });
|
59 | const [innerValue, setInnerValue] = useState(value);
|
60 | useEffect(() => {
|
61 | if (!visible) {
|
62 | setInnerValue(value);
|
63 | }
|
64 | }, [visible, value]);
|
65 | const cascaderElement = withNativeProps(props, React.createElement("div", {
|
66 | className: classPrefix
|
67 | }, React.createElement("div", {
|
68 | className: `${classPrefix}-header`
|
69 | }, React.createElement("a", {
|
70 | className: `${classPrefix}-header-button`,
|
71 | onClick: () => {
|
72 | var _a;
|
73 | (_a = props.onCancel) === null || _a === void 0 ? void 0 : _a.call(props);
|
74 | setVisible(false);
|
75 | }
|
76 | }, props.cancelText), React.createElement("div", {
|
77 | className: `${classPrefix}-header-title`
|
78 | }, props.title), React.createElement("a", {
|
79 | className: `${classPrefix}-header-button`,
|
80 | onClick: () => {
|
81 | setValue(innerValue, true);
|
82 | setVisible(false);
|
83 | }
|
84 | }, props.confirmText)), React.createElement("div", {
|
85 | className: `${classPrefix}-body`
|
86 | }, React.createElement(CascaderView, Object.assign({}, props, {
|
87 | value: innerValue,
|
88 | onChange: (val, ext) => {
|
89 | var _a;
|
90 | setInnerValue(val);
|
91 | if (visible) {
|
92 | (_a = props.onSelect) === null || _a === void 0 ? void 0 : _a.call(props, val, ext);
|
93 | }
|
94 | }
|
95 | })))));
|
96 | const popupElement = React.createElement(Popup, {
|
97 | visible: visible,
|
98 | position: 'bottom',
|
99 | onMaskClick: () => {
|
100 | var _a;
|
101 | (_a = props.onCancel) === null || _a === void 0 ? void 0 : _a.call(props);
|
102 | setVisible(false);
|
103 | },
|
104 | getContainer: props.getContainer,
|
105 | destroyOnClose: props.destroyOnClose,
|
106 | forceRender: props.forceRender,
|
107 | afterShow: props.afterShow,
|
108 | afterClose: props.afterClose,
|
109 | onClick: props.onClick,
|
110 | stopPropagation: props.stopPropagation
|
111 | }, cascaderElement);
|
112 | return React.createElement(React.Fragment, null, popupElement, (_a = props.children) === null || _a === void 0 ? void 0 : _a.call(props, generateValueExtend(value).items, actions));
|
113 | }); |
\ | No newline at end of file |