Skip to content

Commit

Permalink
✨ Add support to infer types from itemGroups for Select
Browse files Browse the repository at this point in the history
  • Loading branch information
Frontendland committed Dec 9, 2024
1 parent c746064 commit f0b57f3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/components/Select/Select.astro
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ const {
...rest
} = Astro.props
const inputRestProps = Object.fromEntries(
Object.entries(rest).filter(([key]) => key.includes('data'))
)
const classes = classNames([
styles.select,
disabled && styles.disabled,
className
])
const inferredValue = rest.itemGroups.map(group => group.items)
.flat()
.find(item => item.value === value)?.name
const inputRestProps = Object.fromEntries(
Object.entries(rest).filter(([key]) => key.includes('data'))
)
---

<Input
type="text"
value={value}
value={(value && inferredValue) ? inferredValue : value}
readonly={true}
disabled={disabled}
placeholder={placeholder}
Expand Down
9 changes: 7 additions & 2 deletions src/components/Select/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import styles from './select.module.scss'
import type { ListEventType } from '../List/list'
import type { ListEventType, ListProps } from '../List/list'
export let name: SvelteSelectProps['name'] = ''
export let value: SvelteSelectProps['value'] = ''
Expand All @@ -42,6 +42,11 @@
styles.popover
])
const inferredValue = $$restProps.itemGroups
.map((group: ListProps['itemGroups'][0]) => group.items)
.flat()
.find((item: ListProps['itemGroups'][0]['items'][0]) => item.value === value)?.name
const inputRestProps = Object.fromEntries(
Object.entries($$restProps).filter(([key]) => key.includes('data'))
)
Expand Down Expand Up @@ -110,7 +115,7 @@

<Input
type="text"
value={value || null}
value={(value && inferredValue) ? inferredValue : value}
readonly={true}
disabled={disabled}
placeholder={placeholder || null}
Expand Down
6 changes: 5 additions & 1 deletion src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const Select = ({
onChange,
...rest
}: ReactSelectProps) => {
const [val, setValue] = useState(value)
const inferredValue = rest.itemGroups.map(group => group.items)
.flat()
.find(item => item.value === value)?.name

const [val, setValue] = useState((value && inferredValue) ? inferredValue : value)

const classes = classNames([
styles.select,
Expand Down
8 changes: 8 additions & 0 deletions src/pages/components/select.astro
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ const sections = getSections({
/>
</ComponentWrapper>

<ComponentWrapper title="With inferred value (from itemGroups)">
<section.component
name="with-inferred-value"
value="new"
itemGroups={listWithStates}
/>
</ComponentWrapper>

<ComponentWrapper title="Disabled">
<section.component
name="disabled"
Expand Down

0 comments on commit f0b57f3

Please sign in to comment.