11import React from 'react' ;
22import PropTypes from 'prop-types' ;
3- import { randomString , isFalsy } from '@togglecorp/fujs' ;
3+ import {
4+ _cs ,
5+ randomString ,
6+ isFalsy ,
7+ } from '@togglecorp/fujs' ;
48
59import styles from './styles.scss' ;
610
@@ -16,12 +20,22 @@ const propTypes = {
1620 error : PropTypes . string ,
1721 disabled : PropTypes . bool ,
1822 readOnly : PropTypes . bool ,
23+ renderer : PropTypes . func ,
24+ rendererClassName : PropTypes . string ,
25+ rendererParams : PropTypes . func ,
26+ index : PropTypes . number . isRequired ,
27+ datum : PropTypes . object , // eslint-disable-line react/forbid-prop-types
28+ data : PropTypes . array . isRequired , // eslint-disable-line react/forbid-prop-types
1929} ;
2030
2131const defaultProps = {
2232 error : '' ,
2333 disabled : false ,
2434 readOnly : false ,
35+ renderer : undefined ,
36+ rendererClassName : '' ,
37+ rendererParams : undefined ,
38+ datum : { } ,
2539} ;
2640
2741export default class SegmentOption extends React . PureComponent {
@@ -35,64 +49,61 @@ export default class SegmentOption extends React.PureComponent {
3549 this . inputId = randomString ( ) ;
3650 }
3751
38- getClassName = ( ) => {
52+ render ( ) {
3953 const {
54+ checked,
55+ data,
56+ datum,
4057 disabled,
4158 error,
59+ id,
60+ index,
61+ label,
62+ name,
63+ onChange,
4264 readOnly,
43- checked,
65+ renderer : Renderer ,
66+ rendererClassName,
67+ rendererParams,
4468 } = this . props ;
4569
46- const classNames = [
70+ const classNames = _cs (
4771 'segment-option' ,
4872 styles . segmentOption ,
49- ] ;
50-
51- if ( checked ) {
52- classNames . push ( 'checked' ) ;
53- classNames . push ( styles . checked ) ;
54- }
55- if ( readOnly ) {
56- classNames . push ( 'read-only' ) ;
57- classNames . push ( styles . readOnly ) ;
58- }
59- if ( disabled ) {
60- classNames . push ( 'disabled' ) ;
61- classNames . push ( styles . disabled ) ;
62- }
63- if ( ! isFalsy ( error , [ '' ] ) ) {
64- classNames . push ( 'error' ) ;
65- classNames . push ( styles . error ) ;
66- }
67- return classNames . join ( ' ' ) ;
68- }
69-
70- render ( ) {
71- const {
72- id,
73- onChange,
74- checked,
75- label,
76- name,
77- } = this . props ;
73+ checked && 'checked' ,
74+ checked && styles . checked ,
75+ readOnly && 'read-only' ,
76+ readOnly && styles . readOnly ,
77+ disabled && 'disabled' ,
78+ disabled && styles . disabled ,
79+ ! isFalsy ( error , [ '' ] ) && 'error' ,
80+ ! isFalsy ( error , [ '' ] ) && styles . error ,
81+ ) ;
7882
79- const classNames = this . getClassName ( ) ;
83+ const extraProps = rendererParams ? rendererParams ( id , datum , index , data ) : undefined ;
8084
8185 return (
8286 < label
8387 htmlFor = { this . inputId }
8488 className = { classNames }
8589 >
8690 < input
87- className = { ` ${ styles . segmentButtonInput } segment-option-input` }
91+ className = { _cs ( styles . segmentButtonInput , ' segment-option-input' ) }
8892 type = "radio"
8993 onChange = { onChange }
9094 checked = { checked }
9195 id = { this . inputId }
9296 value = { id }
9397 name = { name }
9498 />
95- { label }
99+ { Renderer ? (
100+ < Renderer
101+ className = { rendererClassName }
102+ { ...extraProps }
103+ />
104+ ) : (
105+ label
106+ ) }
96107 </ label >
97108 ) ;
98109 }
0 commit comments