-
Notifications
You must be signed in to change notification settings - Fork 73
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
Popup和Picker结合使用时,.getValue()报错,picker对象中没有此方法? this.props.onOk!(this.picker && this.picker.getValue()) #66
Comments
@yiminghe 同样问题,正在找原因 |
我今天也遇到了这个问题,看了下源码,发现问题出现在popupmixin.js中 getContent = () => {
if (this.props.picker) {
let { pickerValue } = this.state;
if (pickerValue === null) {
pickerValue = this.props.value;
}
return React.cloneElement(this.props.picker, ({
[this.props.pickerValueProp!]: pickerValue,
[this.props.pickerValueChangeProp!]: this.onPickerChange,
ref: this.saveRef,
}));
} else {
return this.props.content;
}
} 这里的this.props.picker 实际上是PickerMixin高阶组件返回的组件,并不是实际的picker。由于ref这个key不能在父子组件中传递,所以ref指向了一个wrapperComponent,getValue方法是不存在的。建议不要使用ref作为key,继续使用saveRef传递到实际的picker组件中。 |
看到m-date-picker里也有使用picker组件,应该也是有问题的吧 |
@slsay 我也遇到同樣的問題,而我是參考 rmc-date-picker 的寫法解決: getDate() {
return this.clipDate(this.state.date || this.getDefaultMinDate());
}
// used by rmc-picker/lib/PopupMixin.js
getValue() {
return this.getDate();
} 這裡的getValue()最後返回的是 DatePicker 的選取值 我的寫法 import PickerElement from 'rmc-picker/lib/Picker';
import Popup from 'rmc-picker/lib/Popup';
import PopupStyles from 'rmc-picker/lib/PopupStyles';
class Picker extends React.Component {
state = {
selectedValue: get(this.props, ['itemList', 0, 'value']),
};
getValue = () => {
return this.state.selectedValue;
};
onValueChange = value => {
this.setState({ selectedValue: value });
};
render() {
const { disabled, itemList } = this.props;
const selectedValue = get(this.state, 'selectedValue');
return (
<PickerElement
onValueChange={value => this.onValueChange(value)}
selectedValue={selectedValue}
disabled={disabled}
>
{itemList.map(item => (
<PickerElement.Item key={item.value} value={item.value}>
{item.label}
</PickerElement.Item>
))}
</PickerElement>
);
}
}
const PopupPicker = props => {
const { disabled, itemList, isVisible, children } = props;
const onOk = get(props, 'onOk', noop);
const onDismiss = get(props, 'onDismiss', noop);
return (
<Popup
picker={<Picker disabled={disabled} itemList={itemList} />}
styles={PopupStyles}
okText={確定}
dismissText={取消}
onOk={value => onOk(value)}
onDismiss={onDismiss}
visible={isVisible}
>
{children}
</Popup>
);
};
export default PopupPicker; 供您參考 |
@yanghaochang104 |
PopupMixin.tsx line:116
Popup和Picker结合使用时,.getValue()报错,picker对象中没有此方法?
The text was updated successfully, but these errors were encountered: