1import React, { useState, useCallback } from 'react';
2import { Flex, ErrorText } from 'indigo-react';
3import cn from 'classnames';
4import { useField } from 'react-final-form';
6import { hasErrors } from 'form/validators';
8const Chip = ({ value, onDelete }) => {
10 <Flex className="r32 ph2 m1 flex-center h7 bg-gray2">
11 <Flex.Item>{value}</Flex.Item>
12 <Flex.Item onClick={onDelete} className="ml1 f6">
19const EmailChipInput = ({ className, name, label }) => {
20 const [chips, _setChips] = useState([]);
22 const [value, setValue] = useState('');
24 const handleChange = useCallback(
26 setValue(event.target.value);
33 meta: { active, error },
35 subscription: { active: true, error: true },
38 const setChips = useCallback(
41 input.onChange(chips);
46 const addToChips = useCallback(() => {
47 setChips([...chips, value]);
49 }, [chips, value, setChips]);
51 const handleKeyDown = useCallback(
53 if (['Enter', 'Tab', ','].includes(event.key) && value !== '') {
54 event.preventDefault();
56 } else if ('Backspace' === event.key && value === '') {
57 event.preventDefault();
58 setChips(chips.slice(0, -1));
61 [setChips, chips, value]
64 const handleDelete = useCallback(
66 setChips([...chips.slice(0, idx), ...chips.slice(idx + 1)]);
71 const handleBlur = useCallback(
81 const hasError = hasErrors(error);
84 <Flex col className={className}>
86 <Flex.Item as="label" className={cn('f6 lh-tall')} htmlFor={name}>
91 className={cn('flex b1 flex-wrap', {
92 'b-green2': active && !hasError,
93 'b-red3': active && hasError,
95 {chips.map((chip, idx) => (
96 <Chip key={idx} value={chip} onDelete={handleDelete(idx)} />
101 className="mv1 mh2 h7 b-none flex-grow"
102 placeholder="Enter an email address"
105 onChange={handleChange}
106 onKeyDown={handleKeyDown}
107 onFocus={input.onFocus}
112 <Flex.Item as={ErrorText} className="mv1">
120export 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