aboutsummaryrefslogblamecommitdiffstats
path: root/packages/website/ts/components/ui/multi_select.tsx
blob: 2cf44cae1f0973900360c4c615ae6f85c8bde556 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                          
                                        


                                        
                                                                



                                   
                              

                                   
                    




                                                                    

                                      
                                                                              
                






                                                 
                                       

                                        

                                                                  
                                              
                                                                                                            
                      






                                       
                                                                

                         
                             


                                                                                 
                      

               
                    
       


                                                                                                               


                    
import { colors } from '@0x/react-shared';
import * as _ from 'lodash';
import * as React from 'react';

import { Container } from './container';

export interface MultiSelectItemConfig {
    value: string;
    renderItemContent: (isSelected: boolean) => React.ReactNode;
    onClick?: () => void;
}

export interface MultiSelectProps {
    selectedValues?: string[];
    items: MultiSelectItemConfig[];
    backgroundColor?: string;
    height?: string;
}

export class MultiSelect extends React.Component<MultiSelectProps> {
    public static defaultProps = {
        backgroundColor: colors.white,
    };
    public render(): React.ReactNode {
        const { items, backgroundColor, selectedValues, height } = this.props;
        return (
            <Container
                backgroundColor={backgroundColor}
                borderRadius="4px"
                width="100%"
                height={height}
                overflowY="scroll"
            >
                {_.map(items, item => (
                    <MultiSelectItem
                        key={item.value}
                        renderItemContent={item.renderItemContent}
                        backgroundColor={backgroundColor}
                        onClick={item.onClick}
                        isSelected={_.isUndefined(selectedValues) || _.includes(selectedValues, item.value)}
                    />
                ))}
            </Container>
        );
    }
}

export interface MultiSelectItemProps {
    renderItemContent: (isSelected: boolean) => React.ReactNode;
    isSelected?: boolean;
    onClick?: () => void;
    backgroundColor?: string;
}

export const MultiSelectItem: React.StatelessComponent<MultiSelectItemProps> = ({
    renderItemContent,
    isSelected,
    onClick,
    backgroundColor,
}) => (
    <Container cursor="pointer" shouldDarkenOnHover={true} onClick={onClick} backgroundColor={backgroundColor}>
        <Container borderBottom={`1px solid ${colors.lightestGrey}`} margin="0px 15px" padding="10px 0px">
            {renderItemContent(isSelected)}
        </Container>
    </Container>
);