diff --git a/lib/field/modify/switch.d.ts b/lib/field/modify/switch.d.ts index 24579536de1fc8287959da85f85f66d47c76eb39..bca14e954bc7e182ddd13fa24cad0afc03ebfae9 100644 --- a/lib/field/modify/switch.d.ts +++ b/lib/field/modify/switch.d.ts @@ -2,5 +2,7 @@ import * as React from 'react'; import * as Storage from './storage'; declare class SwitchView extends React.PureComponent { render(): JSX.Element; + _onRadioChange: (e: any) => void; + _onSwitchChange: (value: boolean) => void; } export default SwitchView; diff --git a/lib/field/modify/switch.js b/lib/field/modify/switch.js index 420c65e720d1f99d2d8dace50f0f6aa98ff91a0b..4a68fa89dcc84e0cd1357c7cfe1722b2857875c3 100644 --- a/lib/field/modify/switch.js +++ b/lib/field/modify/switch.js @@ -13,6 +13,17 @@ const Storage = __importStar(require("./storage")); const Render = __importStar(require("../util/render")); const Field = __importStar(require("../../config")); class SwitchView extends React.PureComponent { + constructor() { + super(...arguments); + this._onRadioChange = (e) => { + const { field, onChange } = this.props; + onChange && onChange(field.name, e.target.value); + }; + this._onSwitchChange = (value) => { + const { field, onChange } = this.props; + onChange && onChange(field.name, value); + }; + } render() { const field = this.props.field; const form = this.props.form; @@ -28,10 +39,10 @@ class SwitchView extends React.PureComponent { message: "请填写" + label, }], })(useRadio - ? React.createElement(antd_1.Radio.Group, { name: field.name, defaultValue: initialValue, style: { width: field.options.valueWidth || 200 } }, + ? React.createElement(antd_1.Radio.Group, { name: field.name, defaultValue: initialValue, style: { width: field.options.valueWidth || 200 }, onChange: this._onRadioChange }, React.createElement(antd_1.Radio, { value: true }, field.options.trueLabel), React.createElement(antd_1.Radio, { value: false }, field.options.falseLabel)) - : React.createElement(antd_1.Switch, { checkedChildren: field.options.trueLabel, unCheckedChildren: field.options.falseLabel, style: { width: field.options.valueWidth || "auto" } })), + : React.createElement(antd_1.Switch, { defaultChecked: initialValue, checkedChildren: field.options.trueLabel, unCheckedChildren: field.options.falseLabel, style: { width: field.options.valueWidth || "auto" }, onChange: this._onSwitchChange })), useRadio ? null : Render.unit(field.options.unitLabel, field.options.showTag === undefined ? true : field.options.showTag, field.options.unitWidth), Render.description(field.options.description, field.options.showTag === undefined ? true : field.options.showTag))); } diff --git a/src/field/modify/switch.tsx b/src/field/modify/switch.tsx index 060a9ed93ea0ca88cbab6622d3414af54e93a110..24667bad1f0c9431cc4b8dd09b841b81bc434cdd 100644 --- a/src/field/modify/switch.tsx +++ b/src/field/modify/switch.tsx @@ -27,6 +27,7 @@ class SwitchView extends React.PureComponent { name={field.name} defaultValue={initialValue} style={{ width: field.options.valueWidth || 200 }} + onChange={this._onRadioChange} > {field.options.trueLabel} @@ -36,9 +37,11 @@ class SwitchView extends React.PureComponent { : )} {useRadio ? null : Render.unit(field.options.unitLabel, field.options.showTag === undefined ? true : field.options.showTag, field.options.unitWidth)} @@ -46,6 +49,16 @@ class SwitchView extends React.PureComponent { ); } + + _onRadioChange = (e: any) => { + const { field, onChange } = this.props; + onChange && onChange(field.name, e.target.value); + }; + + _onSwitchChange = (value: boolean) => { + const { field, onChange } = this.props; + onChange && onChange(field.name, value); + }; } Storage.set(Field.Switch.Type, (option) => );