mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
✨ Create a much cleaner “SelectBox” alternative
This commit is contained in:
parent
34b8e2de90
commit
e3bbd73e66
2 changed files with 101 additions and 0 deletions
|
@ -0,0 +1,100 @@
|
|||
import classNames from 'classnames';
|
||||
import { PayPalCheckbox, PayPalRdb } from './index';
|
||||
|
||||
const OptionSelector = ( {
|
||||
multiSelect = false,
|
||||
options,
|
||||
value,
|
||||
onChange,
|
||||
} ) => (
|
||||
<div className="ppcp-r-select-box-wrapper">
|
||||
{ options.map(
|
||||
( { value: itemValue, title, description, contents } ) => {
|
||||
let isSelected;
|
||||
|
||||
if ( Array.isArray( value ) ) {
|
||||
isSelected = value.includes( itemValue );
|
||||
} else {
|
||||
isSelected = value === itemValue;
|
||||
}
|
||||
|
||||
return (
|
||||
<OptionItem
|
||||
key={ itemValue }
|
||||
itemTitle={ title }
|
||||
itemDescription={ description }
|
||||
itemValue={ itemValue }
|
||||
onChange={ onChange }
|
||||
isMulti={ multiSelect }
|
||||
isSelected={ isSelected }
|
||||
>
|
||||
{ contents }
|
||||
</OptionItem>
|
||||
);
|
||||
}
|
||||
) }
|
||||
</div>
|
||||
);
|
||||
|
||||
export default OptionSelector;
|
||||
|
||||
const OptionItem = ( {
|
||||
itemTitle,
|
||||
itemDescription,
|
||||
itemValue,
|
||||
onChange,
|
||||
isMulti,
|
||||
isSelected,
|
||||
children,
|
||||
} ) => {
|
||||
const boxClassName = classNames( 'ppcp-r-select-box', {
|
||||
'ppcp--selected': isSelected,
|
||||
} );
|
||||
|
||||
return (
|
||||
<div className={ boxClassName }>
|
||||
<InputField
|
||||
value={ itemValue }
|
||||
isRadio={ ! isMulti }
|
||||
onChange={ onChange }
|
||||
isSelected={ isSelected }
|
||||
/>
|
||||
|
||||
<div className="ppcp-r-select-box__content">
|
||||
<div className="ppcp-r-select-box__content-inner">
|
||||
<span className="ppcp-r-select-box__title">
|
||||
{ itemTitle }
|
||||
</span>
|
||||
<p className="ppcp-r-select-box__description">
|
||||
{ itemDescription }
|
||||
</p>
|
||||
{ children && (
|
||||
<div className="ppcp-r-select-box__additional-content">
|
||||
{ children }
|
||||
</div>
|
||||
) }
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const InputField = ( { value, onChange, isRadio, isSelected } ) => {
|
||||
if ( isRadio ) {
|
||||
return (
|
||||
<PayPalRdb
|
||||
value={ value }
|
||||
handleRdbState={ onChange }
|
||||
checked={ isSelected }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<PayPalCheckbox
|
||||
value={ value }
|
||||
changeCallback={ onChange }
|
||||
checked={ isSelected }
|
||||
/>
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue