Updates to Select component

Use `value` instead of `defaultValue` and properly control it
This commit is contained in:
Justin Edmund 2022-12-23 15:19:14 -08:00
parent b04965ae03
commit b329d2b27a
6 changed files with 84 additions and 102 deletions

View file

@ -32,11 +32,6 @@ const FilterBar = (props: Props) => {
const [recencyOpen, setRecencyOpen] = useState(false) const [recencyOpen, setRecencyOpen] = useState(false)
const [elementOpen, setElementOpen] = useState(false) const [elementOpen, setElementOpen] = useState(false)
// Set up refs for filter dropdowns
const elementSelect = React.createRef<HTMLSelectElement>()
const raidSelect = React.createRef<HTMLSelectElement>()
const recencySelect = React.createRef<HTMLSelectElement>()
// Set up classes object for showing shadow on scroll // Set up classes object for showing shadow on scroll
const classes = classNames({ const classes = classNames({
FilterBar: true, FilterBar: true,
@ -69,9 +64,9 @@ const FilterBar = (props: Props) => {
<div className={classes}> <div className={classes}>
{props.children} {props.children}
<Select <Select
defaultValue={`${props.element}`} value={`${props.element}`}
open={elementOpen} open={elementOpen}
onChange={elementSelectChanged} onValueChange={elementSelectChanged}
onClick={openElementSelect} onClick={openElementSelect}
> >
<SelectItem data-element="all" key={-1} value={-1}> <SelectItem data-element="all" key={-1} value={-1}>
@ -105,14 +100,13 @@ const FilterBar = (props: Props) => {
defaultRaid="all" defaultRaid="all"
showAllRaidsOption={true} showAllRaidsOption={true}
onChange={raidSelectChanged} onChange={raidSelectChanged}
ref={raidSelect}
/> />
<Select <Select
defaultValue={`${props.recency}`} value={`${props.recency}`}
trigger={'All time'} trigger={'All time'}
open={recencyOpen} open={recencyOpen}
onChange={recencySelectChanged} onValueChange={recencySelectChanged}
onClick={openRecencySelect} onClick={openRecencySelect}
> >
<SelectItem key={-1} value={-1}> <SelectItem key={-1} value={-1}>

View file

@ -102,11 +102,11 @@ const JobDropdown = React.forwardRef<HTMLSelectElement, Props>(
return ( return (
<Select <Select
defaultValue={props.currentJob} value={currentJob ? currentJob.id : 'no-job'}
placeholder={'Select a class...'} placeholder={'Select a class...'}
open={open} open={open}
onClick={openJobSelect} onClick={openJobSelect}
onChange={handleChange} onValueChange={handleChange}
triggerClass="Job" triggerClass="Job"
> >
<SelectItem key={-1} value="no-job"> <SelectItem key={-1} value="no-job">

View file

@ -40,11 +40,11 @@ const JobSkillSearchFilterBar = (props: Props) => {
return ( return (
<div className="SearchFilterBar"> <div className="SearchFilterBar">
<Select <Select
defaultValue="-1" value={-1}
triggerClass="Bound" triggerClass="Bound"
trigger={'All elements'} trigger={'All elements'}
open={open} open={open}
onChange={onChange} onValueChange={onChange}
onClick={openSelect} onClick={openSelect}
> >
<SelectItem key="all" value={-1}> <SelectItem key="all" value={-1}>

View file

@ -25,18 +25,6 @@ interface Props {
} }
const Party = (props: Props) => { const Party = (props: Props) => {
// Cookies
const cookie = getCookie('account')
const accountData: AccountCookie = cookie
? JSON.parse(cookie as string)
: null
const headers = useMemo(() => {
return accountData
? { headers: { Authorization: `Bearer ${accountData.token}` } }
: {}
}, [accountData])
// Set up router // Set up router
const router = useRouter() const router = useRouter()
@ -55,12 +43,13 @@ const Party = (props: Props) => {
async function createParty(extra: boolean = false) { async function createParty(extra: boolean = false) {
let body = { let body = {
party: { party: {
...(accountData && { user_id: accountData.userId }),
extra: extra, extra: extra,
}, },
} }
return await api.endpoints.parties.create(body, headers) console.log(body)
return await api.endpoints.parties.create(body)
} }
// Methods: Updating the party's details // Methods: Updating the party's details
@ -68,13 +57,9 @@ const Party = (props: Props) => {
appState.party.extra = event.target.checked appState.party.extra = event.target.checked
if (party.id) { if (party.id) {
api.endpoints.parties.update( api.endpoints.parties.update(party.id, {
party.id, party: { extra: event.target.checked },
{ })
party: { extra: event.target.checked },
},
headers
)
} }
} }
@ -86,17 +71,13 @@ const Party = (props: Props) => {
) { ) {
if (appState.party.id) if (appState.party.id)
api.endpoints.parties api.endpoints.parties
.update( .update(appState.party.id, {
appState.party.id, party: {
{ name: name,
party: { description: description,
name: name, raid_id: raid?.id,
description: description,
raid_id: raid?.id,
},
}, },
headers })
)
.then(() => { .then(() => {
appState.party.name = name appState.party.name = name
appState.party.description = description appState.party.description = description
@ -110,7 +91,7 @@ const Party = (props: Props) => {
function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) { function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
if (appState.party.editable && appState.party.id) { if (appState.party.editable && appState.party.id) {
api.endpoints.parties api.endpoints.parties
.destroy({ id: appState.party.id, params: headers }) .destroy({ id: appState.party.id })
.then(() => { .then(() => {
// Push to route // Push to route
router.push('/') router.push('/')
@ -131,26 +112,26 @@ const Party = (props: Props) => {
} }
// Methods: Storing party data // Methods: Storing party data
const storeParty = function (party: Party) { const storeParty = function (team: Party) {
// Store the important party and state-keeping values // Store the important party and state-keeping values
appState.party.name = party.name appState.party.name = team.name
appState.party.description = party.description appState.party.description = team.description
appState.party.raid = party.raid appState.party.raid = team.raid
appState.party.updated_at = party.updated_at appState.party.updated_at = team.updated_at
appState.party.job = party.job appState.party.job = team.job
appState.party.jobSkills = party.job_skills appState.party.jobSkills = team.job_skills
appState.party.id = party.id appState.party.id = team.id
appState.party.extra = party.extra appState.party.extra = team.extra
appState.party.user = party.user appState.party.user = team.user
appState.party.favorited = party.favorited appState.party.favorited = team.favorited
appState.party.created_at = party.created_at appState.party.created_at = team.created_at
appState.party.updated_at = party.updated_at appState.party.updated_at = team.updated_at
// Populate state // Populate state
storeCharacters(party.characters) storeCharacters(team.characters)
storeWeapons(party.weapons) storeWeapons(team.weapons)
storeSummons(party.summons) storeSummons(team.summons)
} }
const storeCharacters = (list: Array<GridCharacter>) => { const storeCharacters = (list: Array<GridCharacter>) => {
@ -256,13 +237,11 @@ const Party = (props: Props) => {
<React.Fragment> <React.Fragment>
{navigation} {navigation}
<section id="Party">{currentGrid()}</section> <section id="Party">{currentGrid()}</section>
{ <PartyDetails
<PartyDetails editable={party.editable}
editable={party.editable} updateCallback={updateDetails}
updateCallback={updateDetails} deleteCallback={deleteTeam}
deleteCallback={deleteTeam} />
/>
}
</React.Fragment> </React.Fragment>
) )
} }

View file

@ -42,7 +42,7 @@ const RaidDropdown = React.forwardRef<HTMLSelectElement, Props>(
// Set up local states for storing raids // Set up local states for storing raids
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const [currentRaid, setCurrentRaid] = useState<Raid>() const [currentRaid, setCurrentRaid] = useState<Raid | undefined>(undefined)
const [raids, setRaids] = useState<Raid[]>() const [raids, setRaids] = useState<Raid[]>()
const [sortedRaids, setSortedRaids] = useState<Raid[][]>() const [sortedRaids, setSortedRaids] = useState<Raid[][]>()
@ -78,7 +78,7 @@ const RaidDropdown = React.forwardRef<HTMLSelectElement, Props>(
useEffect(() => { useEffect(() => {
if (raids && props.currentRaid) { if (raids && props.currentRaid) {
const raid = raids.find((raid) => raid.slug === props.currentRaid) const raid = raids.find((raid) => raid.slug === props.currentRaid)
setCurrentRaid(raid) if (raid) setCurrentRaid(raid)
} }
}, [raids, props.currentRaid]) }, [raids, props.currentRaid])
@ -100,13 +100,12 @@ const RaidDropdown = React.forwardRef<HTMLSelectElement, Props>(
sortedRaids[index].length > 0 && sortedRaids[index].length > 0 &&
sortedRaids[index] sortedRaids[index]
.sort((a, b) => a.element - b.element) .sort((a, b) => a.element - b.element)
.map((item, i) => { .map((item, i) => (
return ( <SelectItem key={i} value={item.slug}>
<SelectItem key={i} value={item.slug}> {item.name[locale]}
{item.name[locale]} </SelectItem>
</SelectItem> ))
)
})
return ( return (
<SelectGroup <SelectGroup
key={index} key={index}
@ -119,17 +118,19 @@ const RaidDropdown = React.forwardRef<HTMLSelectElement, Props>(
} }
return ( return (
<Select <React.Fragment>
defaultValue={props.showAllRaidsOption ? props.currentRaid : undefined} <Select
placeholder={'Select a raid...'} value={props.currentRaid}
open={open} placeholder={'Select a raid...'}
onClick={openRaidSelect} open={open}
onChange={handleChange} onClick={openRaidSelect}
> onValueChange={handleChange}
{Array.from(Array(sortedRaids?.length)).map((x, i) => >
renderRaidGroup(i) {Array.from(Array(sortedRaids?.length)).map((x, i) =>
)} renderRaidGroup(i)
</Select> )}
</Select>
</React.Fragment>
) )
} }
) )

View file

@ -1,4 +1,4 @@
import React from 'react' import React, { useEffect, useState } from 'react'
import * as RadixSelect from '@radix-ui/react-select' import * as RadixSelect from '@radix-ui/react-select'
import classNames from 'classnames' import classNames from 'classnames'
@ -7,26 +7,34 @@ import ArrowIcon from '~public/icons/Arrow.svg'
import './index.scss' import './index.scss'
// Props // Props
interface Props { interface Props
extends React.DetailedHTMLProps<
React.SelectHTMLAttributes<HTMLSelectElement>,
HTMLSelectElement
> {
open: boolean open: boolean
defaultValue?: string | number
placeholder?: string placeholder?: string
trigger?: React.ReactNode trigger?: React.ReactNode
children?: React.ReactNode children?: React.ReactNode
onClick?: () => void onClick?: () => void
onChange?: (value: string) => void onValueChange?: (value: string) => void
triggerClass?: string triggerClass?: string
} }
const Select = React.forwardRef<HTMLSelectElement, Props>(function useFieldSet( const Select = (props: Props) => {
props, const [value, setValue] = useState('')
ref
) { useEffect(() => {
if (props.value) setValue(`${props.value}`)
}, [props.value])
function onValueChange(newValue: string) {
setValue(`${newValue}`)
if (props.onValueChange) props.onValueChange(newValue)
}
return ( return (
<RadixSelect.Root <RadixSelect.Root value={value} onValueChange={onValueChange}>
defaultValue={props.defaultValue as string}
onValueChange={props.onChange}
>
<RadixSelect.Trigger <RadixSelect.Trigger
className={classNames('SelectTrigger', props.triggerClass)} className={classNames('SelectTrigger', props.triggerClass)}
placeholder={props.placeholder} placeholder={props.placeholder}
@ -50,6 +58,6 @@ const Select = React.forwardRef<HTMLSelectElement, Props>(function useFieldSet(
</RadixSelect.Portal> </RadixSelect.Portal>
</RadixSelect.Root> </RadixSelect.Root>
) )
}) }
export default Select export default Select