@fesjs/fes-design
Version:
fes-design for PC
50 lines (47 loc) • 1.27 kB
JavaScript
import { provide, unref } from 'vue';
import { useNormalModel } from '../_util/use/useModel';
import { CHANGE_EVENT } from '../_util/constants';
import useFormAdaptor from '../_util/use/useFormAdaptor';
import { radioGroupKey, COMPONENT_NAME } from './const';
const useRadioGroup = (props, emit) => {
const {
validate,
isFormDisabled
} = useFormAdaptor({
forbidChildValidate: true
});
const [currentValue, updateCurrentValue] = useNormalModel(props, emit);
const handleChange = () => {
emit(CHANGE_EVENT, currentValue.value);
validate(CHANGE_EVENT);
};
const isSelect = value => {
const radioGroupVal = unref(currentValue);
const radioVal = unref(value);
return radioGroupVal === radioVal;
};
const onSelect = (value, afterSelectHandler) => {
const radioGroupVal = unref(currentValue);
const radioVal = unref(value);
if (radioGroupVal === radioVal) {
if (!props.cancelable) {
return;
}
updateCurrentValue(null);
} else {
updateCurrentValue(radioVal);
}
handleChange();
afterSelectHandler();
};
provide(radioGroupKey, {
name: COMPONENT_NAME,
isSelect,
onSelect,
props
});
return {
isFormDisabled
};
};
export { useRadioGroup };