-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathFieldReviewRating.js
131 lines (111 loc) · 3.41 KB
/
FieldReviewRating.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { intlShape, injectIntl } from '../../util/reactIntl';
import { Field } from 'react-final-form';
import classNames from 'classnames';
import { IconReviewStar, ValidationError } from '../../components';
import css from './FieldReviewRating.module.css';
class FieldReviewRatingComponent extends Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.props.input.onChange(event.target.value);
}
render() {
/* eslint-disable no-unused-vars */
const {
rootClassName,
className,
inputRootClass,
customErrorText,
id,
intl,
label,
input,
meta,
...rest
} = this.props;
/* eslint-enable no-unused-vars */
const { touched, error } = meta;
const errorText = customErrorText || error;
const fieldMeta = { touched, error: errorText };
const { value, ...restInputProps } = input;
const inputProps = { ...restInputProps, type: 'radio', name: 'rating', ...rest };
const classes = classNames(rootClassName || css.root, className);
const createStarRating = starCount => {
let inputsAndLabels = [];
// Star inpu order: reverse order expected (5 -> 1) and also input before label
// This is due to CSS selectors.
// Sibling combinator (~) selects following siblings, but we want to select previous siblings
for (let i = starCount; i > 0; i--) {
const inputValue = `${i}`;
const starId = `star${i}`;
const inputId = `${id}.${starId}`;
inputsAndLabels.push(
<input
key={inputId}
id={inputId}
className={css.rateInput}
value={inputValue}
checked={value === inputValue}
{...inputProps}
/>
);
inputsAndLabels.push(
<label
key={`label.${inputId}`}
className={css.label}
htmlFor={inputId}
title={intl.formatMessage({ id: `FieldReviewRating.${starId}` })}
>
<IconReviewStar rootClassName={css.star} />
</label>
);
}
return inputsAndLabels;
};
return (
<div className={classes}>
<fieldset
className={css.ratingFieldSet}
ref={c => {
this.ratingFieldSet = c;
}}
>
{label ? <legend>{label}</legend> : null}
<div className={css.rating}>{createStarRating(5)}</div>
</fieldset>
<ValidationError fieldMeta={fieldMeta} />
</div>
);
}
}
FieldReviewRatingComponent.defaultProps = {
rootClassName: null,
className: null,
customErrorText: null,
label: null,
};
const { string, shape, func, object } = PropTypes;
FieldReviewRatingComponent.propTypes = {
rootClassName: string,
className: string,
id: string.isRequired,
label: string,
// Error message that can be manually passed to input field,
// overrides default validation message
customErrorText: string,
// Generated by final-form's Field component
input: shape({
onChange: func.isRequired,
}).isRequired,
meta: object.isRequired,
// from injectIntl
intl: intlShape.isRequired,
};
const FieldReviewRating = props => {
return <Field component={FieldReviewRatingComponent} {...props} />;
};
export default injectIntl(FieldReviewRating);