1import React, { useState, useCallback } from 'react';
2import { Flex, ErrorText } from 'indigo-react';
3import cn from 'classnames';
4import { useField } from 'react-final-form';
5import { useFieldArray } from 'react-final-form-arrays';
7import { hasErrors } from 'form/validators';
9const Chip = ({ onDelete, disabled, name }) => {
10 const { input } = useField(name, { subscription: { value: true } });
13 className={cn('r32 ph2 m1 flex-center h7', {
14 'bg-gray2': !disabled,
15 'bg-gray1 gray4': disabled,
17 <Flex.Item>{input.value}</Flex.Item>
19 onClick={disabled ? undefined : onDelete}
20 className="ml1 f6 pointer-hover">
27const EmailChipInput = ({ className, name, label, disabled }) => {
30 meta: { error, active },
31 } = useFieldArray(name, { subscription: { error: true, active: true } });
33 const [value, setValue] = useState('');
35 const handleChange = useCallback(
37 event.preventDefault();
38 setValue(event.target.value);
43 const addToChips = useCallback(() => {
44 fields.concat(value.split(/[\n\t, ]+/));
46 }, [value, fields, setValue]);
48 const handleKeyDown = useCallback(
50 if (['Enter', 'Tab', ',', ' '].includes(event.key) && value !== '') {
51 event.preventDefault();
53 } else if ('Backspace' === event.key && value === '') {
54 event.preventDefault();
56 } else if (disabled) {
57 event.preventDefault();
60 [fields, value, disabled, addToChips]
63 const handleDelete = useCallback(
70 const handleBlur = useCallback(
80 const hasError = hasErrors(error);
83 <Flex col className={className}>
85 <Flex.Item as="label" className={cn('f6 lh-tall')} htmlFor={name}>
90 className={cn('flex b1 flex-wrap', {
91 'b-green2': active && !disabled && !hasError,
92 'b-red3': active && !disabled && hasError,
94 {fields.map((name, idx) => (
99 onDelete={handleDelete(idx)}
105 className="mv1 mh2 h7 b-none flex1"
106 placeholder="Enter an email address"
108 onChange={handleChange}
109 onKeyDown={handleKeyDown}
114 <Flex.Item as={ErrorText} className="mv1">
115 {JSON.stringify(error)}
122export 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