Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #412 and picker demo #459

Merged
merged 1 commit into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions components/picker/cascader/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import arrayTreeFilter from 'array-tree-filter';
import React from 'react';
import MultiPicker from '../MultiPicker';
import Picker from '../Picker';
import { CascaderProps } from './CascaderTypes';
import { CascaderDataItem, CascaderProps } from './CascaderTypes';

class Cascader extends React.Component<CascaderProps, any> {
static defaultProps = {
Expand Down Expand Up @@ -53,17 +53,34 @@ class Cascader extends React.Component<CascaderProps, any> {

getValue(d: any, val: any) {
let data = d || this.props.data;
let value = val || this.props.value || this.props.defaultValue;
if (!value || !value.length || value.indexOf(undefined) > -1) {
value = [];
for (let i = 0; i < this.props.cols!; i++) {
if (data && data.length) {
value[i] = data[0].value;
data = data[0].children;
const value = val || this.props.value || this.props.defaultValue;
let level = 0;
const nextValue = [];

if (value && value.length) {
do {
const index = (data as CascaderDataItem[]).findIndex(item => item.value === value[level]);

if (index < 0) {
break;
}

nextValue[level] = value[level];
level += 1;
data = data[index].children || [];
} while (data.length > 0);
}

for (let i = level; i < this.props.cols!; i++) {
if (data && data.length) {
nextValue[i] = data[0].value;
data = data[0].children;
} else {
break;
}
}
return value;

return nextValue;
}

getCols() {
Expand Down
4 changes: 1 addition & 3 deletions components/picker/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ export default class PopupExample extends React.Component<any, any> {
value={this.state.value}
onChange={this.onChange}
>
<List.Item arrow="horizontal" onPress={this.onPress}>
省市选择
</List.Item>
<List.Item arrow="horizontal">省市选择</List.Item>
</Picker>
<Picker
data={this.state.data}
Expand Down