|
| 1 | +import React from 'react'; |
| 2 | +import styled, { StyledComponent } from 'styled-components'; |
| 3 | +import { ButtonIcon, Chip } from 'ui/atoms'; |
| 4 | +import { IconClose } from 'static/icons'; |
| 5 | +import { InputContainer, InputElement } from 'ui/elements'; |
| 6 | +import { Priority } from 'lib/types'; |
| 7 | + |
| 8 | +type MultiSelectOptions = React.OptionHTMLAttributes<HTMLOptionElement>; |
| 9 | + |
| 10 | +interface InputChipProps { |
| 11 | + className?: string; |
| 12 | + disabled: boolean; |
| 13 | + leftIcon: React.ReactNode; |
| 14 | + name?: string; |
| 15 | + onBlur?: (value?: unknown) => void; |
| 16 | + onChange?: React.EventHandler<React.SyntheticEvent>; |
| 17 | + onChipClose: React.EventHandler<React.SyntheticEvent>; |
| 18 | + onClick: React.EventHandler<React.SyntheticEvent>; |
| 19 | + onFocus?: (value?: unknown) => void; |
| 20 | + options: Array<MultiSelectOptions>; |
| 21 | + rightIcon: React.ReactNode; |
| 22 | +} |
| 23 | + |
| 24 | +export const InputChipBase: React.FC<InputChipProps & Priority> = ({ |
| 25 | + className, |
| 26 | + disabled = false, |
| 27 | + leftIcon, |
| 28 | + name = 'multiselect-input', |
| 29 | + onBlur, |
| 30 | + onChange, |
| 31 | + onChipClose, |
| 32 | + onClick, |
| 33 | + onFocus, |
| 34 | + options = [], |
| 35 | + priority = 'secondary', |
| 36 | + rightIcon, |
| 37 | +}) => { |
| 38 | + if (!options) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + let inputField = <InputElement onChange={() => {}} name={name} type="text" disabled />; |
| 43 | + |
| 44 | + if (onChange) { |
| 45 | + inputField = <InputElement onChange={onChange} name={name} type="text" />; |
| 46 | + } |
| 47 | + |
| 48 | + return ( |
| 49 | + <InputContainer |
| 50 | + className={className} |
| 51 | + leftIcon={leftIcon} |
| 52 | + rightIcon={rightIcon} |
| 53 | + disabled={disabled} |
| 54 | + onClick={onClick} |
| 55 | + priority={priority} |
| 56 | + > |
| 57 | + {options.map(({ id, label }) => ( |
| 58 | + <div key={id} data-element="input-chip"> |
| 59 | + <Chip |
| 60 | + key={id} |
| 61 | + text={label} |
| 62 | + priority={priority} |
| 63 | + rightIcon={ |
| 64 | + <ButtonIcon |
| 65 | + data-id={id} |
| 66 | + icon={<IconClose />} |
| 67 | + onClick={onChipClose} |
| 68 | + priority={priority} |
| 69 | + /> |
| 70 | + } |
| 71 | + /> |
| 72 | + </div> |
| 73 | + ))} |
| 74 | + <div>{inputField}</div> |
| 75 | + </InputContainer> |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +export const InputChip = styled(InputChipBase)` |
| 80 | + [data-element='input'] { |
| 81 | + display: flex; |
| 82 | + } |
| 83 | + [data-element='input-chip'] { |
| 84 | + --woly-component-level: 0; |
| 85 | + margin: 0 3px 3px 0; |
| 86 | + } |
| 87 | +` as StyledComponent<'div', Record<string, unknown>, InputChipProps & Priority>; |
0 commit comments