1import React, { useCallback, useRef, useState } from 'react';
2import cn from 'classnames';
4import Flex from './Flex';
5import { ErrorText } from './Typography';
6import useOnClickOutside from 'indigo-react/lib/useOnClickOutside';
7import AccessoryIcon from './AccessoryIcon';
8import { useField } from 'react-final-form';
10// NOTE: if we really care about accessibility, we should pull in a dependency
11export default function SelectInput({
22 meta: { active, error, submitting, touched, valid },
27 disabled = disabled || submitting;
29 const [isOpen, setIsOpen] = useState(false);
32 // close select on outside clicks
33 useOnClickOutside(ref, useCallback(() => setIsOpen(false), [setIsOpen]));
35 const toggleOpen = useCallback(() => {
37 setIsOpen(isOpen => !isOpen);
40 const onChange = value => {
41 // construct a pseudo event that sets the value correctly
42 input.onChange({ target: { value } });
48 const text = options.find(o => o.value === input.value).text;
53 className={cn(className, 'mb1 us-none')}
57 pointerEvents: 'none',
58 cursor: 'not-allowed',
61 <Flex.Item as="label" className="f6 lh-tall" htmlFor={name}>
64 <Flex.Item as={Flex} row className="rel clickable" onClick={toggleOpen}>
68 'b b1 p3 outline-none',
71 'bg-white': !disabled,
80 'b-black': !valid && active,
81 'b-yellow3': !valid && !active && touched && error,
82 'b-gray2': !valid && !active && !touched && !error,
87 {isOpen ? placeholder : text}
98 <AccessoryIcon className="gray4">{isOpen ? '▲' : '▼'}</AccessoryIcon>
103 className="abs bg-white b-black b1 z10"
105 top: 'calc(100% + 4px)',
111 {options.map(option => (
114 className="pv2 ph3 hover-bg-grey3"
117 onChange(option.value);
125 {touched && !active && error && (
126 <Flex.Item as={ErrorText} className="mv1">
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