UNPKG

1.4 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import { useField, useFormikContext } from 'formik';
4import FeedbackButton from './FeedbackButton';
5
6const SmileField = ({ name, options, onChange }) => {
7 const [{ value }] = useField(name);
8 const { setFieldValue } = useFormikContext();
9
10 return options.map(option => (
11 <FeedbackButton
12 style={{ flex: 1, margin: '0 2% 0 2%' }}
13 key={option.icon}
14 icon={option.icon}
15 iconSize="2x"
16 value={option}
17 active={value && value.icon}
18 onClick={() => {
19 setFieldValue(name, option);
20 if (onChange) {
21 onChange(option);
22 }
23 }}
24 >
25 {option.description}
26 </FeedbackButton>
27 ));
28};
29
30SmileField.propTypes = {
31 name: PropTypes.string.isRequired,
32 options: PropTypes.arrayOf(
33 PropTypes.shape({
34 icon: PropTypes.string,
35 description: PropTypes.string,
36 placeholder: PropTypes.string,
37 })
38 ),
39 onChange: PropTypes.func,
40};
41
42SmileField.defaultProps = {
43 options: [
44 {
45 icon: 'smile',
46 description: 'Smiley face',
47 placeholder: 'What do you like?',
48 },
49 {
50 icon: 'meh',
51 description: 'Meh face',
52 placeholder: 'What would you improve?',
53 },
54 {
55 icon: 'frown',
56 description: 'Frowny face',
57 placeholder: "What don't you like?",
58 },
59 ],
60};
61
62export default SmileField;