Skip to content

Commit

Permalink
fix: fix switch beforeChanged 增加value入参
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbing4 committed May 22, 2024
1 parent 0b6836b commit 9e3169e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions components/switch/demos/beforeChange.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {Switch} from 'kpc';
```

```ts
import { Message } from 'kpc';
export default class extends Component {
static template = template;
static defaults() {
Expand All @@ -31,14 +32,16 @@ export default class extends Component {
}
}

beforeChange() {
beforeChange(value: any) {
return new Promise<boolean>(resolve => {
Message.warning(`current value is ${value}`);
setTimeout(() => {
resolve(true);
}, 1000);
});
}
beforeChangeFalse() {
beforeChangeFalse(value: any) {
Message.warning(`current value is ${value}`);
return new Promise<boolean>(resolve => {
setTimeout(() => {
resolve(false);
Expand Down
2 changes: 1 addition & 1 deletion components/switch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sidebar: doc
| size | 尺寸 | `"large"` &#124; `"default"` &#124; `"small"` &#124; `"mini"` | `"default"` |
| disabled | 控制禁用 | `boolean` | `false` |
| loading | 开关loading属性 | `boolean` | `false` |
| beforeChange | 切换`Switch`之前的拦截函数,如果该函数返回`false`则阻止本次切换,支持异步函数 | <code>() => boolean &#124; Promise&lt;boolean&gt;</code> | `undefined` |
| beforeChange | 切换`Switch`之前的拦截函数,如果该函数返回`false`则阻止本次切换,支持异步函数, 参数为当前value | <code>(value: any) => boolean &#124; Promise&lt;boolean&gt;</code> | `undefined` |

# 扩展点

Expand Down
6 changes: 3 additions & 3 deletions components/switch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface SwitchProps<True = any, False = any> {
size?: Sizes
disabled?: boolean
loading?: boolean
beforeChange?: () => boolean | Promise<boolean>;
beforeChange?: (value: True | False | undefined) => boolean | Promise<boolean>;
}

export interface SwitchEvents {
Expand Down Expand Up @@ -96,7 +96,7 @@ export class Switch<True = true, False = false> extends Component<SwitchProps<Tr
}

public async toggle(isKeypress: boolean) {
const {disabled, beforeChange} = this.get();
const {disabled, beforeChange, value} = this.get();
if (disabled) return;

// if is not keypress, we blur it to remove focus style
Expand All @@ -110,7 +110,7 @@ export class Switch<True = true, False = false> extends Component<SwitchProps<Tr
loading: true
});
try {
const ret = await beforeChange();
const ret = await beforeChange(value);
if (!ret) return;
} finally {
this.set({
Expand Down

0 comments on commit 9e3169e

Please sign in to comment.