1 | /** |
---|
2 | * External dependencies. |
---|
3 | */ |
---|
4 | import cx from 'classnames'; |
---|
5 | import { Component } from '@wordpress/element'; |
---|
6 | import { __ } from '@wordpress/i18n'; |
---|
7 | |
---|
8 | /** |
---|
9 | * Internal dependencies. |
---|
10 | */ |
---|
11 | import { getFieldType } from '../../registry/fields'; |
---|
12 | |
---|
13 | class ComplexGroup extends Component { |
---|
14 | /** |
---|
15 | * Handles the click on the "Toggle" button. |
---|
16 | * |
---|
17 | * @return {void} |
---|
18 | */ |
---|
19 | handleToggleClick = () => { |
---|
20 | const { |
---|
21 | id, |
---|
22 | onToggle |
---|
23 | } = this.props; |
---|
24 | |
---|
25 | onToggle( id ); |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Handles the click on the "Clone" button. |
---|
30 | * |
---|
31 | * @return {void} |
---|
32 | */ |
---|
33 | handleCloneClick = () => { |
---|
34 | const { |
---|
35 | id, |
---|
36 | onClone |
---|
37 | } = this.props; |
---|
38 | |
---|
39 | onClone( id ); |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Handles the click on the "Remove" button. |
---|
44 | * |
---|
45 | * @return {void} |
---|
46 | */ |
---|
47 | handleRemoveClick = () => { |
---|
48 | const { |
---|
49 | id, |
---|
50 | onRemove |
---|
51 | } = this.props; |
---|
52 | |
---|
53 | onRemove( id ); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Renders the component. |
---|
58 | * |
---|
59 | * @return {Object} |
---|
60 | */ |
---|
61 | render() { |
---|
62 | const { |
---|
63 | index, |
---|
64 | label, |
---|
65 | name, |
---|
66 | prefix, |
---|
67 | tabbed, |
---|
68 | hidden, |
---|
69 | dragged, |
---|
70 | collapsed, |
---|
71 | allowClone, |
---|
72 | fields, |
---|
73 | context, |
---|
74 | onFieldSetup |
---|
75 | } = this.props; |
---|
76 | |
---|
77 | const groupClasses = cx( |
---|
78 | 'cf-complex__group', |
---|
79 | { |
---|
80 | 'cf-complex__group--grid': ! tabbed, |
---|
81 | 'cf-complex__group--tabbed': tabbed, |
---|
82 | 'cf-complex__group--collapsed': collapsed, |
---|
83 | 'cf-complex__group--dragged': dragged |
---|
84 | } |
---|
85 | ); |
---|
86 | |
---|
87 | const toggleClasses = cx( |
---|
88 | 'dashicons-before', |
---|
89 | 'cf-complex__group-action-icon', |
---|
90 | { |
---|
91 | 'dashicons-arrow-up': ! collapsed, |
---|
92 | 'dashicons-arrow-down': collapsed |
---|
93 | } |
---|
94 | ); |
---|
95 | |
---|
96 | const actionsClasses = cx( |
---|
97 | 'cf-complex__group-actions', |
---|
98 | { |
---|
99 | 'cf-complex__group-actions--grid': ! tabbed, |
---|
100 | 'cf-complex__group-actions--tabbed': tabbed |
---|
101 | } |
---|
102 | ); |
---|
103 | |
---|
104 | return ( |
---|
105 | <div className={ groupClasses } hidden={ hidden }> |
---|
106 | { name && ( |
---|
107 | <input |
---|
108 | type="hidden" |
---|
109 | name={ `${ prefix }[value]` } |
---|
110 | value={ name } |
---|
111 | /> |
---|
112 | ) } |
---|
113 | |
---|
114 | { ! tabbed && ( |
---|
115 | <div className="cf-complex__group-head"> |
---|
116 | <span className="cf-complex__group-index"> |
---|
117 | { index + 1 } |
---|
118 | </span> |
---|
119 | |
---|
120 | <span className="cf-complex__group-title"> |
---|
121 | { label } |
---|
122 | </span> |
---|
123 | </div> |
---|
124 | ) } |
---|
125 | |
---|
126 | { ! dragged && ( |
---|
127 | <div className="cf-complex__group-body" hidden={ ! tabbed && collapsed }> |
---|
128 | { fields.map( ( field ) => { |
---|
129 | const FieldEdit = getFieldType( field.type, context ); |
---|
130 | |
---|
131 | if ( ! FieldEdit ) { |
---|
132 | return null; |
---|
133 | } |
---|
134 | |
---|
135 | const [ Field, props ] = onFieldSetup( field, {}, this.props ); |
---|
136 | |
---|
137 | return ( |
---|
138 | // The `key` will be assigned via `onFieldSetup`. |
---|
139 | // eslint-disable-next-line react/jsx-key |
---|
140 | <Field { ...props }> |
---|
141 | <FieldEdit { ...props } /> |
---|
142 | </Field> |
---|
143 | ); |
---|
144 | } ) } |
---|
145 | </div> |
---|
146 | ) } |
---|
147 | |
---|
148 | <div className={ actionsClasses }> |
---|
149 | { allowClone && ( |
---|
150 | <button type="button" title={ __( 'Duplicate', 'carbon-fields-ui' ) } className="cf-complex__group-action" onClick={ this.handleCloneClick }> |
---|
151 | <span className="dashicons-before dashicons-admin-page cf-complex__group-action-icon"></span> |
---|
152 | |
---|
153 | <span className="cf-complex__group-action-text"> |
---|
154 | { __( 'Duplicate', 'carbon-fields-ui' ) } |
---|
155 | </span> |
---|
156 | </button> |
---|
157 | ) } |
---|
158 | |
---|
159 | <button type="button" title={ __( 'Remove', 'carbon-fields-ui' ) } className="cf-complex__group-action" onClick={ this.handleRemoveClick }> |
---|
160 | <span className="dashicons-before dashicons-trash cf-complex__group-action-icon"></span> |
---|
161 | |
---|
162 | <span className="cf-complex__group-action-text"> |
---|
163 | { __( 'Remove', 'carbon-fields-ui' ) } |
---|
164 | </span> |
---|
165 | </button> |
---|
166 | |
---|
167 | { ! tabbed && ( |
---|
168 | <button type="button" title={ __( 'Collapse', 'carbon-fields-ui' ) } className="cf-complex__group-action" onClick={ this.handleToggleClick }> |
---|
169 | <span className={ toggleClasses }></span> |
---|
170 | |
---|
171 | <span className="cf-complex__group-action-text"> |
---|
172 | { __( 'Collapse', 'carbon-fields-ui' ) } |
---|
173 | </span> |
---|
174 | </button> |
---|
175 | ) } |
---|
176 | </div> |
---|
177 | </div> |
---|
178 | ); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | export default ComplexGroup; |
---|