Skip to content

Commit

Permalink
fix: add null check in 'onChange' property of 'Select'
Browse files Browse the repository at this point in the history
  • Loading branch information
pirosiki197 committed Sep 2, 2024
1 parent 3646987 commit 860350e
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions dashboard/src/components/templates/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ type SelectProps<T extends string | number> = {

export type SingleSelectProps<T extends string | number> = SelectProps<T> & {
value: T | undefined
setValue?: (v: T) => void
setValue?: (v: T | undefined) => void
}

export const SingleSelect = <T extends string | number>(props: SingleSelectProps<T>): JSX.Element => {
Expand All @@ -176,16 +176,12 @@ export const SingleSelect = <T extends string | number>(props: SingleSelectProps

// const selectedOption = () => props.options.find((o) => o.value === props.value)
const [selectedOption, setSelectedOption] = createSignal<SelectOption<T>>()
// KobalteのSelect/Comboboxではundefinedを使用できないため、空文字列を指定している
const defaultOption = { label: '', value: '' as T }

createEffect(() => {
const found = props.options.find((o) => o.value === props.value)
// KobalteのSelect/Comboboxではundefinedを使用できないため、空文字列を指定している
setSelectedOption(
found ?? {
label: '',
value: '' as T,
},
)
setSelectedOption(found ?? defaultOption)
})

return (
Expand All @@ -196,8 +192,8 @@ export const SingleSelect = <T extends string | number>(props: SingleSelectProps
disallowEmptySelection
value={selectedOption()}
onChange={(v) => {
props.setValue?.(v.value)
setSelectedOption(v)
props.setValue?.(v?.value)
setSelectedOption(v ?? defaultOption)
}}
optionValue="value"
optionTextValue="label"
Expand Down Expand Up @@ -372,16 +368,12 @@ export const ComboBox = <T extends string | number>(props: ComboBoxProps<T>): JS
)

const [selectedOption, setSelectedOption] = createSignal<SelectOption<T>>()
// KobalteのSelect/Comboboxではundefinedを使用できないため、空文字列を指定している
const defaultOption = { label: '', value: '' as T }

createEffect(() => {
const found = props.options.find((o) => o.value === props.value)
// KobalteのSelect/Comboboxではundefinedを使用できないため、空文字列を指定している
setSelectedOption(
found ?? {
label: '',
value: '' as T,
},
)
setSelectedOption(found ?? defaultOption)
})

return (
Expand All @@ -391,8 +383,8 @@ export const ComboBox = <T extends string | number>(props: ComboBoxProps<T>): JS
allowDuplicateSelectionEvents
value={selectedOption()}
onChange={(v) => {
props.setValue?.(v.value)
setSelectedOption(v)
props.setValue?.(v?.value)
setSelectedOption(v ?? defaultOption)
}}
optionValue="value"
optionTextValue="label"
Expand Down

0 comments on commit 860350e

Please sign in to comment.