-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathOption.js
116 lines (100 loc) · 3.39 KB
/
Option.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint-disable react/prop-types */
import React from "react";
import PropTypes from "prop-types";
import Checkbox from "@paprika/checkbox";
import tokens from "@paprika/tokens";
import { PropsContext } from "../../store/PropsProvider";
import { getA11yAttributesForOption } from "../../helpers/DOMAttributes";
import * as sc from "./Option.styles";
const Option = props => {
const {
index,
groupId,
label,
id,
internalHandleOnClick,
isSelected,
hasNoIcon,
isMulti,
hasTag,
...moreProps
} = props;
const providedProps = React.useContext(PropsContext);
const { isReadOnly, size } = providedProps;
const isDisabled = providedProps.isDisabled || props.isDisabled || isReadOnly;
const { CHECKED, UNCHECKED } = Checkbox.types.state;
const [checkedState, setCheckedState] = React.useState(UNCHECKED);
const handleChange = () => setCheckedState(checkedState === CHECKED ? UNCHECKED : CHECKED);
const checkIcon =
isMulti &&
(!hasTag ? (
<Checkbox
checkedState={isSelected ? CHECKED : checkedState}
onChange={handleChange}
style={{ paddingRight: tokens.space }}
/>
) : (
<sc.PlusIcon />
));
return (
<sc.Option
{...moreProps}
{...getA11yAttributesForOption(isSelected)}
hasPreventDefaultOnSelect={props.preventDefaultOnSelect}
id={id}
isDisabled={isDisabled}
isSelected={isSelected}
size={size}
key={index}
onClick={event => internalHandleOnClick({ event, isDisabled, onClick: props.onClick, index })}
data-pka-anchor={isSelected ? "list-option--is-selected" : "list-option"}
data-pka-prevent-default-on-select={props.preventDefaultOnSelect}
tabIndex={-1}
>
{hasNoIcon ? null : checkIcon}
{typeof props.children === "function" ? props.children({ isSelected, isDisabled, id }) : props.children}
</sc.Option>
);
};
Option.displayName = "ListBox.Option";
Option.propTypes = {
/** String, number or JSX content */
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
/* Controls if the option is selected or not, never combine it with defaultIsSelected */
isSelected: PropTypes.bool,
/** Describe if the option started as selected or not */
defaultIsSelected: PropTypes.bool,
/** When no PlusIcon or CheckBox are needed */
hasNoIcon: PropTypes.bool,
/** Describe if the option is enable or not */
isDisabled: PropTypes.bool,
/** Describe if the option is hidden or not */
isHidden: PropTypes.bool,
/** When the children are not a String, label should need to be add so the filter can work */
label: PropTypes.string,
/** Callback for the clicking event */
onClick: PropTypes.func,
/** Value of your option this can be any data structure */
value: PropTypes.any, // eslint-disable-line
/** Internal prop, which shouldn't be documented */
internalHandleOnClick: PropTypes.func,
/** Internal prop, which shouldn't be documented */
id: PropTypes.string,
/** Internal prop, which shouldn't be documented */
preventDefaultOnSelect: PropTypes.bool,
};
Option.defaultProps = {
hasNoIcon: false,
isDisabled: false,
isHidden: false,
id: "",
internalHandleOnClick: () => null,
preventDefaultOnSelect: false,
isSelected: null,
defaultIsSelected: null,
label: null,
onClick: null,
value: null,
};
const OptionMemoized = React.memo(Option);
export default OptionMemoized;