aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/components/ui/select.tsx
blob: e4fb50f59e45795b6994c7e89126bd7eb0a67460 (plain) (blame)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { colors } from '@0x/react-shared';
import * as _ from 'lodash';
import * as React from 'react';

import { zIndex } from 'ts/style/z_index';

import { Container } from './container';
import { Overlay } from './overlay';
import { Text } from './text';

export interface SelectItemConfig {
    text: string;
    onClick?: () => void;
}

export interface SelectProps {
    value: string;
    label?: string;
    items: SelectItemConfig[];
    onOpen?: () => void;
    border?: string;
    fontSize?: string;
    iconSize?: number;
    textColor?: string;
    labelColor?: string;
    backgroundColor?: string;
}

export interface SelectState {
    isOpen: boolean;
}

export class Select extends React.Component<SelectProps, SelectState> {
    public static defaultProps = {
        items: [] as SelectItemConfig[],
        textColor: colors.black,
        backgroundColor: colors.white,
        fontSize: '16px',
        iconSize: 25,
    };
    public state: SelectState = {
        isOpen: false,
    };
    public render(): React.ReactNode {
        const { value, label, items, border, textColor, labelColor, backgroundColor, fontSize, iconSize } = this.props;
        const { isOpen } = this.state;
        const hasItems = !_.isEmpty(items);
        const borderRadius = isOpen ? '4px 4px 0px 0px' : '4px';
        return (
            <React.Fragment>
                {isOpen && (
                    <Overlay
                        style={{
                            zIndex: zIndex.overlay,
                            backgroundColor: 'rgba(255, 255, 255, 0)',
                        }}
                        onClick={this._closeDropdown}
                    />
                )}
                <Container position="relative">
                    <Container
                        cursor={hasItems ? 'pointer' : undefined}
                        onClick={this._handleDropdownClick}
                        borderRadius={borderRadius}
                        hasBoxShadow={isOpen}
                        border={border}
                        backgroundColor={backgroundColor}
                        padding="0.5em 0.8em"
                        width="100%"
                    >
                        <Container className="flex justify-between">
                            <Text fontSize={fontSize} fontColor={textColor}>
                                {value}
                            </Text>
                            <Container>
                                {label && (
                                    <Text fontSize={fontSize} fontColor={labelColor}>
                                        {label}
                                    </Text>
                                )}
                                {hasItems && (
                                    <Container marginLeft="5px" display="inline-block">
                                        <i
                                            className="zmdi zmdi-chevron-down"
                                            style={{ fontSize: iconSize, color: colors.darkGrey }}
                                        />
                                    </Container>
                                )}
                            </Container>
                        </Container>
                    </Container>
                    {isOpen && (
                        <Container
                            width="100%"
                            position="absolute"
                            onClick={this._closeDropdown}
                            zIndex={zIndex.aboveOverlay}
                            hasBoxShadow={true}
                        >
                            {_.map(items, (item, index) => (
                                <SelectItem
                                    key={item.text}
                                    {...item}
                                    isLast={index === items.length - 1}
                                    backgroundColor={backgroundColor}
                                    textColor={textColor}
                                    border={border}
                                />
                            ))}
                        </Container>
                    )}
                </Container>
            </React.Fragment>
        );
    }
    private readonly _handleDropdownClick = (): void => {
        if (_.isEmpty(this.props.items)) {
            return;
        }
        const isOpen = !this.state.isOpen;
        this.setState({
            isOpen,
        });

        if (isOpen && this.props.onOpen) {
            this.props.onOpen();
        }
    };
    private readonly _closeDropdown = (): void => {
        this.setState({
            isOpen: false,
        });
    };
}

export interface SelectItemProps extends SelectItemConfig {
    text: string;
    onClick?: () => void;
    isLast: boolean;
    backgroundColor?: string;
    border?: string;
    textColor?: string;
    fontSize?: string;
}

export const SelectItem: React.StatelessComponent<SelectItemProps> = ({
    text,
    onClick,
    isLast,
    border,
    backgroundColor,
    textColor,
    fontSize,
}) => (
    <Container
        onClick={onClick}
        cursor="pointer"
        backgroundColor={backgroundColor}
        padding="0.8em"
        borderTop="0"
        border={border}
        shouldDarkenOnHover={true}
        borderRadius={isLast ? '0px 0px 4px 4px' : undefined}
        width="100%"
    >
        <Text fontSize={fontSize} fontColor={textColor}>
            {text}
        </Text>
    </Container>
);