Skip to content

Commit

Permalink
See pewresearch/pewresearch-org@1f2d3bf from refs/heads/release/5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
prcdevgitbot committed Mar 15, 2024
1 parent ac06970 commit cbb4166
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 24 deletions.
22 changes: 16 additions & 6 deletions blocks/form-input-select/build/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@
'isSelected' => false,
),
);
$input_label = null;
if ( null !== $input_value ) {
foreach ( $input_options as $option ) {
if ( $option['value'] === $input_value ) {
$input_label = $option['label'];
break;
}
}

}
$input_disabled = array_key_exists( 'disabled', $attributes ) ? $attributes['disabled'] : false;
$input_options = empty($input_options) && array_key_exists( 'prc-block/form-input-options', $block->context) ? $block->context['prc-block/form-input-options'] : $input_options;

$input_options = array_map( function( $option ) use ( $input_value ) {
$option['isSelected'] = $option['value'] === $input_value;
return $option;
}, $input_options );
// $input_options = array_map( function( $option ) use ( $input_value ) {
// $option['isSelected'] = $option['value'] === $input_value;
// return $option;
// }, $input_options );
$input_id = md5( $target_namespace . $input_name );

$input_attrs = \PRC\Platform\Block_Utils\get_block_html_attributes( array(
Expand Down Expand Up @@ -67,7 +77,7 @@
'targetNamespace' => $target_namespace,
'activeIndex' => 0,
'value' => $input_value,
'label' => null,
'label' => $input_label,
'isOpen' => false,
'isHidden' => false,
'isDisabled' => $input_disabled,
Expand All @@ -84,7 +94,7 @@
'data-wp-class--is-error' => 'context.isError',
'data-wp-class--is-success' => 'context.isSuccess',
'data-wp-class--is-processing' => 'context.isProcessing',
'data-wp-watch--on-value-change' => 'callbacks.onValueChange',
// 'data-wp-watch--on-value-change' => 'callbacks.onValueChange',
'style' => '--block-gap:' . \PRC\Platform\Block_Utils\get_block_gap_support_value($attributes, 'horizontal') . ';',
) );

Expand Down
2 changes: 1 addition & 1 deletion blocks/form-input-select/build/view.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('@wordpress/interactivity', 'wp-polyfill'), 'version' => '6e8ad0a33bdcbbc9865e', 'type' => 'module');
<?php return array('dependencies' => array('@wordpress/interactivity', 'wp-polyfill'), 'version' => '81104a98711243b90a41', 'type' => 'module');
2 changes: 1 addition & 1 deletion blocks/form-input-select/build/view.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion blocks/form-input-select/build/view.js.map

Large diffs are not rendered by default.

21 changes: 15 additions & 6 deletions blocks/form-input-select/src/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@
'isSelected' => false,
),
);
$input_label = null;
if ( null !== $input_value ) {
foreach ( $input_options as $option ) {
if ( $option['value'] === $input_value ) {
$input_label = $option['label'];
break;
}
}

}
$input_disabled = array_key_exists( 'disabled', $attributes ) ? $attributes['disabled'] : false;
$input_options = empty($input_options) && array_key_exists( 'prc-block/form-input-options', $block->context) ? $block->context['prc-block/form-input-options'] : $input_options;

$input_options = array_map( function( $option ) use ( $input_value ) {
$option['isSelected'] = $option['value'] === $input_value;
return $option;
}, $input_options );
// $input_options = array_map( function( $option ) use ( $input_value ) {
// $option['isSelected'] = $option['value'] === $input_value;
// return $option;
// }, $input_options );
$input_id = md5( $target_namespace . $input_name );

$input_attrs = \PRC\Platform\Block_Utils\get_block_html_attributes( array(
Expand Down Expand Up @@ -67,7 +77,7 @@
'targetNamespace' => $target_namespace,
'activeIndex' => 0,
'value' => $input_value,
'label' => null,
'label' => $input_label,
'isOpen' => false,
'isHidden' => false,
'isDisabled' => $input_disabled,
Expand All @@ -84,7 +94,6 @@
'data-wp-class--is-error' => 'context.isError',
'data-wp-class--is-success' => 'context.isSuccess',
'data-wp-class--is-processing' => 'context.isProcessing',
'data-wp-watch--on-value-change' => 'callbacks.onValueChange',
'style' => '--block-gap:' . \PRC\Platform\Block_Utils\get_block_gap_support_value($attributes, 'horizontal') . ';',
) );

Expand Down
37 changes: 28 additions & 9 deletions blocks/form-input-select/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const { actions } = store('prc-block/form-input-select', {
highlightedOption,
});

context.value = highlightedOption?.value;
actions.setNewValue(highlightedOption?.value);
context.label = highlightedOption?.label;
context.isOpen = false;

Expand Down Expand Up @@ -165,7 +165,7 @@ const { actions } = store('prc-block/form-input-select', {

context.activeIndex = index;
context.label = label;
context.value = value;
actions.setNewValue(value);

console.log('form-input-select::onClick', {
context,
Expand All @@ -188,25 +188,44 @@ const { actions } = store('prc-block/form-input-select', {

actions.onClose();
},
},
callbacks: {
onValueChange: () => {
setNewValue: (newValue) => {
const { ref } = getElement();
const context = getContext();
const { targetNamespace, value } = context;
const { targetNamespace } = context;
// if the value is not empty and the targetNamespace is not the same as the current namespace
// then hoist the value up to the targetNamespace
if (value && 'prc-block/form-input-select' !== targetNamespace) {
context.value = newValue;
if (newValue && 'prc-block/form-input-select' !== targetNamespace) {
const { actions: targetActions } = store(targetNamespace);
if (targetActions.onSelectChange) {
console.log(
'form-input-select::onValueChange -> onSelectChange:',
context,
value
newValue
);
targetActions.onSelectChange(value, ref);
targetActions.onSelectChange(newValue, ref);
}
}
},
},
callbacks: {
// onValueChange: () => {
// const { ref } = getElement();
// const context = getContext();
// const { targetNamespace, value } = context;
// // if the value is not empty and the targetNamespace is not the same as the current namespace
// // then hoist the value up to the targetNamespace
// if (value && 'prc-block/form-input-select' !== targetNamespace) {
// const { actions: targetActions } = store(targetNamespace);
// if (targetActions.onSelectChange) {
// console.log(
// 'form-input-select::onValueChange -> onSelectChange:',
// context,
// value
// );
// targetActions.onSelectChange(value, ref);
// }
// }
// },
},
});

0 comments on commit cbb4166

Please sign in to comment.