A wrapper around n-radio-group that adds async option support and supports button-group style.
Specify options through options, and bind the value with v-model.
浏览器不支持,请手动复制 basic.en.vue < template>
< div>
{{ value }}
</ div>
< np-radio v-model = " value" :options = " ['Beijing', 'Shanghai', 'Guangzhou']" />
</ template>
< script lang = " ts" setup >
import { ref } from 'vue'
const value = ref< string | null > ( null )
</ script>
Set button to use the button-group style.
浏览器不支持,请手动复制 button-group.en.vue < template>
< div>
{{ value }}
</ div>
< np-radio
v-model = " value"
button
:options = " [
{ label: 'Shanghai', value: 'shanghai' },
{ label: 'Beijing', value: 'beijing' },
{ label: 'Shenzhen', value: 'shenzhen' },
]"
/>
</ template>
< script lang = " ts" setup >
import { ref } from 'vue'
const value = ref< string | null > ( 'shanghai' )
</ script>
The options prop supports an async function, e.g. for fetching dictionaries from a backend API.
浏览器不支持,请手动复制 async.en.vue < template>
< div>
{{ value }}
</ div>
< np-radio v-model = " value" :options = " options" />
</ template>
< script lang = " ts" setup >
import { ref } from 'vue'
const value = ref< string | null > ( null )
const options = async ( ) => {
await new Promise < any[ ] > ( resolve => setTimeout ( resolve, 2000 ) )
return [
{ label : 'Beijing' , value : 'beijing' } ,
{ label : 'Shanghai' , value : 'shanghai' } ,
{ label : 'Guangzhou' , value : 'guangzhou' } ,
]
}
</ script>
The options prop supports an async computed function, e.g. for fetching different dictionaries from different APIs based on the type.
浏览器不支持,请手动复制 computed.en.vue < template>
< div>
{{ value }}
</ div>
< div>
< np-button
:type = " type === 'Fujian' ? 'primary' : undefined"
@click = " () => (type = 'Fujian')"
>
Fujian
</ np-button>
< np-button
:type = " type === 'Guangdong' ? 'primary' : undefined"
@click = " () => (type = 'Guangdong')"
>
Guangdong
</ np-button>
</ div>
< np-radio v-model = " value" :options = " options" />
</ template>
< script lang = " ts" setup >
import { ref } from 'vue'
const value = ref< string | null > ( null )
const type = ref ( 'Fujian' )
const options = async ( ) => {
return type. value === 'Fujian'
? await new Promise < any[ ] > ( resolve => {
setTimeout (
( ) => resolve ( [ 'Fuzhou' , 'Xiamen' , 'Longyan' , 'Sanming' ] ) ,
1500
)
} )
: await new Promise < any[ ] > ( resolve => {
setTimeout ( ( ) => resolve ( [ 'Guangzhou' , 'Shenzhen' , 'Zhuhai' ] ) , 1500 )
} )
}
</ script>
Usually options are fetched from a backend, and the data structure is not always {label, value}. You can use label-field and value-field to specify option field names.
< template>
< div>
{{ value }}
</ div>
< np-radio
v-model = " value"
:options = " [
{ name: 'Beijing', id: 'beijing' },
{ name: 'Shanghai', id: 'shanghai' },
{ name: 'Guangzhou', id: 'guangzhou' },
]"
value-field = " id"
label-field = " name"
/>
</ template>
< script lang = " ts" setup >
import { ref } from 'vue'
const value = ref< string | null > ( null )
</ script>
Get the value change via the change event.
浏览器不支持,请手动复制 change.en.vue < template>
< div>
{{ value }}
</ div>
< np-radio
:options = " [
{ label: 'Beijing', value: 'beijing' },
{ label: 'Shanghai', value: 'shanghai' },
{ label: 'Guangzhou', value: 'guangzhou' },
]"
@change = " (val: any) => (value = val)"
/>
</ template>
< script lang = " ts" setup >
import { ref } from 'vue'
const value = ref< string | number | boolean | null > ( null )
</ script>
Prop Description Type Default v-model Selected value RadioGroupValue - options Options AsyncValue<RadioOption[]> - label-field Custom label field name string labelvalue-field Custom value field name string valuebutton Whether to use button-group style boolean falseloadingDelay Loading delay in ms, effective when options is async number 250 props NRadioGroup props NRadioGroup Propsopen in new window - onChange Change event (value: RadioGroupValue) => void-
Event Description Callback update:modelValue Value change event (value: RadioGroupValue) => voidchange Change event (value: RadioGroupValue) => void
The component exports the following type definitions:
import type { NpRadioProps } from 'naive-plus'