import { createNamespace } from '../utils'; import { preventDefault } from '../utils/dom/event'; import { deepClone } from '../utils/deep-clone'; import { pickerProps } from './shared'; import { BORDER_TOP_BOTTOM, BORDER_UNSET_TOP_BOTTOM } from '../utils/constant'; import Loading from '../loading'; import PickerColumn from './PickerColumn'; const [createComponent, bem, t] = createNamespace('picker'); export default createComponent({ props: { ...pickerProps, defaultIndex: { type: Number, default: 0 }, columns: { type: Array, default: () => [] }, toolbarPosition: { type: String, default: 'top' }, valueKey: { type: String, default: 'text' } }, data() { return { children: [] }; }, computed: { simple() { return this.columns.length && !this.columns[0].values; } }, watch: { columns: 'setColumns' }, methods: { setColumns() { const columns = this.simple ? [{ values: this.columns }] : this.columns; columns.forEach((column, index) => { this.setColumnValues(index, deepClone(column.values)); }); }, emit(event) { if (this.simple) { this.$emit(event, this.getColumnValue(0), this.getColumnIndex(0)); } else { this.$emit(event, this.getValues(), this.getIndexes()); } }, onChange(columnIndex) { if (this.simple) { this.$emit( 'change', this, this.getColumnValue(0), this.getColumnIndex(0) ); } else { this.$emit('change', this, this.getValues(), columnIndex); } }, // get column instance by index getColumn(index) { return this.children[index]; }, // @exposed-api // get column value by index getColumnValue(index) { const column = this.getColumn(index); return column && column.getValue(); }, // @exposed-api // set column value by index setColumnValue(index, value) { const column = this.getColumn(index); column && column.setValue(value); }, // @exposed-api // get column option index by column index getColumnIndex(columnIndex) { return (this.getColumn(columnIndex) || {}).currentIndex; }, // @exposed-api // set column option index by column index setColumnIndex(columnIndex, optionIndex) { const column = this.getColumn(columnIndex); column && column.setIndex(optionIndex); }, // @exposed-api // get options of column by index getColumnValues(index) { return (this.children[index] || {}).options; }, // @exposed-api // set options of column by index setColumnValues(index, options) { const column = this.children[index]; if ( column && JSON.stringify(column.options) !== JSON.stringify(options) ) { column.options = options; column.setIndex(0); } }, // @exposed-api // get values of all columns getValues() { return this.children.map(child => child.getValue()); }, // @exposed-api // set values of all columns setValues(values) { values.forEach((value, index) => { this.setColumnValue(index, value); }); }, // @exposed-api // get indexes of all columns getIndexes() { return this.children.map(child => child.currentIndex); }, // @exposed-api // set indexes of all columns setIndexes(indexes) { indexes.forEach((optionIndex, columnIndex) => { this.setColumnIndex(columnIndex, optionIndex); }); }, onConfirm() { this.children.map(child => child.stopMomentum()); this.emit('confirm'); }, onCancel() { this.emit('cancel'); }, genTitle() { const titleSlot = this.slots('title'); if (titleSlot) { return titleSlot; } if (this.title) { return
{this.title}
; } }, genToolbar() { if (this.showToolbar) { return (
{this.slots() || [ , this.genTitle(), ]}
); } }, genColumns() { const columns = this.simple ? [this.columns] : this.columns; return columns.map((item, index) => ( { this.onChange(index); }} /> )); } }, render(h) { const { itemHeight } = this; const wrapHeight = itemHeight * this.visibleItemCount; const frameStyle = { height: `${itemHeight}px` }; const columnsStyle = { height: `${wrapHeight}px` }; const maskStyle = { backgroundSize: `100% ${(wrapHeight - itemHeight) / 2}px` }; return (
{this.toolbarPosition === 'top' ? this.genToolbar() : h()} {this.loading ? : h()} {this.slots('columns-top')}
{this.genColumns()}
{this.slots('columns-bottom')} {this.toolbarPosition === 'bottom' ? this.genToolbar() : h()}
); } });