UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

93 lines (79 loc) 2.14 kB
/** @jsx createElement */ 'use strict'; import { Component, createElement, PropTypes, unmountComponentAtNode, findDOMNode, render } from 'rax'; import WebPicker from 'nuke-web-picker'; import defaultLocale from './locale'; const noop = () => {}; const pickerInstance = null; const container = null; function formatDataSource(dataSource) { const result = []; dataSource.forEach((item, index) => { result.push({ key: index, value: item }); }); return result; } function formatResult(result) { return { data: result[0].key }; } function show(options = {}, onSelect, onCancel, onShow, onFail) { let { title, dataSource, selectedKey, maskClosable = true, locale = {}, ...others } = options; onSelect = onSelect || noop; onShow = onShow || noop; onCancel = onCancel || noop; onFail = onFail || noop; if (!dataSource) { onFail({ err: 'empty dataSource' }); return; } if (!selectedKey) { if (dataSource[0].children) { selectedKey = [dataSource[0].key, dataSource[0].children[0].key]; } else { selectedKey = dataSource[0].key; } } if (!selectedKey) { if (dataSource[0].children) { selectedKey = [dataSource[0].key, dataSource[0].children[0].key]; } else { selectedKey = [dataSource[0].key]; } } if (typeof selectedKey === 'string') { selectedKey = [selectedKey]; } if (typeof selectedKey === 'number') { selectedKey = [selectedKey.toString()]; } WebPicker.show( { title, dataSource: formatDataSource(dataSource, options.selectedKey), selectedKey, hasToolbar: true, hasToolbarButton: true, locale: { confirm: locale.confirm || defaultLocale.confirm, cancel: locale.cancel || defaultLocale.cancel, }, }, (result) => { onSelect(formatResult(result)); }, onCancel, onShow, onFail ); function onSelectCallback(res) { const result = { success: true }; if (res && res[0] && res.length > 0) { result.data = res[0]; } onSelect(result); } } function hide() { WebPicker.hide(); } export default { show, hide };