Skip to content

Instantly share code, notes, and snippets.

@BenBroide
Created January 18, 2021 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenBroide/dd685fbb363709084a961b74436d3849 to your computer and use it in GitHub Desktop.
Save BenBroide/dd685fbb363709084a961b74436d3849 to your computer and use it in GitHub Desktop.
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