I am creating a ecommerce project to learn react, and i guess thats the best way to learn a language by doing a complex project so that you get to know every aspect of it. but now i am stuck with something that i searched for everywhere but couldnt find.
Basically i want to create a section where the person can add product variants like color and sizes to it, like the example below
it doesnt have to be complicated but if i could add into an object depending on the values the user inputs is what all i need.
if someone could help me that would be great! Thank you!!!
I think I have done similar project.
Flow was as follows:
const [words, setWords] = useState([]);
...
function handleWord(e) {
if (e.key === "Enter") {
let temp = words ? words : [];
temp.push(value);
setWords(temp);
setValue("");
}
}
...
<input
type="text"
onChange={(e) => setValue(e.target.value)}
onKeyDown={handleWord}
value={value}
placeholder="Enter one keyword..."
/>
You can modify this depending on how you are constructing object.
Is that what you want?
[{id:110, hingeName : "Color", priority: 1 }, {id:220, hingeName: "Size", priority:2}]
productData = {
id:12132,
prodcutImg:'https://abc.img.in',
productPrice:'$24',
prodAttribute:[{id:110, value:'Black'},{id:220, value:"XL"}],
variantMeta:
{
hinges:[
{id:110, hingeName : "Color", priority: 1 },
{id:220, hingeName: "Size", priority:2}
],
hingesValues: [
{110: ["Black","Red","White"]},
{220: ['XS','S','L','XL','XXL']}
],
variantsOfCurrentProducts: [
{variant_id:'12131', attributes: { 110:'Black',220:'S'}},
{variant_id:'12132', attributes: { 110:'Red',220:'XL'}},
{variant_id:'12133', attributes: { 110:'White',220:'XS'}},
{variant_id:'12134', attributes: { 110:'Red',220:'XL'}}
]
}
}]
Color :
Red Black White
Size :
XS S L XL XXL
Color :
Red "Black" White
Size :
XS S L "XL" XXL
NOTE: I have used "double_quote" for showing the attribute of current product. You can render it with different background and colour on UI.
Now make sure that hinge at the top most priority should always be enabled so that user can be able to select that at any point of time
If someone click on the hinge having 2nd highest priority then filter all those variants which have attribute with highest priority and the 2nd highest priority and render them as selectable values and all those variants which don't have these attributes disable them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.