22
33import { Timer } from '@performant-software/shared-components' ;
44import React , { Component , type ComponentType } from 'react' ;
5- import { Button , Dropdown , Message } from 'semantic-ui-react' ;
5+ import {
6+ Button ,
7+ Dropdown ,
8+ Message ,
9+ type ButtonProps
10+ } from 'semantic-ui-react' ;
611import _ from 'underscore' ;
712import EditModal from './EditModal' ;
813import i18n from '../i18n/i18n' ;
@@ -16,6 +21,7 @@ type Option = {
1621} ;
1722
1823type Props = {
24+ buttons ?: Array < ButtonProps > ,
1925 className ?: string ,
2026 collectionName : string ,
2127 header ?: ComponentType < any > ,
@@ -45,6 +51,10 @@ type State = {
4551 value : ?number | ?string
4652} ;
4753
54+ const BUTTON_ADD = 'add' ;
55+ const BUTTON_CLEAR = 'clear' ;
56+ const BUTTON_EDIT = 'edit' ;
57+
4858const TIMEOUT = 500 ;
4959
5060class AssociatedDropdown extends Component < Props , State > {
@@ -243,13 +253,33 @@ class AssociatedDropdown extends Component<Props, State> {
243253 return null ;
244254 }
245255
256+ return this . renderButton ( BUTTON_ADD , {
257+ basic : true ,
258+ content : i18n . t ( 'Common.buttons.add' ) ,
259+ icon : 'plus' ,
260+ onClick : ( ) => this . setState ( { modalAdd : true } ) ,
261+ type : 'button'
262+ } ) ;
263+ }
264+
265+ /**
266+ * Renders the button with the passed name using the provided props.
267+ *
268+ * @param name
269+ * @param defaults
270+ *
271+ * @returns {JSX.Element|null }
272+ */
273+ renderButton ( name , defaults ) {
274+ const button = _ . findWhere ( this . props . buttons , { name } ) || { } ;
275+
276+ if ( button . accept && ! button . accept ( ) ) {
277+ return null ;
278+ }
279+
246280 return (
247281 < Button
248- basic
249- content = { i18n . t ( 'Common.buttons.add' ) }
250- icon = 'plus'
251- onClick = { ( ) => this . setState ( { modalAdd : true } ) }
252- type = 'button'
282+ { ..._ . defaults ( button , defaults ) }
253283 />
254284 ) ;
255285 }
@@ -264,15 +294,13 @@ class AssociatedDropdown extends Component<Props, State> {
264294 return null ;
265295 }
266296
267- return (
268- < Button
269- basic
270- content = { i18n . t ( 'Common.buttons.clear' ) }
271- icon = 'times'
272- onClick = { this . onClear . bind ( this ) }
273- type = 'button'
274- />
275- ) ;
297+ return this . renderButton ( BUTTON_CLEAR , {
298+ basic : true ,
299+ content : i18n . t ( 'Common.buttons.clear' ) ,
300+ icon : 'times' ,
301+ onClick : this . onClear . bind ( this ) ,
302+ type : 'button'
303+ } ) ;
276304 }
277305
278306 /**
@@ -285,15 +313,13 @@ class AssociatedDropdown extends Component<Props, State> {
285313 return null ;
286314 }
287315
288- return (
289- < Button
290- basic
291- content = { i18n . t ( 'Common.buttons.edit' ) }
292- icon = 'pencil'
293- onClick = { ( ) => this . setState ( { modalEdit : true } ) }
294- type = 'button'
295- />
296- ) ;
316+ return this . renderButton ( BUTTON_EDIT , {
317+ basic : true ,
318+ content : i18n . t ( 'Common.buttons.edit' ) ,
319+ icon : 'pencil' ,
320+ onClick : ( ) => this . setState ( { modalEdit : true } ) ,
321+ type : 'button'
322+ } ) ;
297323 }
298324
299325 /**
0 commit comments