Created
January 18, 2021 06:04
-
-
Save BenBroide/dd685fbb363709084a961b74436d3849 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {withSelect, select, withDispatch, useSelect} = wp.data | |
const {TextControl} = wp.components | |
const ControlField = withSelect( | |
(select, props) => { | |
const { label, meta_key} = props.field; | |
const {row_index,property_key} = props | |
const value = select('core/editor').getEditedPostAttribute('meta')[meta_key]; | |
const key = meta_key + row_index + property_key; | |
if( typeof row_index === 'undefined' ) { | |
return {value, key, label: `Set ${label}`}; | |
} | |
return { | |
value: value[row_index][property_key], | |
key, | |
label: `Set ${property_key.replace('_', ' ')}` | |
}; | |
} | |
)(TextControl); | |
export default withDispatch( | |
(dispatch, props) => { | |
const {meta_key} = props.field; | |
const {row_index,property_key} = props | |
return { | |
onChange: (value) => { | |
let newValue = value; | |
if(typeof row_index !== 'undefined') { | |
let repeaterValues = select('core/editor').getEditedPostAttribute('meta')?.[meta_key] | |
newValue = repeaterValues.map((row, innerIndex) => { | |
return innerIndex === row_index ? {...row, [property_key]: value} : row | |
}); | |
} | |
dispatch('core/editor').editPost({meta: {[meta_key]: newValue}}); | |
} | |
} | |
} | |
)(ControlField); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment