-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.tsx
148 lines (131 loc) 路 3.93 KB
/
index.tsx
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable jsx-a11y/anchor-is-valid */
import React, { useState } from 'react';
import T from '@airbnb/lunar/lib/components/Translate';
import MenuToggle from '@airbnb/lunar/lib/components/MenuToggle';
import { Props as DropdownProps } from '@airbnb/lunar/lib/components/Dropdown';
import { Props as MenuProps } from '@airbnb/lunar/lib/components/Menu';
import Link from '@airbnb/lunar/lib/components/Link';
import useStyles from '@airbnb/lunar/lib/hooks/useStyles';
import Row from './private/Row';
import { styleSheet } from './styles';
export type Props = {
/** Accessibility label. */
accessibilityLabel: string;
/** Number of currently active filters. */
activeCount?: number;
/** Form components to be shown on the expanded view. */
children?: React.ReactNode;
/** Props to pass to the `Dropdown` component. */
dropdownProps?: Partial<DropdownProps>;
/** If true, will not close the menu when an outside element is clicked. */
ignoreClickOutside?: boolean;
/** If true, will not close the menu when the menu is reset and cleared. */
keepOpenOnClear?: boolean;
/** Increase font size to large. */
large?: boolean;
/** Props to pass to the `Menu` component. */
menuProps?: Partial<MenuProps>;
/** Callback fired when the apply button is clicked. */
onApply?: () => void;
/** Callback fired when the menu is reset and cleared. */
onClear?: () => void;
/** Callback fired when the menu popover is closed. */
onHide?: () => void;
/** Callback fired when the menu popover is opened. */
onShow?: () => void;
/** Decrease font size to small. */
small?: boolean;
/** Z-index of the menu. */
zIndex?: number;
};
export type State = {
opened: Boolean;
};
/** A button that opens a dropdown that shows filter options for a table or similar component. */
export default function FilterMenu({
accessibilityLabel,
activeCount,
children,
dropdownProps = { right: 0 },
ignoreClickOutside,
keepOpenOnClear,
large,
menuProps,
small,
zIndex = 1,
onApply,
onClear,
onHide,
onShow,
}: Props) {
const [styles, cx] = useStyles(styleSheet);
const [opened, setOpened] = useState(false);
const handleShowFilters = () => {
setOpened(true);
if (onShow) {
onShow();
}
};
const handleHideFilters = () => {
setOpened(false);
if (onHide) {
onHide();
}
};
const handleApply = () => {
if (onApply) {
onApply();
}
handleHideFilters();
};
const handleClear = () => {
if (onClear) {
onClear();
}
if (!keepOpenOnClear) {
handleHideFilters();
}
};
const activeCountLabel =
activeCount && activeCount > 0 ? (
<T
k="lunar.filter.filterCount"
phrase="%{smartCount} Filter||||%{smartCount} Filters"
smartCount={activeCount}
context="Number of filters applied within a form"
/>
) : null;
const toggleLabel = opened ? (
<T k="lunar.filter.close" phrase="Close filters" context="Filter menu toggle button label" />
) : (
<T k="lunar.filter.open" phrase="Open filters" context="Filter menu toggle button label" />
);
return (
<MenuToggle
inverted
ignoreClickOutside={ignoreClickOutside}
accessibilityLabel={accessibilityLabel}
dropdownProps={dropdownProps}
large={large}
menuProps={menuProps}
small={small}
toggleLabel={activeCountLabel || toggleLabel}
zIndex={zIndex}
onHide={handleHideFilters}
onShow={handleShowFilters}
>
{children}
<Row>
<section className={cx(styles.controls)}>
<Link type="submit" onClick={handleApply}>
<T k="lunar.common.apply" phrase="Apply" context="Apply filters button label" />
</Link>
<Link muted type="reset" onClick={handleClear}>
<T k="lunar.common.reset" phrase="Reset" context="Button label to reset a form" />
</Link>
</section>
</Row>
</MenuToggle>
);
}
export { Row };