From 0b32350151bcae6d9798f582cadec9683329e223 Mon Sep 17 00:00:00 2001 From: liuzedong02 Date: Mon, 21 Feb 2022 14:48:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AF=84=E5=88=86=20=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EventAction/InputRatingEvent.jsx | 35 +++++++++++++++++++ examples/components/Example.jsx | 6 ++++ src/renderers/Form/InputRating.tsx | 27 +++++++++++--- 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 examples/components/EventAction/InputRatingEvent.jsx diff --git a/examples/components/EventAction/InputRatingEvent.jsx b/examples/components/EventAction/InputRatingEvent.jsx new file mode 100644 index 000000000..7d8b76643 --- /dev/null +++ b/examples/components/EventAction/InputRatingEvent.jsx @@ -0,0 +1,35 @@ +export default { + type: 'page', + title: '评分组件事件', + regions: ['body', 'toolbar', 'header'], + body: [ + { + type: 'tpl', + tpl: 'InputRating组件', + inline: false, + wrapperComponent: 'h2' + }, + { + type: 'form', + debug: true, + body: [ + { + name: "input-rating", + type: "input-rating", + option: "事件", + onEvent: { + change: { + actions: [ + { + actionType: 'toast', + msgType: 'info', + msg: '派发change事件' + } + ] + } + } + } + ] + } + ] +}; diff --git a/examples/components/Example.jsx b/examples/components/Example.jsx index 00f129c65..0f3a1bdb0 100644 --- a/examples/components/Example.jsx +++ b/examples/components/Example.jsx @@ -81,6 +81,7 @@ import TabsEventSchema from './EventAction/TabsEvent'; import UploadEventSchema from './EventAction/UploadEvent'; import SelectEventActionSchema from './EventAction/SelectEvent'; import ButtonEventActionSchema from './EventAction/ButtonEvent'; +import InputRatingEventSchema from './EventAction/InputRatingEvent'; import WizardSchema from './Wizard'; import ChartSchema from './Chart'; import EChartsEditorSchema from './ECharts'; @@ -574,6 +575,11 @@ export const examples = [ label: '标签页组件', path: 'examples/event/tabs', component: makeSchemaRenderer(TabsEventSchema) + }, + { + label: '评分组件', + path: 'examples/event/input-rating', + component: makeSchemaRenderer(InputRatingEventSchema) } ] }, diff --git a/src/renderers/Form/InputRating.tsx b/src/renderers/Form/InputRating.tsx index dcb2d0e17..034c24d1a 100644 --- a/src/renderers/Form/InputRating.tsx +++ b/src/renderers/Form/InputRating.tsx @@ -1,5 +1,6 @@ import React from 'react'; import {FormItem, FormControlProps, FormBaseControl} from './Item'; +import {autobind, createObject} from '../../utils/helper'; import Rating, {textPositionType} from '../../components/Rating'; /** @@ -84,6 +85,27 @@ export default class RatingControl extends React.Component { readOnly: false }; + async dispatchChangeEvent(eventName: string, eventData: any = {}) { + const {dispatchEvent, data} = this.props; + let rendererEvent = await dispatchEvent( + eventName, + createObject(data, { + value: eventData, + }) + ); + + return rendererEvent?.prevented ?? false; + } + + @autobind + async handleChange(value: number) { + const {onChange} = this.props; + + const prevented = await this.dispatchChangeEvent('change', value); + + prevented || (onChange && onChange(value)); + } + render() { const { className, @@ -92,7 +114,6 @@ export default class RatingControl extends React.Component { half, readOnly, disabled, - onChange, onHoverChange, allowClear, char, @@ -122,9 +143,7 @@ export default class RatingControl extends React.Component { charClassName={charClassName} textClassName={textClassName} textPosition={textPosition} - onChange={(value: number) => { - onChange && onChange(value); - }} + onChange={this.handleChange} onHoverChange={(value: number) => { onHoverChange && onHoverChange(value); }}