From 67f277d5a21ac6b4239084db03b42e11bfe7a450 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Mon, 24 Jan 2022 15:37:55 +0800 Subject: [PATCH 1/2] refactor(steps): turn into controlled component --- example/pages/steps/steps.ts | 12 +++++++++++- example/pages/steps/steps.wxml | 34 +++++++++++++++++----------------- src/steps/README.md | 32 +++++++++++++++++++++++++++----- src/steps/props.ts | 5 +++++ src/steps/steps.ts | 23 ++++++++++++++--------- src/steps/type.ts | 12 ++++++++++-- 6 files changed, 84 insertions(+), 34 deletions(-) diff --git a/example/pages/steps/steps.ts b/example/pages/steps/steps.ts index 560d44d43..a3a3a619b 100644 --- a/example/pages/steps/steps.ts +++ b/example/pages/steps/steps.ts @@ -1 +1,11 @@ -Page({}); +Page({ + data: { + current: 0, + }, + handleChange({ detail }) { + console.log(detail.current); + this.setData({ + current: detail.current, + }); + }, +}); diff --git a/example/pages/steps/steps.wxml b/example/pages/steps/steps.wxml index bc563fa74..d7cfeeefd 100644 --- a/example/pages/steps/steps.wxml +++ b/example/pages/steps/steps.wxml @@ -4,23 +4,23 @@ - + - + - + - + @@ -28,13 +28,13 @@ - + - + @@ -45,12 +45,12 @@ 横向带图标可操作步骤条 - + - + @@ -60,32 +60,32 @@ 横向只读步骤条 - + - + - + - + - + - + @@ -93,19 +93,19 @@ - + - + - + diff --git a/src/steps/README.md b/src/steps/README.md index 578af8034..e68da7d10 100644 --- a/src/steps/README.md +++ b/src/steps/README.md @@ -25,27 +25,27 @@ isComponent: true ```html - - + ``` +#### 横向只读步骤条 + ```html - ``` +#### 竖向只读步骤条 ```html - @@ -53,10 +53,11 @@ isComponent: true ``` +#### 自定义内容步骤条 + ```html - 可自定义此处内容 @@ -73,6 +74,27 @@ isComponent: true ``` +### 受控用法 + +```html + + + + +``` + +```js +Page({ + data: { + current: 0 + }, + onChange(e) { + const { current } = e.detail; + this.setData({ current }); + }, +}) +``` + ## API ### Steps Props diff --git a/src/steps/props.ts b/src/steps/props.ts index b25dcfc35..c7c43214d 100644 --- a/src/steps/props.ts +++ b/src/steps/props.ts @@ -12,6 +12,11 @@ const props: TdStepsProps = { type: String, optionalTypes: [Number], }, + /** 当前步骤 */ + defaultCurrent: { + type: null, + value: undefined, + }, /** 组件类名,用于设置组件外层元素元素类名 */ externalClasses: { type: Array, diff --git a/src/steps/steps.ts b/src/steps/steps.ts index 11737c2a9..1b3220b08 100644 --- a/src/steps/steps.ts +++ b/src/steps/steps.ts @@ -20,12 +20,25 @@ export default class Steps extends SuperComponent { properties = props; + controlledProps = [ + { + key: 'current', + event: 'change', + }, + ]; + // 组件的内部数据 data = { prefix, classPrefix: name, }; + observers = { + current() { + this.updateChildren(); + }, + }; + methods = { updateChildren() { const items = this.getRelationNodes('./step-item'); @@ -44,15 +57,7 @@ export default class Steps extends SuperComponent { } if (!this.data.readonly) { const preIndex = this.data.current; - this.setData( - { - current: index, - }, - () => { - this.updateChildren(); - }, - ); - this.triggerEvent('change', { + this._trigger('change', { previous: preIndex, current: index, }); diff --git a/src/steps/type.ts b/src/steps/type.ts index 89cbd6e7e..389047d9a 100644 --- a/src/steps/type.ts +++ b/src/steps/type.ts @@ -15,6 +15,14 @@ export interface TdStepsProps { value?: string | number; required?: boolean; }; + /** + * 当前步骤 + */ + defaultCurrent?: { + type: StringConstructor; + value?: string | number; + required?: boolean; + }; /** * 组件类名,用于设置组件外层元素元素类名 */ @@ -50,7 +58,7 @@ export interface TdStepsProps { value?: 'default' | 'dot'; required?: boolean; }; -}; +} export interface TdStepItemProps { /** @@ -96,6 +104,6 @@ export interface TdStepItemProps { value?: string; required?: boolean; }; -}; +} export type StepStatus = 'default' | 'process' | 'finish' | 'error'; From a3d165c4a366818bd1a5d3cb0a55cec2df9ce2e2 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Mon, 24 Jan 2022 17:28:56 +0800 Subject: [PATCH 2/2] docs(steps): update document --- src/steps/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/steps/README.md b/src/steps/README.md index e68da7d10..24e56e573 100644 --- a/src/steps/README.md +++ b/src/steps/README.md @@ -101,6 +101,7 @@ Page({ 名称 | 类型 | 默认值 | 说明 | 必传 -- | -- | -- | -- | -- current | String / Number | - | 当前步骤,即整个步骤条进度。默认根据步骤下标判断步骤的完成状态,当前步骤为进行中,当前步骤之前的步骤为已完成,当前步骤之后的步骤为未开始。如果每个步骤没有设置 value,current 值为步骤长度则表示所有步骤已完成。如果每个步骤设置了自定义 value,则 current = 'FINISH' 表示所有状态完成 | N +defaultCurrent | String / Number | - | (非受控)当前步骤,即整个步骤条进度 | N external-classes | Array | - | 组件类名,用于设置组件外层元素元素类名。`['t-class']` | N layout | String | horizontal | 步骤条方向,有两种:横向和纵向。可选项:horizontal/vertical | N readonly | Boolean | false | 是否只读 | N