-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathtemplate.js
More file actions
67 lines (61 loc) · 1.64 KB
/
template.js
File metadata and controls
67 lines (61 loc) · 1.64 KB
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
/**
* External dependencies
*/
import { connect } from 'react-redux';
import { isEmpty, map } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { withContext, withInstanceId } from '@wordpress/components';
import { compose } from '@wordpress/element';
/**
* Internal dependencies
*/
import './style.scss';
import { getEditedPostAttribute } from '../../store/selectors';
import { editPost } from '../../store/actions';
export function PageTemplate( { availableTemplates, selectedTemplate, instanceId, onUpdate } ) {
if ( isEmpty( availableTemplates ) ) {
return null;
}
const selectId = `template-selector-${ instanceId }`;
const onEventUpdate = ( event ) => onUpdate( event.target.value );
return (
<div className="editor-page-attributes__template">
<label htmlFor={ selectId }>{ __( 'Template:' ) }</label>
<select
id={ selectId }
value={ selectedTemplate }
onBlur={ onEventUpdate }
onChange={ onEventUpdate }
>
{ map( { '': __( 'Default template' ), ...availableTemplates }, ( templateName, templateSlug ) => (
<option key={ templateSlug } value={ templateSlug }>{ templateName }</option>
) ) }
</select>
</div>
);
}
const applyConnect = connect(
( state ) => {
return {
selectedTemplate: getEditedPostAttribute( state, 'template' ),
};
},
{
onUpdate( templateSlug ) {
return editPost( { template: templateSlug || '' } );
},
}
);
const applyWithContext = withContext( 'editor' )(
( settings ) => ( {
availableTemplates: settings.availableTemplates,
} )
);
export default compose(
applyConnect,
applyWithContext,
withInstanceId,
)( PageTemplate );