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

Radio & Rate & Selector & Switch & TabBar & Checkbox & Checklist & Collapse & ImageUpload 组件去掉hook相关的实现 #1183

Merged
merged 19 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
79 changes: 38 additions & 41 deletions compiled/alipay/src/Checkbox/CheckboxGroup/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
import { useEvent } from 'functional-mini/component';
import { mountComponent } from '../../_util/component';
import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
import { useMixState } from '../../_util/hooks/useMixState';
import { CheckboxGroupFunctionalProps, ICheckboxGroupProps } from './props';
import { Component, triggerEvent, getValueFromProps } from '../../_util/simply';
import { CheckboxGroupDefaultProps } from './props';
import mixinValue from '../../mixins/value';

const CheckboxGroup = (props: ICheckboxGroupProps) => {
const [value, { isControlled, update }] = useMixState(props.defaultValue, {
value: props.value,
postState(value) {
return {
valid: true,
value: value || [],
};
Component(
CheckboxGroupDefaultProps,
{
onChange(args, e) {
if (getValueFromProps(this, 'disabled')) {
return;
}
let event;
event = e;
let currentValue = this.getValue();
const { index } = event.currentTarget.dataset;
const selectValue = getValueFromProps(this, 'options')[index].value;
if (currentValue.indexOf(selectValue) > -1) {
currentValue = currentValue.filter((v) => v !== selectValue);
} else {
currentValue = [...currentValue, selectValue];
}
if (!this.isControlled()) {
this.update(currentValue);
}
triggerEvent(this, 'change', currentValue, e);
},
});
const { triggerEvent } = useComponentEvent(props);
useEvent('onChange', (args, e) => {
if (props.disabled) {
return;
}
let event;
event = e;

let currentValue = value;
const { index } = event.currentTarget.dataset;
const selectValue = props.options[index].value;
if (currentValue.indexOf(selectValue) > -1) {
currentValue = currentValue.filter((v) => v !== selectValue);
} else {
currentValue = [...currentValue, selectValue];
}
if (!isControlled) {
update(currentValue);
}
triggerEvent('change', currentValue, e);
});
return {
mixin: { value },
};
};

mountComponent(CheckboxGroup, CheckboxGroupFunctionalProps);
},
null,
[
mixinValue({
transformValue(val) {
const value = val || [];
return {
needUpdate: true,
value,
};
},
}),
]
);
6 changes: 0 additions & 6 deletions compiled/alipay/src/Checkbox/CheckboxGroup/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ export interface ICheckboxGroupProps extends IBaseProps {
}

export const CheckboxGroupDefaultProps: Partial<ICheckboxGroupProps> = {
options: [],
position: 'vertical',
defaultValue: [],
};

export const CheckboxGroupFunctionalProps: Partial<ICheckboxGroupProps> = {
value: null,
defaultValue: [],
disabled: false,
Expand Down
53 changes: 21 additions & 32 deletions compiled/alipay/src/Checkbox/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
import { useEvent } from 'functional-mini/component';
import '../_util/assert-component2';
import { mountComponent } from '../_util/component';
import { useComponentEvent } from '../_util/hooks/useComponentEvent';
import { useMixState } from '../_util/hooks/useMixState';
import { ICheckboxProps } from './props';
import { CheckboxDefaultProps } from './props';
import { Component, triggerEvent } from '../_util/simply';
import mixinValue from '../mixins/value';

const Checkbox = (props: ICheckboxProps) => {
const [value, { isControlled, update }] = useMixState(props.defaultChecked, {
value: props.checked,
});
const { triggerEvent } = useComponentEvent(props);
useEvent('onChange', (e) => {
const newValue = !value;
if (!isControlled) {
update(newValue);
}
triggerEvent('change', newValue, e);
});

return {
mixin: {
value,
Component(
CheckboxDefaultProps,
{
onChange(e) {
const value = !this.getValue();
if (!this.isControlled()) {
this.update(value);
}
triggerEvent(this, 'change', value, e);
},
};
};

mountComponent(Checkbox, {
value: null,
checked: null,
defaultChecked: null,
disabled: false,
color: '',
});
},
null,
[
mixinValue({
valueKey: 'checked',
defaultValueKey: 'defaultChecked',
}),
]
);
8 changes: 7 additions & 1 deletion compiled/alipay/src/Checkbox/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ export interface ICheckboxProps extends IBaseProps {
onChange?: (checked: boolean, e: any) => void;
}

export const CheckboxDefaultProps: Partial<ICheckboxProps> = {};
export const CheckboxDefaultProps: Partial<ICheckboxProps> = {
value: null,
checked: null,
defaultChecked: null,
disabled: false,
color: '',
};
9 changes: 4 additions & 5 deletions compiled/alipay/src/Checklist/ChecklistItem/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Component, triggerEvent } from '../../_util/simply';
import { Component, triggerEvent, getValueFromProps } from '../../_util/simply';
import { ChecklistItemDefaultProps } from './props';

Component(ChecklistItemDefaultProps, {
onChecklistItemClick() {
triggerEvent(this, 'change', this.props.item);

}
})
triggerEvent(this, 'change', getValueFromProps(this, 'item'));
},
});
107 changes: 51 additions & 56 deletions compiled/alipay/src/Checklist/index.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,56 @@
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,
triggerEventValues,
getValueFromProps,
} from '../_util/simply';
import { ChecklistDefaultProps } from './props';
import mixinValue from '../mixins/value';

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 {
currentValue = [...currentValue, value];
}
if (!isControlled) {
update(currentValue);
}
triggerEventValues('change', [
currentValue,
options.filter((v) => currentValue.indexOf(v.value) > -1),
Component(
ChecklistDefaultProps,
{
onChange(item) {
const [multiple, options] = getValueFromProps(this, [
'multiple',
'options',
]);
} else {
if (!isControlled) {
update(value);
let value;
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);
}
triggerEventValues(this, 'change', [
currentValue,
options.filter((v) => currentValue.indexOf(v.value) > -1),
]);
} else {
if (!this.isControlled()) {
this.update(value);
}
triggerEventValues(this, 'change', [
value,
options.find((v) => v.value === value),
]);
}
triggerEventValues('change', [
value,
options.find((v) => v.value === value),
]);
}
});

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

mountComponent(Checkbox, ChecklistFunctionalProps);
},
null,
[
mixinValue({
transformValue(val) {
const value = val || [];
return {
needUpdate: true,
value,
};
},
}),
]
);
7 changes: 1 addition & 6 deletions compiled/alipay/src/Checklist/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,8 @@ export interface IChecklistProps extends IBaseProps {
}

export const ChecklistDefaultProps: Partial<IChecklistProps> = {
multiple: false,
options: [],
};

export const ChecklistFunctionalProps: Partial<IChecklistProps> = {
value: null,
defaultValue: null,
multiple: false,
options: [],
multiple: false,
};
Loading
Loading