Skip to content

Commit

Permalink
feat: check list 适配 functional
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondYuan committed Jan 18, 2024
1 parent 07034fd commit 1b5dd14
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 127 deletions.
2 changes: 1 addition & 1 deletion compiled/alipay/demo/pages/FormCustom/checklist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ Component({
onChange(value, e) {
this.emit('onChange', value);
},
}
},
});
21 changes: 12 additions & 9 deletions compiled/alipay/src/Checklist/ChecklistItem/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ChecklistItemDefaultProps } from './props';
import { useEvent } from 'functional-mini/component';
import { mountComponent } from '../../_util/component';
import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
import { ChecklistItemDefaultProps, IChecklistItemProps } from './props';

Component({
props: ChecklistItemDefaultProps,
methods: {
onChecklistItemClick() {
this.props.onChange(this.props.item);
},
},
});
const CheckListItem = (props: IChecklistItemProps) => {
const { triggerEvent } = useComponentEvent(props);
useEvent('onChecklistItemClick', () => {
triggerEvent('change', props.item);
});
};

mountComponent(CheckListItem, ChecklistItemDefaultProps);
5 changes: 4 additions & 1 deletion compiled/alipay/src/Checklist/ChecklistItem/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export interface IChecklistItemProps extends IBaseProps {
onChange: (item: ChecklistItem) => void;
}

export const ChecklistItemDefaultProps: Partial<IChecklistItemProps> = {};
export const ChecklistItemDefaultProps: Partial<IChecklistItemProps> = {
item: null,
checked: false,
};
107 changes: 57 additions & 50 deletions compiled/alipay/src/Checklist/index.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
import { ChecklistDefaultProps } from './props';
import mixinValue from '../mixins/value';
import fmtEvent from '../_util/fmtEvent';
import '../_util/assert-component2';
import { mountComponent } from '../_util/component';
import { useComponentEvent } from '../_util/hooks/useComponentEvent';
import { useHandleCustomEvent } from '../_util/hooks/useHandleCustomEvent';
import { useMixState } from '../_util/hooks/useMixState';
import {
ChecklistFunctionalProps,
ChecklistItem,
IChecklistProps,
} from './props';

