refactor: reorganize all components (#8303)

This commit is contained in:
neverland
2021-03-08 11:50:37 +08:00
committed by GitHub
parent 3144a63d2b
commit e540876398
193 changed files with 1307 additions and 400 deletions

99
src/checkbox/Checkbox.tsx Normal file
View File

@@ -0,0 +1,99 @@
import { computed, watch, defineComponent } from 'vue';
// Utils
import { createNamespace, pick } from '../utils';
import {
CHECKBOX_GROUP_KEY,
CheckboxGroupProvide,
} from '../checkbox-group/CheckboxGroup';
// Composables
import { useParent } from '@vant/use';
import { useExpose } from '../composables/use-expose';
import { useLinkField } from '../composables/use-link-field';
// Components
import Checker, { checkerProps } from './Checker';
const [name, bem] = createNamespace('checkbox');
export default defineComponent({
name,
props: {
...checkerProps,
bindGroup: {
type: Boolean,
default: true,
},
},
emits: ['change', 'update:modelValue'],
setup(props, { emit, slots }) {
const { parent } = useParent<CheckboxGroupProvide>(CHECKBOX_GROUP_KEY);
const setParentValue = (checked: boolean) => {
const { name } = props;
const { max, modelValue } = parent!.props;
const value = modelValue.slice();
if (checked) {
const overlimit = max && value.length >= max;
if (!overlimit && !value.includes(name)) {
value.push(name);
if (props.bindGroup) {
parent!.updateValue(value);
}
}
} else {
const index = value.indexOf(name);
if (index !== -1) {
value.splice(index, 1);
if (props.bindGroup) {
parent!.updateValue(value);
}
}
}
};
const checked = computed(() => {
if (parent && props.bindGroup) {
return parent.props.modelValue.indexOf(props.name) !== -1;
}
return !!props.modelValue;
});
const toggle = (newValue = !checked.value) => {
if (parent && props.bindGroup) {
setParentValue(newValue);
} else {
emit('update:modelValue', newValue);
}
};
watch(
() => props.modelValue,
(value) => emit('change', value)
);
useExpose({ toggle, props, checked });
useLinkField(() => props.modelValue);
return () => (
<Checker
v-slots={pick(slots, ['default', 'icon'])}
bem={bem}
role="checkbox"
parent={parent}
checked={checked.value}
onToggle={toggle}
{...props}
/>
);
},
});