1import React, { useCallback, useRef, useState } from 'react';
2import cn from 'classnames';
3import { useField } from 'react-final-form';
4import useOnClickOutside from 'indigo-react/lib/useOnClickOutside';
6import Flex from './Flex';
7import { ErrorText } from './Typography';
8import AccessoryIcon from './AccessoryIcon';
10// NOTE: if we really care about accessibility, we should pull in a dependency
11export default function SelectInput({
34 disabled = disabled || submitting;
36 const [isOpen, setIsOpen] = useState(false);
39 // close select on outside clicks
40 useOnClickOutside(ref, useCallback(() => setIsOpen(false), [setIsOpen]));
42 const toggleOpen = useCallback(() => {
44 setIsOpen(isOpen => !isOpen);
47 const onChange = value => {
48 // construct a pseudo event that sets the value correctly
49 input.onChange({ target: { value } });
54 const text = options.find(o => o.value === input.value).text;
59 className={cn(className, 'mb1 us-none')}
63 pointerEvents: 'none',
64 cursor: 'not-allowed',
67 <Flex.Item as="label" className="f6 lh-tall" htmlFor={name}>
70 <Flex.Item as={Flex} row className="rel clickable" onClick={toggleOpen}>
74 'b b1 p3 outline-none',
77 'bg-white': !disabled,
86 'b-black': !valid && active,
87 'b-yellow3': !valid && !active && touched && error,
88 'b-gray2': !valid && !active && !touched && !error,
93 {isOpen ? placeholder : text}
104 <AccessoryIcon className="gray4">{isOpen ? '▲' : '▼'}</AccessoryIcon>
109 className="abs bg-white b-black b1 z10"
111 top: 'calc(100% + 4px)',
117 {options.map(option => (
120 className="pv2 ph3 hover-bg-grey3"
123 onChange(option.value);
133 <Flex.Item as={ErrorText} className="mv1">
138 {touched && !active && error && (
139 <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