hossein-zare / react-native-dropdown-picker

A single / multiple, categorizable, customizable, localizable and searchable item picker (drop-down) component for react native which supports both Android & iOS.
https://hossein-zare.github.io/react-native-dropdown-picker-website/
MIT License
970 stars 294 forks source link

value is: Function anonymous #693

Closed Acidias closed 10 months ago

Acidias commented 10 months ago

I'm trying to use with formik but I'm keep getting "Value: [Function anonymous]"

const [open, setOpen] = useState(false);
   const [value, setValue] = useState(null);
   const [items, setItems] = useState([
      { label: "Tier 1", value: "Tier 1" },
      { label: "Tier 2", value: "Tier 2" },
   ]);

   const onOpen = useCallback(() => {
      console.log("Open");
   });
  const initialValues = {
      buildingTo: selectedBuildingTo,
      tier: "",
   };
const handleFormSubmit = (values) => {
      console.log("values ", values);
      const allValues = {
         contract: selectedContract,
         building: selectedBuilding,
         buildingTo: selectedBuildingTo,
         tier: values.tier,
      };

      console.log("All Form Values:", allValues);

      setIsLoading(true); // Show spinner
}
<Formik
               initialValues={initialValues}
               validationSchema={validationSchema}
               onSubmit={handleFormSubmit}
            >
               {({
                  handleChange,
                  handleBlur,
                  handleSubmit,
                  setFieldValue,
                  values,
                  errors,
               }) => {
                  const onSubmit = () => {
                     console.log("Submit button pressed. Form values:", values);
                     handleFormSubmit(values);
                  };
                  return (
                     <>
                        <Modal
                           transparent={true}
                           animationType="slide"
                           visible={isLoading}
                           onRequestClose={() => {}}
                        >
                           <View style={styles.overlay}>
                              <ActivityIndicator size="large" color="#4A7CFF" />
                           </View>
                        </Modal>

                        <View style={{ zIndex: 1, padding: 0 }}>
                           <ContractBuildingSelector2
                              onContractSelect={setSelectedContract}
                              onBuildingSelect={setSelectedBuilding}
                              onBuildingSelectTo={setSelectedBuildingTo}
                           />
                        </View>
                        <DropDownPicker
                           open={open}
                           onOpen={onOpen}
                           value={value}
                           items={items}
                           setOpen={setOpen}
                           setValue={(value) => {
                              setValue(value);
                              console.log("Value:", value);
                              setFieldValue("tier", value);
                           }}
                           setItems={setItems}
                        />

                        <View style={styles.section}>
                           <TouchableOpacity
                              style={styles.button}
                              onPress={onSubmit}
                              disabled={isLoading} // Disable button while submitting
                           >
                              <Text style={styles.buttonText}>Submit</Text>
                           </TouchableOpacity>
                        </View>
                     </>
                  );
               }}
            </Formik>

I tried lot of things, and the same setValue methode I use in a different form and there is working fine... Any ide what is the problem?

px-amaac commented 10 months ago

const value = valueFunction() === your value. 🤷

Acidias commented 10 months ago

Thank you! :) I'm new yet