summaryrefslogtreecommitdiff
path: root/src/components/Field.js
blob: 8041a3b0e5d8d36bbb1f91ba1c2beb648bfdeef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import s from './Field.module.css';

const { useCallback } = React;

export default function Field({ id, label, value, onChange, ...props }) {
  const valueOnChange = useCallback(e => onChange(e), [onChange]);
  const labelClassName = cx({
    [s.floatAbove]: typeof value === 'string' && value !== ''
  });
  return (
    <div className={s.root}>
      <input id={id} value={value} onChange={valueOnChange} {...props} />
      <label htmlFor={id} className={labelClassName}>
        {label}
      </label>
    </div>
  );
}

Field.propTypes = {
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  type: PropTypes.oneOf(['text', 'number']),
  onChange: PropTypes.func,
  id: PropTypes.string,
  label: PropTypes.string
};