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

feat: DateRangePicker和CascaderPicker支持visible和defaultVisible #1158

Merged
merged 2 commits into from
Apr 22, 2024
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
24 changes: 24 additions & 0 deletions compiled/alipay/demo/pages/CascaderPicker/index.axml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@
</ant-button>
</view>
</ant-list>

<ant-container style="margin-top: 20rpx">
<ant-list header="外部控制选择器是否显示">
<ant-list-item>
请选择省市
<ant-cascader-picker
slot="extra"
visible="{{ pickerVisible }}"
placeholder="请选择归属地"
options="{{ cityList }}"
onChange="handleCityPickerChange"
onVisibleChange="handleTriggerControlledPicker"
onOk="handleCityOnOk"
onCancel="handleDismiss" />
</ant-list-item>
</ant-list>

<ant-button
size="medium"
type="primary"
onTap="handleOpenPicker">
打开 CascaderPicker
</ant-button>
</ant-container>
12 changes: 12 additions & 0 deletions compiled/alipay/demo/pages/CascaderPicker/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cityList from './city';
Page({
data: {
pickerVisible: false,
cityList,
value: ['34', '330'],
},
Expand All @@ -23,4 +24,15 @@ Page({
handleDismiss(e) {
console.log('handleDismiss', e);
},
handleTriggerControlledPicker(visible, e) {
console.log('handleTriggerControlledPicker', visible, e);
this.setData({
pickerVisible: visible,
});
},
handleOpenPicker() {
this.setData({
pickerVisible: true,
});
},
});
3 changes: 2 additions & 1 deletion compiled/alipay/demo/pages/CascaderPicker/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ant-cascader-picker": "../../../src/Picker/CascaderPicker/index",
"ant-list-item": "../../../src/List/ListItem/index",
"ant-list": "../../../src/List/index",
"ant-button": "../../../src/Button/index"
"ant-button": "../../../src/Button/index",
"ant-container": "../../../src/Container/index"
}
}
22 changes: 22 additions & 0 deletions compiled/alipay/demo/pages/DatePicker/index.axml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@
打开 DatePicker
</ant-button>
</ant-container>
<ant-container style="margin-top: 20rpx">
<ant-list header="外部控制日期范围选择器是否显示">
<ant-list-item>
请选择日期范围
<ant-range-picker
slot="extra"
visible="{{ dateRangePickerVisible }}"
placeholder="请选择"
defaultValue="{{ defaultDateRange }}"
onPickerChange="handlePickerRangeChange"
onVisibleChange="handleTriggerControlledDateRangePicker"
onOk="handleRangeOk" />
</ant-list-item>
</ant-list>

