MobileCascader.jsx 1.7 KB
Newer Older
钟是志's avatar
钟是志 committed
1
import React, {useMemo, Component} from 'react';
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import {List, Picker} from 'antd-mobile';
import styles from './styles.less';

const getItemChildren = (childrenList, parent) => {
  if (!childrenList || !childrenList.length) {
    return [];
  }
  for (let item of childrenList) {
    if (item.label.indexOf(parent.label) > -1) {
      item.label = item.label.replace(parent.label, '');
    }
    for (let proviceName of ['内蒙古', '广西', '新疆', '香港', '澳门', '西藏', '宁夏']) {
      item.label = item.label.replace(proviceName, '');
    }
    if (item.children) {
      item.children = getItemChildren(item.children, parent);
    }
  }
  return childrenList;
}

钟是志's avatar
钟是志 committed
23 24 25 26 27 28 29
export default class MobileCascader extends Component{
  constructor(props) {
    super(props);
  }
  render() {
    const {onChange, options, label, disabled, value, json, otherProps} = this.props;
    let opt = options &&
30 31 32 33 34 35 36 37
      Array.isArray(options) &&
      options.map((x) => {
        return {
          label: x.label,
          value: x.value,
          children: getItemChildren(x.children, x),
        };
      });
钟是志's avatar
钟是志 committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    return (
      <div className={styles.mobileFormInput}>
        <Picker
          data={opt || []}
          cascade={true}
          // cols={1}
          disabled={disabled}
          value={value}
          extra={disabled ? '' : json.placeholder || '点击选择'}
          onChange={(val) => {
            console.log(val);
            onChange(val || undefined);
          }}
          {...otherProps?.props}
        >
          <List.Item arrow={disabled ? '' : 'horizontal'}>
            {/*{label}*/}
          </List.Item>
        </Picker>
      </div>
    );
  }
60
}
钟是志's avatar
钟是志 committed
61 62 63 64 65
//
//
// export default function MobileCascader(props) { // 级联选择
//
// }