123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import {useCallback} from 'react';
- import styled from '@emotion/styled';
- import {Choices} from 'sentry/types';
- import {defined} from 'sentry/utils';
- const MultipleCheckboxWrapper = styled('div')`
- display: flex;
- flex-wrap: wrap;
- `;
- const Label = styled('label')`
- font-weight: normal;
- white-space: nowrap;
- margin-right: 10px;
- margin-bottom: 10px;
- width: 20%;
- `;
- const CheckboxLabel = styled('span')`
- margin-left: 3px;
- `;
- type SelectedValue = (string | number)[];
- type Props = {
- choices: Choices;
- value: (string | number)[];
- disabled?: boolean;
- onChange?: (value: SelectedValue, event: React.ChangeEvent<HTMLInputElement>) => void;
- };
- function MultipleCheckbox({choices, value, disabled, onChange}: Props) {
- const handleChange = useCallback(
- (selectedValue: string | number, e: React.ChangeEvent<HTMLInputElement>) => {
- let newValue: SelectedValue = [];
- if (typeof onChange !== 'function') {
- return;
- }
- if (e.target.checked) {
- newValue = value ? [...value, selectedValue] : [value];
- } else {
- newValue = value.filter(v => v !== selectedValue);
- }
- onChange(newValue, e);
- },
- [value, onChange]
- );
- return (
- <MultipleCheckboxWrapper>
- {choices.map(([choiceValue, choiceLabel]) => (
- <LabelContainer key={choiceValue}>
- <Label>
- <input
- type="checkbox"
- value={choiceValue}
- onChange={e => handleChange(choiceValue, e)}
- disabled={disabled}
- checked={defined(value) && value.indexOf(choiceValue) !== -1}
- />
- <CheckboxLabel>{choiceLabel}</CheckboxLabel>
- </Label>
- </LabelContainer>
- ))}
- </MultipleCheckboxWrapper>
- );
- }
- export default MultipleCheckbox;
- const LabelContainer = styled('div')`
- width: 100%;
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- width: 50%;
- }
- @media (min-width: ${p => p.theme.breakpoints.medium}) {
- width: 33.333%;
- }
- @media (min-width: ${p => p.theme.breakpoints.large}) {
- width: 25%;
- }
- `;
|