<ant-button
size="medium"
type="primary"
onTap="handleOpenDateRangePicker">
打开 DateRangePicker
</ant-button>
</ant-container>
12 changes: 12 additions & 0 deletions compiled/alipay/demo/pages/DatePicker/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Page({
data: {
pickerVisible: false,
dateRangePickerVisible: false,
min: new Date('2019/01/15').getTime(),
max: new Date('2023/08/20').getTime(),
defaultDate: new Date('2019/02/02').getTime(),
Expand Down Expand Up @@ -55,4 +56,15 @@ Page({
pickerVisible: true,
});
},
handleTriggerControlledDateRangePicker(visible, e) {
console.log('handleTriggerControlledDateRangePicker', visible, e);
this.setData({
dateRangePickerVisible: visible,
});
},
handleOpenDateRangePicker() {
this.setData({
dateRangePickerVisible: true,
});
},
});
1 change: 1 addition & 0 deletions compiled/alipay/src/DatePicker/RangePicker/index.axml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
formattedValueText="{{ formattedValueText }}"
className="ant-range-picker {{ className || '' }}"
popClassName="{{ !currentStartDate || !currentEndDate ? 'ant-range-picker-confirm-disabled' : '' }} {{ popClassName || '' }}"
visible="{{ state.visible }}"
style="{{ style }}"
animationType="{{ animationType }}"
options="{{ columns }}"
Expand Down
34 changes: 27 additions & 7 deletions compiled/alipay/src/DatePicker/RangePicker/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEvent, useMemo } from 'functional-mini/component';
import { useState, useEvent, useEffect, useMemo } from 'functional-mini/component';
import { DateRangePickerFunctionalProps, IDateRangePickerProps } from './props';
import dayjs from 'dayjs';
import {
Expand Down Expand Up @@ -55,6 +55,26 @@ const RangePicker = (props: IDateRangePickerProps) => {

const onFormatLabel = useFormatLabel(props.onFormatLabel);

const [visible, { update: updateVisible }] = useMixState(
props.defaultVisible,
{
value: props.visible,
}
);

useEffect(() => {
setTimeout(() => {
if (visible) {
const state = init(realValue);
const currentValue = getValueByDate(
state.pickerType === 'start' ? state.start : state.end,
props.precision
);
updateColumns(currentValue, props);
}
})
}, [visible]);

function updateColumns(currentValue, currentProps) {
const { precision, min: propsMin, max: propsMax } = currentProps;
const min = getMin(propsMin);
Expand Down Expand Up @@ -85,12 +105,9 @@ const RangePicker = (props: IDateRangePickerProps) => {
useEvent('onVisibleChange', (event) => {
const visible = resolveEventValue(event)
if (visible) {
const state = init(realValue);
const currentValue = getValueByDate(
state.pickerType === 'start' ? state.start : state.end,
props.precision
);
updateColumns(currentValue, props);
updateVisible(true);
} else {
updateVisible(false);
}
triggerEvent('visibleChange', visible);
});
Expand Down Expand Up @@ -148,6 +165,9 @@ const RangePicker = (props: IDateRangePickerProps) => {
});

return {
state: {
visible
},
formattedValueText,
realValue,
columns,
Expand Down
4 changes: 4 additions & 0 deletions compiled/alipay/src/DatePicker/RangePicker/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type PickerValue = [Date, Date];
* @description 对话框
*/
export interface IDateRangePickerProps extends IBaseProps {
visible?: boolean;
defaultVisible?: boolean;
/**
* @desciption 动画类型
* @default "transform"
Expand Down Expand Up @@ -165,6 +167,8 @@ export const DateRangePickerDefaultProps: Partial<IDateRangePickerProps> = {
};

export const DateRangePickerFunctionalProps: Partial<IDateRangePickerProps> = {
visible: null,
defaultVisible: null,
animationType: 'transform',
format: 'YYYY/MM/DD',
min: null,
Expand Down
2 changes: 2 additions & 0 deletions compiled/alipay/src/DatePicker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ toc: 'content'
| suffix | 后缀 | slot | - |
| title | 弹出框标题 | string \| slot | - |
| value | 选中的时间范围 | [Date, Date] | - |
| visible | 是否显示 | boolean | false |
| defaultVisible | 默认是否显示 | boolean | false |
| onOk | 点击确定按钮,触发回调 | (date: [Date, Date], dateStr: [string, string], event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void | - |
| onCancel | 点击取消按钮/蒙层,触发回调 | (event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void | - |
| onPickerChange | 选中项发生变化,触发回调 | (type: `start` \| `end`, date: Date, dateStr: string, event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void | - |
Expand Down
1 change: 1 addition & 0 deletions compiled/alipay/src/Picker/CascaderPicker/index.axml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
animationType="{{ animationType }}"
popClassName="ant-cascader-picker-popup {{ popClassName || '' }}"
popStyle="{{ popStyle }}"
visible="{{ state.visible }}"
options="{{ columns }}"
value="{{ currentValue }}"
disabled="{{ disabled }}"
Expand Down
31 changes: 26 additions & 5 deletions compiled/alipay/src/Picker/CascaderPicker/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEvent, useMemo, useState } from 'functional-mini/component';
import { useEvent, useMemo, useEffect, useState } from 'functional-mini/component';
import { mountComponent } from '../../_util/component';
import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
import { useComponentUpdateEffect } from '../../_util/hooks/useLayoutEffect';
Expand Down Expand Up @@ -83,12 +83,28 @@ const CascaderPicker = (props: ICascaderProps) => {
return defaultFormat(realValue, getOptionByValue(realValue, props.options));
}, [realValue]);

const [visible, { update: updateVisible }] = useMixState(
props.defaultVisible,
{
value: props.visible,
}
);
useEffect(() => {
setTimeout(() => {
if (visible) {
const newColumns = getterColumns(realValue, props.options);
const currentValue = getValidValue(realValue, newColumns);
setState({ value: currentValue, columns: newColumns });
}
})
}, [visible]);

useEvent('onVisibleChange', (event) => {
const visible = resolveEventValue(event)
if (visible) {
const newColumns = getterColumns(realValue, props.options);
const currentValue = getValidValue(realValue, newColumns);
setState({ value: currentValue, columns: newColumns });
updateVisible(true);
} else {
updateVisible(false);
}
triggerEvent('visibleChange', visible);
});
Expand All @@ -98,7 +114,9 @@ const CascaderPicker = (props: ICascaderProps) => {
const validValue = getValidValue(value, columns);

if (!isRealValueControlled) {
updateRealValue(validValue);
setTimeout(() => {
updateRealValue(validValue);
})
}

triggerEventValues('ok', [
Expand All @@ -119,6 +137,9 @@ const CascaderPicker = (props: ICascaderProps) => {
});

return {
state: {
visible
},
formattedValueText,
currentValue: value,
columns,
Expand Down
4 changes: 4 additions & 0 deletions compiled/alipay/src/Picker/CascaderPicker/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface ICascaderOption {
* @description 级联组件,基于Picker封装
*/
export interface ICascaderProps extends IBaseProps {
visible?: boolean;
defaultVisible?: boolean;
/**
* @desciption 动画类型
* @default "transform"
Expand Down Expand Up @@ -127,6 +129,8 @@ export const CascaderDefaultProps: Partial<ICascaderProps> = {
};

export const CascaderFunctionalProps: Partial<ICascaderProps> = {
visible: null,
defaultVisible: null,
animationType: 'transform',
value: null,
defaultValue: null,
Expand Down
2 changes: 2 additions & 0 deletions compiled/alipay/src/Picker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Picker 选择器显示一个或多个选项集合的可滚动列表,相比于
| suffix | 后缀 | slot | - |
| title | 弹出框标题 | string \| slot | - |
| value | 选中的值 | string[] | - |
| visible | 是否显示 | boolean | false |
| defaultVisible | 默认是否显示 | boolean | false |
| onOk | 点击确定按钮,触发回调 | (value: string[], selectedOptions: [CascaderOption](#cascaderoption)[], event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void | - |
| onCancel | 点击取消按钮/蒙层,触发回调 | (event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void |
| onChange | 选中项发生变化,触发回调 | (value: string[], selectedOptions: [CascaderOption](#cascaderoption)[], event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void | - |
Expand Down
12 changes: 12 additions & 0 deletions compiled/wechat/demo/pages/CascaderPicker/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cityList from './city';
Page({
data: {
pickerVisible: false,
cityList,
value: ['34', '330'],
},
Expand All @@ -23,4 +24,15 @@ Page({
handleDismiss(e) {
console.log('handleDismiss', e);
},
handleTriggerControlledPicker(visible, e) {
console.log('handleTriggerControlledPicker', visible);
this.setData({
pickerVisible: visible.detail,
});
},
handleOpenPicker() {
this.setData({
pickerVisible: true,
});
},
});
3 changes: 2 additions & 1 deletion compiled/wechat/demo/pages/CascaderPicker/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ant-cascader-picker": "../../../src/Picker/CascaderPicker/index",
"ant-list-item": "../../../src/List/ListItem/index",
"ant-list": "../../../src/List/index",
"ant-button": "../../../src/Button/index"
"ant-button": "../../../src/Button/index",
"ant-container": "../../../src/Container/index"
}
}
24 changes: 24 additions & 0 deletions compiled/wechat/demo/pages/CascaderPicker/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@
</ant-button>
</view>
</ant-list>

<ant-container style="margin-top: 20rpx">
<ant-list header="外部控制选择器是否显示">
<ant-list-item>
请选择省市
<ant-cascader-picker
slot="extra"
visible="{{ pickerVisible }}"
placeholder="请选择归属地"
options="{{ cityList }}"
bind:change="handleCityPickerChange"
bind:visiblechange="handleTriggerControlledPicker"
bind:ok="handleCityOnOk"
bind:cancel="handleDismiss" />
</ant-list-item>
</ant-list>

<ant-button
size="medium"
type="primary"
bind:tap="handleOpenPicker">
打开 CascaderPicker
</ant-button>
</ant-container>
12 changes: 12 additions & 0 deletions compiled/wechat/demo/pages/DatePicker/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Page({
data: {
pickerVisible: false,
dateRangePickerVisible: false,
min: new Date('2019/01/15').getTime(),
max: new Date('2023/08/20').getTime(),
defaultDate: new Date('2019/02/02').getTime(),
Expand Down Expand Up @@ -59,4 +60,15 @@ Page({
pickerVisible: true,
});
},
handleTriggerControlledDateRangePicker(visible, e) {
console.log('handleTriggerControlledDateRangePicker', visible);
this.setData({
dateRangePickerVisible: visible.detail,
});
},
handleOpenDateRangePicker() {
this.setData({
dateRangePickerVisible: true,
});
},
});
Loading
Loading