Skip to content

Commit 631d85e

Browse files
sunshineLixunsendya
authored andcommitted
feat: add ProFormRadio
1 parent a19279f commit 631d85e

File tree

7 files changed

+190
-19
lines changed

7 files changed

+190
-19
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { defineComponent, type App, DefineComponent, Plugin } from 'vue';
2+
import { fieldRadioProps, FieldRadioProps } from './types';
3+
import { Radio } from 'ant-design-vue';
4+
import { getSlot } from '@ant-design-vue/pro-utils';
5+
6+
export const slots = ['renderExtraFooter', 'suffixIcon', 'clearIcon'];
7+
8+
const FieldRadio = defineComponent({
9+
name: 'FieldRadio',
10+
inheritAttrs: false,
11+
props: fieldRadioProps,
12+
slots,
13+
setup(props, { slots }) {
14+
15+
const render = getSlot(slots, props.fieldProps as Record<string, any>, 'render') as Function;
16+
const renderFormItem = getSlot(slots, props.fieldProps as Record<string, any>, 'renderFormItem') as Function;
17+
const children = getSlot(slots, props.fieldProps as Record<string, any>, 'default');
18+
19+
return () => {
20+
const { mode, text, fieldProps = {} } = props;
21+
const { options } = fieldProps
22+
23+
if (mode === 'read') {
24+
const optionsValue = fieldProps.value;
25+
const dom = <>{optionsValue}</>;
26+
27+
if (render) {
28+
return render(text, { mode, ...fieldProps }, dom) || null;
29+
}
30+
return dom;
31+
}
32+
33+
if (mode === 'edit') {
34+
const dom = (
35+
<Radio.Group
36+
{...fieldProps}
37+
options={options}
38+
v-slots={{
39+
default: children
40+
}}
41+
/>
42+
);
43+
if (renderFormItem) {
44+
return renderFormItem(text, { mode, ...fieldProps }, dom);
45+
}
46+
return dom;
47+
}
48+
return null;
49+
};
50+
},
51+
});
52+
53+
FieldRadio.install = (app: App) => {
54+
app.component(FieldRadio.name, FieldRadio);
55+
return app;
56+
};
57+
58+
export default FieldRadio as DefineComponent<FieldRadioProps> & Plugin;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { ExtractPropTypes, PropType } from "vue";
2+
import type { RadioGroupProps } from "ant-design-vue/es/radio/Group";
3+
import { proFieldFC } from "../typings";
4+
5+
export const fieldRadioProps = {
6+
...proFieldFC,
7+
fieldProps: {
8+
type: Object as PropType<RadioGroupProps>,
9+
},
10+
};
11+
12+
export type FieldRadioProps = Partial<ExtractPropTypes<typeof fieldRadioProps>>;

