Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(grid): [grid] fix can not select first option when value is empty #2756

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/vue/src/grid/src/adapter/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,32 @@ function getEvents(renderOpts, params, context) {
}

function renderOptions(h, options, renderOpts, params, context) {
let { optionProps = {} } = renderOpts
let labelProp = optionProps.label || 'label'
let valueProp = optionProps.value || 'value'
let { column, row } = params
let { formatConfig } = column.own
let cellValue = isSyncCell(renderOpts, params, context) ? getCellValue(row, column) : column.model.value
const { optionProps = {} } = renderOpts
const labelProp = optionProps.label || 'label'
const valueProp = optionProps.value || 'value'
const { column, row } = params
const { formatConfig } = column.own
const cellValue = isSyncCell(renderOpts, params, context) ? getCellValue(row, column) : column.model.value

if (!options && formatConfig && formatConfig.data) {
options = formatConfig.data
}

return options.map((item, index) => {
let attrs = {
domProps: { value: item[valueProp], selected: item.value === cellValue },
let hasSelected = false
const optionsList = options.map((item, index) => {
const selected = item.value === cellValue
if (selected) {
hasSelected = true
}
const attrs = {
domProps: { value: item[valueProp], selected },
key: index
}
return h('option', attrs, item[labelProp])
})
if (options.length && !hasSelected) {
optionsList.unshift(h('option', { style: 'display:none', selected: true }, ''))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A hidden default option is added to ensure that the first option can be selected when the value is empty. This change prevents the issue where no option is selected by default.

}
return optionsList
}

function renderOptgroups(h, options, params, context) {
Expand Down
Loading