From ac06372d7e95c519eb1299b1c3e3b22572a01617 Mon Sep 17 00:00:00 2001 From: Curry Date: Wed, 13 Mar 2024 11:08:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0radioGroup=E7=9A=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/RadioGroup/PropsType.tsx | 1 + .../src/components/RadioGroup/index.tsx | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/taro-components-rn/src/components/RadioGroup/PropsType.tsx b/packages/taro-components-rn/src/components/RadioGroup/PropsType.tsx index 04e2c9be16f5..1bcab352ed32 100644 --- a/packages/taro-components-rn/src/components/RadioGroup/PropsType.tsx +++ b/packages/taro-components-rn/src/components/RadioGroup/PropsType.tsx @@ -20,6 +20,7 @@ export interface RadioGroupState { export interface RadioGroupProps extends FormItemProps, _RadioGroupProps { children?: React.ReactNode; style?: StyleProp | any; + checkedValue?: string; onChange?: (evt: EventGroupOnChange) => void; _onGroupDataInitial?: (value: any) => void; } diff --git a/packages/taro-components-rn/src/components/RadioGroup/index.tsx b/packages/taro-components-rn/src/components/RadioGroup/index.tsx index 369d6a377f04..b0d16fcc2ea6 100644 --- a/packages/taro-components-rn/src/components/RadioGroup/index.tsx +++ b/packages/taro-components-rn/src/components/RadioGroup/index.tsx @@ -14,8 +14,13 @@ import { RadioGroupProps, RadioGroupState, EventOnChange } from './PropsType' class _RadioGroup extends React.Component { static displayName = '_RadioGroup' - state: RadioGroupState = { - checkedValue: undefined + + constructor(props: RadioGroupProps) { + super(props); + const {checkedValue} = props; + this.state = { + checkedValue + }; } // eslint-disable-next-line default-param-last @@ -30,6 +35,14 @@ class _RadioGroup extends React.Component { }) } + componentDidUpdate (prevProps) { + // 检测 props 是否发生变化 + if (this.props.checkedValue !== prevProps.checkedValue) { + // 执行相应的操作,比如更新组件状态 + this.setState({ checkedValue: this.props.checkedValue }); + } + } + findAndAttachCb = (children: React.ReactNode): React.ReactNode => { return React.Children.toArray(children).map((child: any) => { if (!child.type) return child