packages/pro-field/src/components/TimePicker/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const FieldTimePicker = defineComponent({
2121
'renderExtraFooter'
2222
);
2323

24-
const render = getSlot(slots, props.fieldProps as Record<string, any>, 'render') as any;
25-
const renderFormItem = getSlot(slots, props.fieldProps as Record<string, any>, 'renderFormItem') as any;
24+
const render = getSlot(slots, props.fieldProps as Record<string, any>, 'render') as Function;
25+
const renderFormItem = getSlot(slots, props.fieldProps as Record<string, any>, 'renderFormItem') as Function;
2626

2727
return () => {
2828
const { mode, text, fieldProps } = props;

packages/pro-field/src/index.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ import { fieldTimeRangePickerProps, type FieldTimeRangePickerProps } from './com
7575
import FieldTimeRangePicker, { slots as timeRangePickerSlots } from './components/TimeRangePicker';
7676
export { FieldTimeRangePicker, fieldTimeRangePickerProps, timeRangePickerSlots, FieldTimeRangePickerProps };
7777

78+
import { fieldRadioProps, type FieldRadioProps } from './components/Radio/types';
79+
import FieldRadio from './components/Radio';
80+
export { FieldRadio, fieldRadioProps, FieldRadioProps };
81+
82+
7883
// style
7984
import './default.less';
8085
import './style.less';
@@ -268,6 +273,19 @@ const defaultRenderText = (
268273
);
269274
}
270275

276+
if (valueType === 'radio' || valueType === 'radioButton') {
277+
const { fieldProps } = props
278+
return (
279+
<FieldRadio
280+
{...props}
281+
fieldProps={{
282+
...fieldProps,
283+
}}
284+
text={dataValue as string}
285+
/>
286+
)
287+
}
288+
271289
if (valueType === 'select') {
272290
let text = '';
273291
if (dataValue instanceof Array) {

packages/pro-form/examples/views/ProForm.vue

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,37 @@
252252
</div>
253253
</template>
254254
</ProFormDateTimeRangePicker>
255+
<ProFormRadio
256+
name="radio"
257+
label="Checkbox.Group"
258+
>
259+
<Radio value="a">Hangzhou</Radio>
260+
<Radio value="b">Shanghai</Radio>
261+
<Radio value="c">Beijing</Radio>
262+
<Radio value="d" disabled>Chengdu</Radio>
263+
</ProFormRadio>
264+
<ProFormRadio
265+
name="radio1"
266+
label="Checkbox.Group"
267+
:field-props="{
268+
options: optionsWithDisabled
269+
}"
270+
/>
271+
255272
</pro-form>
256273
</template>
257274

258275
<script lang="ts" setup>
259-
import { reactive, ref, FunctionalComponent, CSSProperties, onMounted } from 'vue';
276+
import { ref, FunctionalComponent, CSSProperties } from 'vue';
260277
import { PlusOutlined } from '@ant-design/icons-vue';
261278
import {
262279
RadioGroup,
263280
RadioButton,
281+
Radio,
264282
Switch,
265283
Divider,
266284
SelectOption,
267-
type SelectProps
285+
type SelectProps, RadioGroupProps
268286
} from 'ant-design-vue';
269287
import type { FormLayout } from 'ant-design-vue/es/form/Form';
270288
import {
@@ -279,7 +297,8 @@ import {
279297
ProFormDatePickerQuarter,
280298
ProFormDateTimeRangePicker,
281299
ProFormTimePicker,
282-
ProFormTimeRangePicker
300+
ProFormTimeRangePicker,
301+
ProFormRadio
283302
} from '@ant-design-vue/pro-form';
284303
import dayjs, { type Dayjs } from 'dayjs';
285304
import type { Recordable } from '@/typings';
@@ -332,7 +351,9 @@ const formModel = ref({
332351
yearTime: ref<Dayjs>(),
333352
timeDate: ref<Dayjs>(),
334353
timeRangeDate: ref<Dayjs>(),
335-
dateTimeRange: ref<RangeValue>()
354+
dateTimeRange: ref<RangeValue>(),
355+
radio: 'a',
356+
radio1: ''
336357
});
337358
338359
const sex = ref([
@@ -386,6 +407,12 @@ const langs = ref([
386407
}
387408
]);
388409
410+
const optionsWithDisabled: RadioGroupProps['options'] = [
411+
{ label: 'Apple', value: 'Apple' },
412+
{ label: 'Pear', value: 'Pear' },
413+
{ label: 'Orange', value: 'Orange', disabled: true },
414+
];
415+
389416
const formLayoutType = ref<FormLayout>('horizontal');
390417
const grid = ref(true);
391418
const readonly = ref(false);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { defineComponent, type PropType, ExtractPropTypes, App, DefineComponent, Plugin } from 'vue';
2+
import type { RadioGroupProps } from 'ant-design-vue/es/radio/Group';
3+
import { getSlot } from '@ant-design-vue/pro-utils';
4+
import { pick } from 'lodash-es';
5+
import ProFormField, { proFormFieldProps } from '../Field';
6+
import { proFormItemProps } from '../FormItem';
7+
import type { VueNode } from 'ant-design-vue/lib/_util/type';
8+
9+
const props = {
10+
...proFormFieldProps,
11+
fieldProps: {
12+
type: Object as PropType<RadioGroupProps>,
13+
},
14+
};
15+
export type ProFormRadioProps = Partial<ExtractPropTypes<typeof props>>;
16+
17+
export const ProFormRadio = defineComponent({
18+
name: 'ProFormRadio',
19+
inheritAttrs: false,
20+
props,
21+
setup(props, { slots }) {
22+
const formItemProps = {
23+
...props.formItemProps,
24+
...pick(props, Object.keys(proFormItemProps)),
25+
};
26+
27+
const children = getSlot(slots, props, 'default') as Function;
28+
29+
return () => {
30+
const { fieldProps, colProps } = props;
31+
return (
32+
<ProFormField
33+
valueType={'radio' || 'radioButton'}
34+
fieldProps={{
35+
...fieldProps,
36+
default: children
37+
}}
38+
filedConfig={{ valueType: 'radio' || 'radioButton'}}
39+
colProps={colProps}
40+
formItemProps={formItemProps}
41+
{...formItemProps}
42+
/>
43+
);
44+
};
45+
},
46+
});
47+
48+
ProFormRadio.install = (app: App) => {
49+
app.component(ProFormRadio.name, ProFormRadio);
50+
return app;
51+
};
52+
53+
export default ProFormRadio as DefineComponent<ProFormRadioProps> & Plugin;
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1-
import ProFormText, { type ProFieldPropsType } from './Text';
1+
import ProFormText, { type ProFieldPropsType } from "./Text";
22
export { ProFormText, ProFieldPropsType };
33

4-
import ProFormPassword, { type ProFieldPasswordPropsType } from './Password';
4+
import ProFormPassword, { type ProFieldPasswordPropsType } from "./Password";
55
export { ProFormPassword, ProFieldPasswordPropsType };
66

7-
import { ProFormSelect } from './Select';
7+
import { ProFormSelect } from "./Select";
88
export { ProFormSelect };
99

10-
import { default as ProFormDatePicker } from './DatePicker';
10+
import { default as ProFormDatePicker } from "./DatePicker";
1111
export { ProFormDatePicker };
1212

13-
import { ProFormDatePickerWeek } from './DatePicker/WeekPicker';
13+
import { ProFormDatePickerWeek } from "./DatePicker/WeekPicker";
1414
export { ProFormDatePickerWeek };
1515

16-
import { ProFormDatePickerMonth } from './DatePicker/MonthPicker';
16+
import { ProFormDatePickerMonth } from "./DatePicker/MonthPicker";
1717
export { ProFormDatePickerMonth };
1818

19-
import { ProFormDatePickerQuarter } from './DatePicker/QuarterPicker';
19+
import { ProFormDatePickerQuarter } from "./DatePicker/QuarterPicker";
2020
export { ProFormDatePickerQuarter };
2121

22-
import { ProFormDatePickerYear } from './DatePicker/YearPicker';
22+
import { ProFormDatePickerYear } from "./DatePicker/YearPicker";
2323
export { ProFormDatePickerYear };
2424

25-
import { ProFormDateRangePicker } from './DateRangePicker';
25+
import { ProFormDateRangePicker } from "./DateRangePicker";
2626
export { ProFormDateRangePicker };
2727

28-
import { ProFormDateTimePicker } from './DateTimePicker';
28+
import { ProFormDateTimePicker } from "./DateTimePicker";
2929
export { ProFormDateTimePicker };
3030

31-
import { ProFormDateTimeRangePicker } from './DateTimeRangePicker';
31+
import { ProFormDateTimeRangePicker } from "./DateTimeRangePicker";
3232
export { ProFormDateTimeRangePicker };
3333

34-
import { ProFormTimePicker } from './TimePicker';
34+
import { ProFormTimePicker } from "./TimePicker";
3535
export { ProFormTimePicker };
3636

37-
import { ProFormTimeRangePicker } from './TimeRangePicker';
37+
import { ProFormTimeRangePicker } from "./TimeRangePicker";
3838
export { ProFormTimeRangePicker };
39+
40+
import { ProFormRadio } from "./Radio";
41+
export { ProFormRadio };

0 commit comments

Comments
 (0)