Reputation: 395
i am using ant design with react to generate an editable table with a select option inside it i will fetch the select options using a hook (figbird) which will take some time to fetch but the select will render with no option inside it . i tried to but the data ( an array of Options) inside a state like this
const lines = useFind("lines");
let [lineNumber, setLineNumber] = useState();
const options = {
lines: [],
};
if (lines.error) return "Failed to load resource Machine Status";
if (lines.status === "loading") {
options.lines[0] = {};
setLineNumber[0] = { value: "loading" };
} else {
let arr = lines.data.map((line, i) => {
console.log(i, line);
options.lines[i] = {};
return { value: line.line_number };
});
setLineNumber = arr;
}
and then render my table and select component inside it
<Formik
initialValues={{ machine_name: "", password: "", remember: false }}
validationSchema={LoginSchema}
onSubmit={onSubmit}
>
{({ values, handleSubmit }) => (
<Form {...layout} onSubmit={handleSubmit}>
<Form.Item name="machine_name" label="Name">
<Input
name="machine_name"
placeholder="Machine Name"
prefix={<TagOutlined />}
value={values.machine_name}
/>
</Form.Item>
<Form.Item name="machine_line" label="Line">
<Select
prefix={<ClusterOutlined />}
name="machine_line"
style={{ width: "100%" }}
value={values.machine_line}
>
{lineNumber.map((line) => {
return (
<Option value={line.line_number}>
{line.line_number}
</Option>
);
})}
</Select>
</Form.Item>
<Form.Item name="url" label="URL">
<Input
name="url"
placeholder="URL"
prefix={<LinkOutlined />}
value={values.url}
/>
</Form.Item>
<Row>
<Col offset={6} span={4}>
<SubmitButton>Submit</SubmitButton>
</Col>
<Col offset={5} span={4}>
<ResetButton>Reset</ResetButton>
</Col>
</Row>
,
</Form>
)}
</Formik>
but the select still render nothing inside it ... i checked the data returnd from the fetch and it's coming with no error but the state seems like is not been set
Upvotes: 7
Views: 10904
Reputation: 349
Building onto OP jihad.khorfan's answer.
According to antd documentation for the Select component, the data type should be { label, value }[]
. Thus, using value/label is a better choice than name/label, especially when you need to use fields that are not user interface friendly (e.g. IDs) for backend rendering. A small fix is seen below.
if (lines.error) return "Failed to load resource Machine Status";
if (lines.status === "success") {
for (let i = 0; i < lines.data.length; i++) {
options.lines[i] = {
value: lines.data[i].line_number,
label: lines.data[i].line_number,
};
}
}
Hope this helps future developers. Posting a new answer due to lacking reputation score!
Upvotes: 3
Reputation: 395
now it's working by puting the following code after using the useFind (hook that fetch data)
if (lines.error) return "Failed to load resource Machine Status";
if (lines.status === "success") {
for (let i = 0; i < lines.data.length; i++) {
options.lines[i] = {
value: lines.data[i].line_number,
name: lines.data[i].line_number,
};
}
}
after that put the select options
<Select
loading={lines.data.status}
prefix={<ClusterOutlined />}
options={options["lines"]}
name="machine_line"
style={{ width: "100%" }}
value={values.machine_line}
/>
Upvotes: 4