moschan / react-native-simple-radio-button

Simple and handy animated radio button component for React Native
MIT License
453 stars 130 forks source link

how to bind selected value in initial #169

Open loki13111997 opened 2 years ago

andre-wells commented 2 years ago

I have a similar issue on this.

Initial value set to -1 on mount and then updated after async load of options and value. But even though I'm passing the "current" value as props, the initial value isn't updated.


export interface IRadioOption<T> {
  label: string;
  value: T;
}

interface IProps<T extends string | number> {
  value?: T;
  options: IRadioOption<T>[];
  onPress: (id?: T) => void;
}

const RadioButtonGroup = <T extends string | number>({value, options, onPress}: IProps<T>) => {
  return (
    <RadioForm
      radio_props={options}
      // @ts-ignore prop not declared on component but is used
      selectedButtonColor={Colors['primary-600']}
      selectedButtonInnerColor={Colors['primary-600']}
      buttonColor={Colors['gray-300']}
      buttonSize={10}
      initial={value ? options.map(x => x.value).indexOf(value) : -1}
      labelStyle={tw`text-gray-700 font-poppinsMedium`}
      onPress={(index: number) => {
        onPress(options.find(x => x.value === index)?.value);
      }}
    />
  );
};