Component({
props: ChecklistDefaultProps,
mixins: [
mixinValue({
transformValue(val) {
const value = val || [];
return {
needUpdate: true,
value,
};
},
}),
],
methods: {
onChange(item) {
const { multiple, options, onChange } = this.props;
const value = item.value;
if (multiple) {
let currentValue = this.getValue();
if (currentValue.indexOf(value) > -1) {
currentValue = currentValue.filter((v) => v !== value);
} else {
currentValue = [...currentValue, value];
}
if (!this.isControlled()) {
this.update(currentValue);
}
if (onChange) {
onChange(
currentValue,
options.filter((v) => currentValue.indexOf(v.value) > -1) as any,
fmtEvent(this.props)
);
}
const Checkbox = (props: IChecklistProps) => {
const [state, { isControlled, update }] = useMixState<
Array<string | number> | string | number
>(props.defaultValue, {
value: props.value,
postState(val) {
const value = val || [];
return {
valid: true,
value,
};
},
});

const { triggerEventValues } = useComponentEvent(props);
useHandleCustomEvent<ChecklistItem>('onChange', (item) => {
const { multiple, options } = props;
const value = item.value;
if (multiple) {
let currentValue = state as Array<string | number>;
if (currentValue.indexOf(value) > -1) {
currentValue = currentValue.filter((v) => v !== value);
} else {
if (!this.isControlled()) {
this.update(value);
}
if (onChange) {
onChange(
value,
options.find((v) => v.value === value) as any,
fmtEvent(this.props)
);
}
currentValue = [...currentValue, value];
}
if (!isControlled) {
update(currentValue);
}
triggerEventValues('change', [
currentValue,
options.filter((v) => currentValue.indexOf(v.value) > -1),
]);
} else {
if (!isControlled) {
update(value);
}
triggerEventValues('change', [
value,
options.find((v) => v.value === value),
]);
}
});

return {
mixin: {
value: state,
},
},
});
};
};

mountComponent(Checkbox, ChecklistFunctionalProps);
7 changes: 7 additions & 0 deletions compiled/alipay/src/Checklist/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ export const ChecklistDefaultProps: Partial<IChecklistProps> = {
multiple: false,
options: [],
};

export const ChecklistFunctionalProps: Partial<IChecklistProps> = {
value: null,
defaultValue: null,
multiple: false,
options: [],
};
3 changes: 1 addition & 2 deletions demo/pages/FormCustom/checklist/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createForm } from '../../../../src/Form/form';


Component({
mixins: [createForm()],
data: {
Expand All @@ -20,5 +19,5 @@ Component({
onChange(value, e) {
this.emit('onChange', value);
},
}
},
});
21 changes: 12 additions & 9 deletions src/Checklist/ChecklistItem/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ChecklistItemDefaultProps } from './props';
import { useEvent } from 'functional-mini/component';
import { mountComponent } from '../../_util/component';
import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
import { ChecklistItemDefaultProps, IChecklistItemProps } from './props';

Component({
props: ChecklistItemDefaultProps,
methods: {
onChecklistItemClick() {
this.props.onChange(this.props.item);
},
},
});
const CheckListItem = (props: IChecklistItemProps) => {
const { triggerEvent } = useComponentEvent(props);
useEvent('onChecklistItemClick', () => {
triggerEvent('change', props.item);
});
};

mountComponent(CheckListItem, ChecklistItemDefaultProps);
5 changes: 4 additions & 1 deletion src/Checklist/ChecklistItem/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export interface IChecklistItemProps extends IBaseProps {
onChange: (item: ChecklistItem) => void;
}

export const ChecklistItemDefaultProps: Partial<IChecklistItemProps> = {};
export const ChecklistItemDefaultProps: Partial<IChecklistItemProps> = {
item: null,
checked: false,
};
107 changes: 57 additions & 50 deletions src/Checklist/index.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
import { ChecklistDefaultProps } from './props';
import mixinValue from '../mixins/value';
import fmtEvent from '../_util/fmtEvent';
import '../_util/assert-component2';
import { mountComponent } from '../_util/component';
import { useComponentEvent } from '../_util/hooks/useComponentEvent';
import { useHandleCustomEvent } from '../_util/hooks/useHandleCustomEvent';
import { useMixState } from '../_util/hooks/useMixState';
import {
ChecklistFunctionalProps,
ChecklistItem,
IChecklistProps,
} from './props';

Component({
props: ChecklistDefaultProps,
mixins: [
mixinValue({
transformValue(val) {
const value = val || [];
return {
needUpdate: true,
value,
};
},
}),
],
methods: {
onChange(item) {
const { multiple, options, onChange } = this.props;
const value = item.value;
if (multiple) {
let currentValue = this.getValue();
if (currentValue.indexOf(value) > -1) {
currentValue = currentValue.filter((v) => v !== value);
} else {
currentValue = [...currentValue, value];
}
if (!this.isControlled()) {
this.update(currentValue);
}
if (onChange) {
onChange(
currentValue,
options.filter((v) => currentValue.indexOf(v.value) > -1) as any,
fmtEvent(this.props)
);
}
const Checkbox = (props: IChecklistProps) => {
const [state, { isControlled, update }] = useMixState<
Array<string | number> | string | number
>(props.defaultValue, {
value: props.value,
postState(val) {
const value = val || [];
return {
valid: true,
value,
};
},
});

const { triggerEventValues } = useComponentEvent(props);
useHandleCustomEvent<ChecklistItem>('onChange', (item) => {
const { multiple, options } = props;
const value = item.value;
if (multiple) {
let currentValue = state as Array<string | number>;
if (currentValue.indexOf(value) > -1) {
currentValue = currentValue.filter((v) => v !== value);
} else {
if (!this.isControlled()) {
this.update(value);
}
if (onChange) {
onChange(
value,
options.find((v) => v.value === value) as any,
fmtEvent(this.props)
);
}
currentValue = [...currentValue, value];
}
if (!isControlled) {
update(currentValue);
}
triggerEventValues('change', [
currentValue,
options.filter((v) => currentValue.indexOf(v.value) > -1),
]);
} else {
if (!isControlled) {
update(value);
}
triggerEventValues('change', [
value,
options.find((v) => v.value === value),
]);
}
});

return {
mixin: {
value: state,
},
},
});
};
};

mountComponent(Checkbox, ChecklistFunctionalProps);
7 changes: 7 additions & 0 deletions src/Checklist/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ export const ChecklistDefaultProps: Partial<IChecklistProps> = {
multiple: false,
options: [],
};

export const ChecklistFunctionalProps: Partial<IChecklistProps> = {
value: null,
defaultValue: null,
multiple: false,
options: [],
};
12 changes: 8 additions & 4 deletions tests/alipay/Checklist/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInstance } from 'tests/utils';
import { getInstance, sleep } from 'tests/utils';
import fmtEvent from 'compiled-alipay/_util/fmtEvent';
import { describe, it, expect, vi } from 'vitest';

Expand All @@ -11,7 +11,7 @@ describe('checklist onChange', () => {
instance.callMethod('onChecklistItemClick');
expect(onChange).toBeCalled();
});
it('check with multiple false', () => {
it('check with multiple false', async () => {
const my = {
canIUse() {
return true;
Expand All @@ -29,9 +29,10 @@ describe('checklist onChange', () => {
);
instance.callMethod('onChange', { value: 1 });
expect(onChange).toBeCalledWith(1, { value: 1 }, fmtEvent({}));
await sleep(20);
expect(instance.getData().mixin.value).toBe(1);
});
it('check with multiple true', () => {
it('check with multiple true', async () => {
const my = {
canIUse() {
return true;
Expand All @@ -49,10 +50,11 @@ describe('checklist onChange', () => {
my
);
instance.callMethod('onChange', { value: 1 });
await sleep(20);
expect(onChange).toBeCalledWith([1], [{ value: 1 }], fmtEvent({}));
expect(instance.getData().mixin.value).toEqual([1]);
});
it('uncheck with multiple true', () => {
it('uncheck with multiple true', async () => {
const my = {
canIUse() {
return true;
Expand All @@ -70,7 +72,9 @@ describe('checklist onChange', () => {
my
);
instance.callMethod('onChange', { value: 1 });
await sleep(20);
instance.callMethod('onChange', { value: 1 });
await sleep(20);
expect(onChange).toBeCalledWith([], [], fmtEvent({}));
expect(instance.getData().mixin.value).toEqual([]);
});
Expand Down

0 comments on commit 1b5dd14

Please sign in to comment.