我正试图在管理仪表板的功能图像组件下面添加一个范围控件,我需要从帖子中访问该值,所以我将其保存在post meta中。这一切都很好,但我不知道如何为范围控制设置非零默认值。这是我的代码,首先是组件:
class RangeControlCustom extends React.Component {
render() {
const { meta, updateBackgroundPosition } = this.props;
return (
el(
window.wp.components.RangeControl,
{
label: \'Set image position\',
help: "Choose where the image is to be aligned within it\'s container. Lower numbers set the top of the image lower, higher numbers set the top of the image higher.",
value: ( meta.banner_image_position !== \'\' && meta.banner_image_position !== null ) ? meta.banner_image_position : 50,
min: 0,
max: 100,
onChange:
( value ) => {
this.setState( { value: value } );
updateBackgroundPosition( value, meta );
}
}
)
);
}
}
现在,包含在功能图像控件下的合成控件:
const composedRangeControl = wp.compose.compose( [
withState( ( value ) => value ),
withSelect( ( select ) => {
const currentMeta = select( \'core/editor\' ).getCurrentPostAttribute( \'meta\' );
const editedMeta = select( \'core/editor\' ).getEditedPostAttribute( \'meta\' );
return {
meta: { ...currentMeta, ...editedMeta },
};
} ),
withDispatch( ( dispatch ) => ( {
updateBackgroundPosition( value, meta ) {
meta = {
...meta,
banner_image_position: value,
};
dispatch( \'core/editor\' ).editPost( { meta } );
},
} ) ),
] )( RangeControlCustom );
现在,如果我将组件的值更改为:
value: meta.banner_image_position ? meta.banner_image_position : 50
。。。默认值是正确的,但如果滑块移到零,它将跳到50。我从逻辑的角度理解为什么会这样,但这给我留下了同样的问题。。。如何将默认值设置为零以外的值?