Skip to content

Radio

A radio input.

Examples

Peace Love Unity Respect
Value: "P"

Preact

jsx
const [checked, setChecked] = useState("P");

return (
  <>
    <Radio name="plur" checked={checked} value="P" onChange={(e) => setChecked(e.currentTarget.value)}>
      Peace
    </Radio>
    <Radio name="plur" checked={checked} value="L" onChange={(e) => setChecked(e.currentTarget.value)}>
      Love
    </Radio>
    <Radio name="plur" checked={checked} value="U" onChange={(e) => setChecked(e.currentTarget.value)}>
      Unity
    </Radio>
    <Radio name="plur" checked={checked} value="R" onChange={(e) => setChecked(e.currentTarget.value)}>
      Respect
    </Radio>
    <div style="margin-top: 0.5em">
      Value: {JSON.stringify(checked)}
    </div>
  </>
)

Vue.js x Custom Element

html
<script setup>
import { ref } from "vue";
const checked = ref("P");
</script>

<phi-group>
  <phi-radio name="plur" value="P" :checked="checked" @change="checked = $event.target.checked">
    Peace
  </phi-radio>
  <phi-radio name="plur" value="L" :checked="checked" @change="checked = $event.target.checked">
    Love
  </phi-radio>
  <phi-radio name="plur" value="U" :checked="checked" @change="checked = $event.target.checked">
    Unity
  </phi-radio>
  <phi-radio name="plur" value="R" :checked="checked" @change="checked = $event.target.checked">
    Respect
  </phi-radio>
</phi-group>
<div style="margin-top: 0.5em">
  Value: {{JSON.stringify(checked)}}
</div>

Pure-CSS

html
<label class="phi-radio">
  <input class="input" name="plur" type="radio" value="P" checked />
  Peace
</label>
<label class="phi-radio">
  <input class="input" name="plur" type="radio" value="L" />
  Love
</label>
<label class="phi-radio">
  <input class="input" name="plur" type="radio" value="U" />
  Unity
</label>
<label class="phi-radio">
  <input class="input" name="plur" type="radio" value="R" />
  Respect
</label>
<div id="value" style="margin-top: 0.5em">
  Value: "P"
</div>

<script>
function onChange (e) {
  if (e.currentTarget.checked) {
    document.getElementById("value").innerHTML = `Value: "${e.currentTarget.value}"`;
  }
}
document.getElementsByName("name").forEach(el => {
  el.addEventListener("change", onChange);
});
</script>

Reference

Preact

Properties

PropertyTypeDefaultDescription
checkedT(required)Value of selected item
valueT(required)Value of THIS option
onChangeT => voidemptyA handler function called on value change
childrenComponentChildrenemptyLabel text

Custom Element

Props / Attrs

SlotCategoryTypeDefault
checkedAttrstring""
valueAttrstring""
checkedPropT""
valuePropT""

Events

Following events are confirmed to work as expected. Some other events may also work.

EventDescription
changeEmitted on value change

Pure-CSS

Classes

Class
phi-radio, inputCore styles