From 229f6421add46158944f96a3bbd61f2be9782db0 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Wed, 21 Jul 2021 17:59:50 +0800 Subject: [PATCH] refactor: picker --- .../generatePicker/generateSinglePicker.tsx | 22 ++- components/vc-picker/Picker.tsx | 1 + components/vc-picker/PickerPanel.tsx | 4 +- .../panels/TimePanel/TimeUnitColumn.tsx | 143 +++++++++--------- examples/App.vue | 33 ++-- 5 files changed, 114 insertions(+), 89 deletions(-) diff --git a/components/date-picker/generatePicker/generateSinglePicker.tsx b/components/date-picker/generatePicker/generateSinglePicker.tsx index 3f4115cf9..9314a4805 100644 --- a/components/date-picker/generatePicker/generateSinglePicker.tsx +++ b/components/date-picker/generatePicker/generateSinglePicker.tsx @@ -23,7 +23,16 @@ export default function generatePicker(generateConfig: GenerateConfig< return defineComponent({ name: displayName, inheritAttrs: false, - props: ['size', 'prefixCls', 'direction', 'getPopupContainer', 'locale', 'value'] as any, + props: [ + 'size', + 'prefixCls', + 'direction', + 'getPopupContainer', + 'locale', + 'value', + 'showTime', + 'showNow', + ] as any, slots: ['suffixIcon'], emits: ['change', 'panelChange', 'ok', 'openChange', 'update:value'], setup(props, { slots, expose, attrs, emit }) { @@ -56,7 +65,8 @@ export default function generatePicker(generateConfig: GenerateConfig< showToday = true, ...restProps } = p; - const { format, showTime } = p as any; + const showTime = p.showTime === '' ? true : p.showTime; + const { format } = p as any; let additionalOverrideProps: any = {}; if (picker) { @@ -66,7 +76,13 @@ export default function generatePicker(generateConfig: GenerateConfig< additionalOverrideProps = { ...additionalOverrideProps, - ...(showTime ? getTimeProps({ format, picker: mergedPicker, ...showTime }) : {}), + ...(showTime + ? getTimeProps({ + format, + picker: mergedPicker, + ...(typeof showTime === 'object' ? showTime : {}), + }) + : {}), ...(mergedPicker === 'time' ? getTimeProps({ format, ...p, picker: mergedPicker }) : {}), diff --git a/components/vc-picker/Picker.tsx b/components/vc-picker/Picker.tsx index 3be446ffd..3ce1492af 100644 --- a/components/vc-picker/Picker.tsx +++ b/components/vc-picker/Picker.tsx @@ -91,6 +91,7 @@ export type PickerSharedProps = { autocomplete?: string; direction?: 'ltr' | 'rtl'; showToday?: boolean; + showTime?: boolean | undefined | object; } & HtmlHTMLAttributes; type OmitPanelProps = Omit< diff --git a/components/vc-picker/PickerPanel.tsx b/components/vc-picker/PickerPanel.tsx index fd133e1ed..1c0a14af8 100644 --- a/components/vc-picker/PickerPanel.tsx +++ b/components/vc-picker/PickerPanel.tsx @@ -134,8 +134,8 @@ function PickerPanel() { mode: String, picker: { type: String, default: 'date' }, tabindex: { type: [Number, String], default: 0 }, - showNow: Boolean, - showTime: Boolean, + showNow: { type: Boolean, default: undefined }, + showTime: [Boolean, Object], showToday: Boolean, renderExtraFooter: Function, hideHeader: Boolean, diff --git a/components/vc-picker/panels/TimePanel/TimeUnitColumn.tsx b/components/vc-picker/panels/TimePanel/TimeUnitColumn.tsx index ea7ae7ad2..c03b64b7b 100644 --- a/components/vc-picker/panels/TimePanel/TimeUnitColumn.tsx +++ b/components/vc-picker/panels/TimePanel/TimeUnitColumn.tsx @@ -3,6 +3,7 @@ import { useInjectPanel } from '../../PanelContext'; import classNames from '../../../_util/classNames'; import { ref } from '@vue/reactivity'; import { onBeforeUnmount, watch } from '@vue/runtime-core'; +import { defineComponent } from 'vue'; export type Unit = { label: any; @@ -19,79 +20,79 @@ export type TimeUnitColumnProps = { onSelect?: (value: number) => void; }; -function TimeUnitColumn(props: TimeUnitColumnProps) { - const { prefixCls, units, onSelect, value, active, hideDisabledOptions } = props; - const cellPrefixCls = `${prefixCls}-cell`; - const { open } = useInjectPanel(); +export default defineComponent({ + name: 'TimeUnitColumn', + props: ['prefixCls', 'units', 'onSelect', 'value', 'active', 'hideDisabledOptions'] as any, + setup(props) { + const { open } = useInjectPanel(); - const ulRef = ref(null); - const liRefs = ref>(new Map()); - const scrollRef = ref(); + const ulRef = ref(null); + const liRefs = ref>(new Map()); + const scrollRef = ref(); - watch( - () => props.value, - () => { - const li = liRefs.value.get(value!); - if (li && open.value !== false) { - scrollTo(ulRef.value!, li.offsetTop, 120); - } - }, - ); - onBeforeUnmount(() => { - scrollRef.value?.(); - }); - - watch(open, () => { - scrollRef.value?.(); - if (open.value) { - const li = liRefs.value.get(value!); - if (li) { - scrollRef.value = waitElementReady(li, () => { - scrollTo(ulRef.value!, li.offsetTop, 0); - }); - } - } - }); - - return ( -
    - {units!.map(unit => { - if (hideDisabledOptions && unit.disabled) { - return null; + watch( + () => props.value, + () => { + const li = liRefs.value.get(props.value!); + if (li && open.value !== false) { + scrollTo(ulRef.value!, li.offsetTop, 120); } + }, + ); + onBeforeUnmount(() => { + scrollRef.value?.(); + }); - return ( -
  • { - liRefs.value.set(unit.value, element as HTMLElement); - }} - class={classNames(cellPrefixCls, { - [`${cellPrefixCls}-disabled`]: unit.disabled, - [`${cellPrefixCls}-selected`]: value === unit.value, - })} - onClick={() => { - if (unit.disabled) { - return; - } - onSelect!(unit.value); - }} - > -
    {unit.label}
    -
  • - ); - })} -
- ); -} + watch(open, () => { + scrollRef.value?.(); + if (open.value) { + const li = liRefs.value.get(props.value!); + if (li) { + scrollRef.value = waitElementReady(li, () => { + scrollTo(ulRef.value!, li.offsetTop, 0); + }); + } + } + }); + return () => { + const { prefixCls, units, onSelect, value, active, hideDisabledOptions } = props; + const cellPrefixCls = `${prefixCls}-cell`; + return ( +
    + {units!.map(unit => { + if (hideDisabledOptions && unit.disabled) { + return null; + } -TimeUnitColumn.displayName = 'TimeUnitColumn'; -TimeUnitColumn.inheritAttrs = false; - -export default TimeUnitColumn; + return ( +
  • { + liRefs.value.set(unit.value, element as HTMLElement); + }} + class={classNames(cellPrefixCls, { + [`${cellPrefixCls}-disabled`]: unit.disabled, + [`${cellPrefixCls}-selected`]: value === unit.value, + })} + onClick={() => { + if (unit.disabled) { + return; + } + onSelect!(unit.value); + }} + > +
    {unit.label}
    +
  • + ); + })} +
+ ); + }; + }, +}); diff --git a/examples/App.vue b/examples/App.vue index ee2847bb5..875c510ff 100644 --- a/examples/App.vue +++ b/examples/App.vue @@ -1,25 +1,32 @@