-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathRadioFilters.js
67 lines (53 loc) · 1.55 KB
/
RadioFilters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { useEffect, useState } from 'react'
import { useIntl } from 'react-intl'
import { RadioGroup } from 'vtex.styleguide'
import ShippingActionButton from './ShippingActionButton'
import useShippingActions from '../hooks/useShippingActions'
const RadioItem = ({ facet }) => {
const intl = useIntl()
const { actionLabel, actionType, openDrawer } = useShippingActions(facet)
return (
<div>
<div>{facet.name}</div>
{actionType ? (
<ShippingActionButton
label={intl.formatMessage({ id: actionLabel ?? 'none' })}
openDrawer={openDrawer}
/>
) : undefined}
</div>
)
}
const RadioFilters = ({ facets, onChange }) => {
const selectedOption = facets.find(facet => facet.selected)
const lastValue = selectedOption ? selectedOption.value : undefined
const [selectedValue, setSelectedValue] = useState(lastValue)
useEffect(() => {
setSelectedValue(lastValue)
}, [lastValue])
const onRadioSelect = e => {
const { value } = e.currentTarget
setSelectedValue(value)
const clickedFacet = facets.find(facet => facet.value === value)
if (clickedFacet.selected) {
return
}
onChange(clickedFacet)
}
return (
<RadioGroup
hideBorder
size="small"
name="shipping"
options={facets.map(facet => ({
id: facet.value,
value: facet.value,
label: <RadioItem facet={facet} />,
disabled: facet.quantity === 0,
}))}
value={selectedValue}
onChange={onRadioSelect}
/>
)
}
export default RadioFilters