简体   繁体   English

将道具传递到自定义组件中

[英]Passing a prop into a custom component

I have a custom component of 我有一个自定义组件

const Search = (props) => {
const { type: TYPE, name: NAME, label: LABEL, onSelect, filter, value } = props;
const [data, setData] = useState([]);
const [select, setSelect] = useState(value || -1);

const applyFilter = (data) => {
    let result = data;
    if (filter) {
        result = filter(data);
    }
    return result;
};

useEffect(() => {
    getLookupData(TYPE)
        .then((response) => {
            setData(response);
        })
        .catch((error) => {
            if (error === HttpStatus.NOT_FOUND) {
                setData([]);
            } else {
                throw error;
            }
        });
}, [TYPE]);

useEffect(() => {
    if (value) {
        setSelect(value);
    }
}, [value]);

const options = applyFilter(data).map((item) => (
    <MenuItem value={item.id} key={item.id}>
        {item[NAME]}
    </MenuItem>
));

const handleChange = (event) => {
    setSelect(event.target.value);
    onSelect && onSelect(event);
};

const { classes } = props;
const labelProps = { ...(props.required && { required: props.required }) };
return (
    <FormControl className={classes.formControl} id={NAME} error={props.error}>
        <FormControlLabel control={<InputLabel htmlFor={NAME} {...labelProps}>{LABEL}</InputLabel>} />
        <Select
            name={TYPE}
            value={select}
            onChange={handleChange}
            disabled={props.disabled || options.length === 0}
            input={<Input name={TYPE} id={NAME} />}
        >
            <MenuItem value=''>
                <em>None</em>
            </MenuItem>
            {options}
        </Select>
    </FormControl>
);
};

It takes in a menu item by default of None, however i want to pass this None menuitem as a prop and call it when i want too, as for some fields i want the option of 'none' and for some fields i do not want the option of none, currently the system is hard coded to have none appear on every drop down and i dont wish for this to happen 它默认接受None菜单项,但是我想将此None菜单项作为道具传递,并在需要时调用它,就某些字段而言,我希望选择“无”,对于某些字段,我不希望如此无选项,当前系统已被硬编码为每个下拉列表均不显示任何内容,我不希望这种情况发生

<Search 
    required
    type="contractOptionToExtend"
    name="optionToExtend"
    label="Option To Extend"
    id="contractOptionToExtend"
    onSelect={change.bind(null, 'contractOptionToExtend')}
    error={touched.contractOptionToExtend && Boolean(errors.contractOptionToExtend)}
    value={values.contractOptionToExtend}
/>

On the above field i want to say wether or not i want the None option, I can solve this by passing it as a prop to the Search component but im not totally sure how, I have done something similar for props.required as you can see but currently i cannot do the same for the menu item. 在上面的字段中,我想说是否要使用None选项,我可以通过将其作为道具传递给Search组件来解决此问题,但不确定如何,我对props.required做了类似的操作看到,但目前我不能为菜单项做同样的事情。

{props.showNone 
    ? <MenuItem value=''>
        <em>None</em>
    </MenuItem> 
    : null
}

then within the desired field 然后在所需字段内

showNone={true}

credit to @Abhey Sehgal 归功于@Abhey Sehgal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM