Skip to content

Commit

Permalink
fix(checkbox): async options state (Tencent#2706)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaishi authored and methodchen committed Aug 25, 2023
1 parent ed0a4e3 commit bf92226
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default defineComponent({
const checkboxGroupExist = ref(false);

const {
checked, indeterminate, disabled, value, lazyLoad,
checked, indeterminate, disabled, value, lazyLoad, label, data,
} = toRefs(props);
const [innerChecked, setInnerChecked] = useVModel(
checked,
Expand Down Expand Up @@ -154,6 +154,10 @@ export default defineComponent({

subscribeParentData(props.checkAll ? 'CHECK_ALL' : value.value);

watch([data, label], () => {
subscribeParentData(props.checkAll ? 'CHECK_ALL' : value.value);
});

onBeforeUnmount(() => {
checkboxStore.unSubscribe(props.checkAll ? 'CHECK_ALL' : value.value);
});
Expand Down
30 changes: 25 additions & 5 deletions src/checkbox/group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default defineComponent({
checkboxStore.init();

const { isArray } = Array;
const { value, disabled, name } = toRefs(props);
const {
value, disabled, name, options,
} = toRefs(props);
const [innerValue, setInnerValue] = useVModel(value, props.defaultValue, props.onChange);

const optionList = ref<Array<CheckboxOptionObj>>([]);
Expand Down Expand Up @@ -68,6 +70,16 @@ export default defineComponent({
checkboxStore.updateCheckbox({ disabled, maxExceeded, checkboxName });
});

watch([options], () => {
nextTick(() => {
checkboxStore.updateCheckbox({
disabled: disabled.value,
maxExceeded: maxExceeded.value,
checkboxName: name.value,
});
});
});

onMounted(() => {
checkboxStore.updateCheckbox({
disabled: disabled.value,
Expand Down Expand Up @@ -163,20 +175,28 @@ export default defineComponent({
);

watch(
[innerValue, isCheckAll, indeterminate],
([val, isCheckAll, indeterminate], [oldValue]) => {
[innerValue],
([val], [oldValue]) => {
nextTick(() => {
checkboxStore.updateChecked({
checked: val,
oldChecked: oldValue,
isCheckAll,
indeterminate,
isCheckAll: isCheckAll.value,
indeterminate: indeterminate.value,
});
});
},
{ immediate: true },
);

watch([isCheckAll, indeterminate, options], ([isCheckAll, indeterminate]) => {
checkboxStore.updateChecked({
checked: innerValue.value,
isCheckAll,
indeterminate,
});
});

const addStoreKeyToCheckbox = (nodes: VNode[]) => {
if (!nodes) return;
for (let i = 0, len = nodes.length; i < len; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/checkbox/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ObserverMap = {

export interface UpdateCheckedData {
checked: CheckboxStoreData['checked'];
oldChecked: CheckboxStoreData['checked'];
oldChecked?: CheckboxStoreData['checked'];
isCheckAll: boolean;
indeterminate: boolean;
}
Expand Down Expand Up @@ -45,7 +45,7 @@ class CheckboxStore {
updateChecked({
checked, isCheckAll, oldChecked, indeterminate,
}: UpdateCheckedData) {
const changedChecked = getChangedChecked(checked, oldChecked);
const changedChecked = oldChecked ? getChangedChecked(checked, oldChecked) : checked;
const checkedParams: ObserverListenerParams = {
parentChecked: checked,
parentIsCheckAll: isCheckAll,
Expand Down

0 comments on commit bf92226

Please sign in to comment.