SelectOneButton is used to choose a single item from a list using buttons. unselectable defines whether the selected button can be unselected again. The examples also show custom buttons that are defined in the custom facet or placed next to the component with layout="custom", a disabled item and the size attribute.
<style>
.custom-button {
display: inline-block;
padding: 0.5rem 1rem;
margin: 0.25rem;
border: 1px solid var(--p-content-border-color);
border-radius: 0.25rem;
background: var(--p-content-background);
color: var(--p-text-color);
cursor: pointer;
transition: all 0.2s;
}
.custom-button:hover {
background: var(--p-content-hover-background);
}
.custom-button.p-state-active {
background: var(--p-primary-color);
color: var(--p-primary-contrast-color);
border-color: var(--p-primary-color);
}
.legend {
display: inline-block;
width: 1.5rem;
height: 1.5rem;
border-radius: 0.25rem;
vertical-align: sub;
margin-right: 0.5rem;
}
.payment-button {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 1.5rem 2rem;
margin: 0.5rem;
border: 2px solid;
border-radius: 0.5rem;
background: var(--p-content-background);
color: var(--p-text-color);
cursor: pointer;
transition: all 0.2s;
min-width: 200px;
min-height: 120px;
}
.payment-button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.payment-button.p-state-active {
background: var(--p-content-hover-background);
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.payment-button.paypal {
border-color: #0070ba;
}
.payment-button.paypal.p-state-active {
border-color: #0070ba;
background: color-mix(in srgb, #0070ba 14%, var(--p-content-background));
}
.payment-button.bitcoin {
border-color: #f7931a;
}
.payment-button.bitcoin.p-state-active {
border-color: #f7931a;
background: color-mix(in srgb, #f7931a 14%, var(--p-content-background));
}
.payment-button.creditcard {
border-color: #6c757d;
}
.payment-button.creditcard.p-state-active {
border-color: #6c757d;
background: color-mix(in srgb, #6c757d 14%, var(--p-content-background));
}
.payment-button.cash {
border-color: #28a745;
}
.payment-button.cash.p-state-active {
border-color: #28a745;
background: color-mix(in srgb, #28a745 14%, var(--p-content-background));
}
.payment-button.p-state-disabled {
opacity: 0.5;
cursor: not-allowed;
pointer-events: none;
}
.payment-icon {
font-size: 2.5rem;
margin-bottom: 0.75rem;
}
.payment-button.paypal .payment-icon {
color: #0070ba;
}
.payment-button.bitcoin .payment-icon {
color: #f7931a;
}
.payment-button.creditcard .payment-icon {
color: #6c757d;
}
.payment-button.cash .payment-icon {
color: #28a745;
}
.payment-title {
font-weight: bold;
font-size: 1.1rem;
margin-bottom: 0.25rem;
}
.payment-tagline {
font-size: 0.85rem;
color: var(--p-text-muted-color);
text-align: center;
}
.payment-button.p-state-active .payment-tagline {
color: var(--p-text-color);
}
</style>
<div class="card">
<h:form>
<p:growl showDetail="true" showSummary="true">
<p:autoUpdate />
</p:growl>
<h5 class="mt-0">Default</h5>
<p:selectOneButton value="#{selectOneButtonView.option}" unselectable="false">
<p:ajax event="change" listener="#{selectOneButtonView.onChange}" />
<f:selectItem itemLabel="Option1" itemValue="Option1"/>
<f:selectItem itemLabel="Option2" itemValue="Option2"/>
<f:selectItem itemLabel="Option3" itemValue="Option3"/>
</p:selectOneButton>
<h5>Unselectable</h5>
<p:selectOneButton value="#{selectOneButtonView.option2}" unselectable="true">
<p:ajax event="change" listener="#{selectOneButtonView.onChange}" />
<f:selectItem itemLabel="Option1" itemValue="Option1"/>
<f:selectItem itemLabel="Option2" itemValue="Option2"/>
<f:selectItem itemLabel="Option3" itemValue="Option3"/>
</p:selectOneButton>
<h5>Custom Layout (Facet)</h5>
<p:selectOneButton id="customButton" value="#{selectOneButtonView.color}">
<p:ajax event="change" listener="#{selectOneButtonView.onChange}" />
<f:selectItem itemLabel="Red" itemValue="Red"/>
<f:selectItem itemLabel="Green" itemValue="Green"/>
<f:selectItem itemLabel="Blue" itemValue="Blue"/>
<f:facet name="custom">
<div class="custom-button" role="radio" tabindex="0">
<span class="legend" style="background:red; display:inline-block; width:1rem; height:1rem; border-radius:0.25rem; vertical-align:middle; margin-right:0.5rem;"/> Red
</div>
<div class="custom-button" role="radio" tabindex="0">
<span class="legend" style="background:green; display:inline-block; width:1rem; height:1rem; border-radius:0.25rem; vertical-align:middle; margin-right:0.5rem;"/> Green
</div>
<p:badge icon="pi pi-bell" severity="info" iconPos="left">
<div class="custom-button" role="radio" tabindex="0">
<span class="legend" style="background:blue; display:inline-block; width:1rem; height:1rem; border-radius:0.25rem; vertical-align:middle; margin-right:0.5rem;"/> Blue
</div>
</p:badge>
</f:facet>
</p:selectOneButton>
<h5>Custom Layout (Referenced)</h5>
<div id="customButton2Container">
<p:selectOneButton id="customButton2" value="#{selectOneButtonView.paymentMethod}" layout="custom">
<p:ajax event="change" listener="#{selectOneButtonView.onChange}" />
<f:selectItem itemLabel="PayPal" itemValue="PayPal"/>
<f:selectItem itemLabel="Bitcoin" itemValue="Bitcoin"/>
<f:selectItem itemLabel="Credit Card" itemValue="Credit Card" itemDisabled="true"/>
<f:selectItem itemLabel="Cash" itemValue="Cash"/>
</p:selectOneButton>
<div class="flex flex-wrap justify-content-start mt-3">
<div class="payment-button paypal" role="radio" tabindex="0">
<i class="pi pi-paypal payment-icon"></i>
<div class="payment-title">PayPal</div>
<div class="payment-tagline">Secure online payment</div>
</div>
<div class="payment-button bitcoin" role="radio" tabindex="0">
<i class="pi pi-bitcoin payment-icon"></i>
<div class="payment-title">Bitcoin</div>
<div class="payment-tagline">Cryptocurrency payment</div>
</div>
<div class="payment-button creditcard p-state-disabled" role="radio" tabindex="-1">
<i class="pi pi-credit-card payment-icon"></i>
<div class="payment-title">Credit Card</div>
<div class="payment-tagline">Currently unavailable</div>
</div>
<div class="payment-button cash" role="radio" tabindex="0">
<i class="pi pi-money-bill payment-icon"></i>
<div class="payment-title">Cash</div>
<div class="payment-tagline">Pay on delivery</div>
</div>
</div>
</div>
<h5>Sizes</h5>
<p:selectOneButton id="smallButton" unselectable="false" size="small" styleClass="mr-2">
<f:selectItem itemLabel="Option1" itemValue="Option1"/>
<f:selectItem itemLabel="Option2" itemValue="Option2"/>
<f:selectItem itemLabel="Option3" itemValue="Option3"/>
</p:selectOneButton>
<p:selectOneButton id="normalButton" unselectable="false" styleClass="mr-2">
<f:selectItem itemLabel="Option1" itemValue="Option1"/>
<f:selectItem itemLabel="Option2" itemValue="Option2"/>
<f:selectItem itemLabel="Option3" itemValue="Option3"/>
</p:selectOneButton>
<p:selectOneButton id="largeButton" unselectable="false" size="large">
<f:selectItem itemLabel="Option1" itemValue="Option1"/>
<f:selectItem itemLabel="Option2" itemValue="Option2"/>
<f:selectItem itemLabel="Option3" itemValue="Option3"/>
</p:selectOneButton>
</h:form>
</div>