1import React, { useState, useCallback } from 'react';
2import { isEqual } from 'lodash';
3import { Flex, ErrorText } from 'indigo-react';
4import cn from 'classnames';
5import { useField } from 'react-final-form';
7import { hasErrors } from 'form/validators';
9const Chip = ({ value, onDelete, disabled }) => {
12 className={cn('r32 ph2 m1 flex-center h7', {
13 'bg-gray2': !disabled,
14 'bg-gray1 gray4': disabled,
16 <Flex.Item>{value}</Flex.Item>
17 <Flex.Item onClick={disabled ? onDelete : undefined} className="ml1 f6">
24const EmailChipInput = ({ className, name, label, disabled }) => {
25 const [chips, _setChips] = useState([]);
27 const [value, setValue] = useState('');
29 const handleChange = useCallback(
31 setValue(event.target.value);
38 meta: { active, error, dirtySinceLastSubmit },
44 dirtySinceLastSubmit: true,
49 const setChips = useCallback(
53 input.onChange(chips);
58 const addToChips = useCallback(() => {
59 setChips([...chips, value]);
61 }, [chips, value, setChips]);
63 const handleKeyDown = useCallback(
65 if (['Enter', 'Tab', ','].includes(event.key) && value !== '') {
66 event.preventDefault();
68 } else if ('Backspace' === event.key && value === '') {
69 event.preventDefault();
70 setChips(chips.slice(0, -1));
71 } else if (disabled) {
72 event.preventDefault();
75 [setChips, chips, value, addToChips, disabled]
78 const handleDelete = useCallback(
80 setChips([...chips.slice(0, idx), ...chips.slice(idx + 1)]);
85 const handleBlur = useCallback(
92 [addToChips, value, input]
95 const hasError = hasErrors(error);
98 <Flex col className={className}>
100 <Flex.Item as="label" className={cn('f6 lh-tall')} htmlFor={name}>
105 className={cn('flex b1 flex-wrap', {
106 'b-green2': active && !disabled && !hasError,
107 'b-red3': active && !disabled && hasError,
109 {chips.map((chip, idx) => (
114 onDelete={handleDelete(idx)}
120 className="mv1 mh2 h7 b-none flex-grow"
121 placeholder="Enter an email address"
124 onChange={handleChange}
125 onKeyDown={handleKeyDown}
130 <Flex.Item as={ErrorText} className="mv1">
138export default EmailChipInput;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list