diff --git a/.vscode/settings.json b/.vscode/settings.json index 335f886a2b574441c5e03bd84bac6452007c3978..94a7feb0e24016b4048483a7a3aea1fad4c357f3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,12 @@ { - "editor.defaultFormatter": "esbenp.prettier-vscode" + "editor.defaultFormatter": "esbenp.prettier-vscode", + "[typescript]": { + "editor.defaultFormatter": "vscode.typescript-language-features" + }, + "[typescriptreact]": { + "editor.defaultFormatter": "vscode.typescript-language-features" + }, + "[vue]": { + "editor.defaultFormatter": "Wscats.vue" + } } \ No newline at end of file diff --git a/packages/ui-vue/components/radio-group/index.ts b/packages/ui-vue/components/radio-group/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..55b4492aca05fa916904c21c2602d98761cc5d8f --- /dev/null +++ b/packages/ui-vue/components/radio-group/index.ts @@ -0,0 +1,12 @@ +import type { App } from 'vue'; +import RadioGroup from './src/radio-group.component'; + +export * from './src/radio-group.props'; + +export { RadioGroup }; + +export default { + install(app: App): void { + app.component(RadioGroup.name, RadioGroup); + } +}; diff --git a/packages/ui-vue/components/radio-group/src/composition/change-radio.ts b/packages/ui-vue/components/radio-group/src/composition/change-radio.ts new file mode 100644 index 0000000000000000000000000000000000000000..a3e75751b2f41a604903d2256ff62814140d94dd --- /dev/null +++ b/packages/ui-vue/components/radio-group/src/composition/change-radio.ts @@ -0,0 +1,38 @@ +import { ChangeRadio, Radio } from './types'; +import { computed, Ref, SetupContext } from 'vue'; +import { RadioGroupProps } from '../radio-group.props'; + +export function changeRadio(props: RadioGroupProps, context: SetupContext, modelValue: Ref): ChangeRadio { + + const canChangeRadioButton = computed(() => !props.disabled); + const enumData = computed(() => props.enumData || []); + + function getValue(item: Radio): any { + return item[props.valueField]; + }; + + function getText(item: Radio): any { + return item[props.textField]; + }; + + function onClickRadio(item, $event: Event) { + if (canChangeRadioButton.value) { + const newValue = getValue(item); + if (modelValue.value !== item) { + modelValue.value = newValue; + + context.emit('changeValue', newValue); + + context.emit('update:modelValue', newValue); + } + } + $event.stopPropagation(); + } + + return { + enumData, + getValue, + getText, + onClickRadio + }; +} diff --git a/packages/ui-vue/components/radio-group/src/composition/types.ts b/packages/ui-vue/components/radio-group/src/composition/types.ts new file mode 100644 index 0000000000000000000000000000000000000000..1630855ac5f78b06f9fa80610c70354fb85275ce --- /dev/null +++ b/packages/ui-vue/components/radio-group/src/composition/types.ts @@ -0,0 +1,32 @@ +import { ComputedRef, Ref } from 'vue'; + +export interface Radio { + /** + * 枚举值 + */ + value: ComputedRef; + /** + * 枚举展示文本 + */ + name: ComputedRef; +} + +export interface ChangeRadio { + + enumData: ComputedRef>; + + /** + * 获取枚举值 + */ + getValue(item: Radio): any; + /** + * 获取枚举文本 + */ + getText(item: Radio): any; + + /** + * 切换单选按钮事件 + */ + onClickRadio: (item: Radio, $event: Event) => void; + +} diff --git a/packages/ui-vue/components/radio-group/src/radio-group.component.tsx b/packages/ui-vue/components/radio-group/src/radio-group.component.tsx new file mode 100644 index 0000000000000000000000000000000000000000..b3d9d3a3b4ab52e1cb4a81bf1766679e32db725c --- /dev/null +++ b/packages/ui-vue/components/radio-group/src/radio-group.component.tsx @@ -0,0 +1,56 @@ +import { defineComponent, computed, ref } from 'vue'; +import type { SetupContext } from 'vue'; +import { radioGroupProps, RadioGroupProps } from './radio-group.props'; +import { changeRadio } from './composition/change-radio'; + +export default defineComponent({ + name: 'FRadioGroup', + props: radioGroupProps, + emits: [ + 'changeValue', + 'update:modelValue', + ], + setup(props: RadioGroupProps, context: SetupContext) { + const modelValue = ref(props.modelValue); + const { enumData, onClickRadio, getValue, getText } = changeRadio(props, context, modelValue); + + + const horizontalClass = computed(() => ({ + 'farris-checkradio-hor': props.horizontal + })); + + + + return () => { + return ( +
+ { + enumData.value.map((item, index) => { + const id = 'radio_' + props.name + index; + + return ( +
+ onClickRadio(item, event)} + /> + +
+ ); + }) + } + +
+ ); + }; + }, +}); diff --git a/packages/ui-vue/components/radio-group/src/radio-group.props.ts b/packages/ui-vue/components/radio-group/src/radio-group.props.ts new file mode 100644 index 0000000000000000000000000000000000000000..b81a9203860cc537c3e42be74fb766a2c36ac4be --- /dev/null +++ b/packages/ui-vue/components/radio-group/src/radio-group.props.ts @@ -0,0 +1,45 @@ +import { ExtractPropTypes, PropType } from 'vue'; +import { Radio } from './composition/types'; + +export const radioGroupProps = { + /** + * 组件标识 + */ + id: String, + /** + * 组件名称 + */ + name: { type: String, default: '' }, + /** + * 单选组枚举数组 + */ + enumData: Array, + /** + * 枚举数组中展示文本的key值。 + */ + textField: { type: String, default: 'name' }, + /** + * 枚举数组中枚举值的key值。 + */ + valueField: { type: String, default: 'value' }, + /** + * 组件是否水平排列 + */ + horizontal: { type: Boolean, default: false }, + /** + * 禁用组件,不允许切换单选值 + */ + disabled: { type: Boolean, default: false }, + + /** + * 组件值 + */ + modelValue: { type: String, default: '' }, + + /** + * 输入框Tab键索引 + */ + tabIndex: Number, +}; + +export type RadioGroupProps = ExtractPropTypes; diff --git a/packages/ui-vue/src/App.vue b/packages/ui-vue/src/App.vue index 99e7703d6be6855c5b70e22039c25b01d6c3f446..39b699c47f9b598546bd36f6ec1f3cb81e8860c1 100644 --- a/packages/ui-vue/src/App.vue +++ b/packages/ui-vue/src/App.vue @@ -7,6 +7,7 @@ import Avatar from './components/avatar.vue'; import ButtonEdit from './components/button-edit.vue'; import FButton from '../components/button/src/button.component'; import Switch from './components/switch.vue'; +import RadioGroup from './components/radio-group.vue'; const canEdit = ref(true); const disable = ref(false); @@ -34,6 +35,7 @@ const canAutoComplete = ref(false); +