"색상, 크기 등"제품 옵션이 있습니다. 사용자가 "색상에서 빨간색 만 선택했습니다"옵션에서 한 가지를 선택할 수 있습니다. 이번에는 선택한 항목을 별도의 "모든 옵션 사용자가 선택한 옵션"으로 광고합니다. 하지만 문제는 사용자가 동일한 필드 "예 : 색상"에 중복 된 옵션을 추가 할 수 있다는 것입니다. 그러면이 문제를 어떻게 해결할 수 있습니까? 마지막 항목은 다음과 같아야합니다. 예를 들어 별도의 옵션에서 하나
SelectedOptions = [
{ name:"red",id:88,optionType:"Color"},
{ name:"XL",id:22,optionType:"Size"},
....
]
하지만 내가 지금가는 것은
SelectedOptions = [
{ name:"red",id:88,optionType:"Color"},
{ name:"green",id:87,optionType:"Color"},
{ name:"XL",id:22,optionType:"Size"},
{ name:"M",id:22,optionType:"Size"},
....
];
나는 선택된 것을 SelectedOptions 배열로 푸시하기 때문에 그것을 확인하여 해결하려고 노력하고 optionType
있지만 좋은 아이디어를 얻지 못했습니다.
코드 스 니펫
const [selectedIndexColor, setSelectedIndexColor] = useState<
number | undefined
>();
const [selectedIndexText, setSelectedIndexText] = useState<number | null>();
const [selectedIndexImage, setSelectedIndexImage] = useState<number | null>();
interface ManipulateValueOptionProp extends valueOptionProp {
optionType: string;
}
const [allOptions, setAllOptions] = useState<ManipulateValueOptionProp[]>([]);
const renderValue = (value: valueOptionProp, type: string) => {
const selectedColor = selectedIndexColor === value.id;
const selectedText = selectedIndexText === value.id;
const selectedImage = selectedIndexImage === value.id;
switch (type) {
case 'text': {
return (
<View
style={[
styles.selectedText,
{backgroundColor: selectedText ? 'black' : 'transparent'},
]}>
<Text
style={{
textAlign: 'center',
color: selectedText ? 'white' : 'black',
fontSize: 12,
}}>
{value.name_en}
</Text>
</View>
);
}
case 'Color': {
return (
<>
<View
// @ts-ignore
// eslint-disable-next-line react-native/no-inline-styles
style={{
width: 53,
height: 53,
backgroundColor: value.display_value,
}}
/>
{selectedColor ? (
<View style={styles.selectedColor}>
<CheckmarkIcon color="black" />
</View>
) : null}
</>
);
}
case 'images': {
return (
<>
<Image
// @ts-ignore
source={{uri: value.display_value}}
// eslint-disable-next-line react-native/no-inline-styles
style={{width: 53, height: 53, backgroundColor: '#0f4c7f'}}
/>
{selectedImage ? (
<View style={styles.selectedColor}>
<CheckmarkIcon />
</View>
) : null}
</>
);
}
default: {
return null;
}
}
};
{item.options.map((option) => {
return (
<View style={styles.optionsBox}>
<Text style={styles.optionTxt}>{option.label_en}</Text>
<View style={{flexDirection: 'row'}}>
{option.values.map((value: valueOptionProp) => {
return (
<ScalePressable
key={`${option.id}${value.id}`}
onPress={() => {
option.type === 'Color' &&
setSelectedIndexColor(value.id);
option.type === 'text' && setSelectedIndexText(value.id);
option.type === 'images' &&
setSelectedIndexImage(value.id);
if (
!allOptions.some(
(alreadyExist) => alreadyExist.id === value.id,
)
) {
setAllOptions((options) => [
...options,
{...value, optionType: option.type},
]);
}
}}>
<View style={styles.values}>
{renderValue(value, option.type)}
</View>
</ScalePressable>
);
})}
</View>
</View>
);
})}
가능한 한 가지 해결책이 있습니다. 새 옵션과 동일한 옵션 유형을 가진 모든 옵션을 제거한 다음 새 옵션을 새 어레이에 추가하여 새 어레이를 만드는 것을 고려할 수 있습니다.
let selectedOptions = [
{ name: 'red', id: 88, optionType: 'Color' },
{ name: 'XL', id: 22, optionType: 'Size' },
];
let newOption = { name: 'green', id: 87, optionType: 'Color' };
selectedOptions = [
...selectedOptions.filter(
option => option.optionType != newOption.optionType
),
newOption,
];
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다