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

Various site and batch forms improvements #301

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export type SelectorOptionQuantity<TValue> = {
quantity?: number,
}

const MINIMUM_QUANTITY = 1 // Makes it easy to refactor if we support a min input
const DEFAULT_QUANTITY = 1

const OptionQuantitySelector = <TValue extends OptionQuantityValueType>(
{ id, label, options, selected, withQuantity, onChange }: Props<TValue>,
) => {
Expand All @@ -43,9 +46,10 @@ const OptionQuantitySelector = <TValue extends OptionQuantityValueType>(
optionQuantity.option.value === option.value
)

existingOptionIndex === -1
? onChange([...selected, { option, quantity: 1 }])
: updateQuantity(existingOptionIndex, 1)
// Do nothing if already selected
if (existingOptionIndex !== -1) return

onChange([...selected, { option, quantity: DEFAULT_QUANTITY }])
}

const updateQuantity = (
Expand All @@ -55,17 +59,16 @@ const OptionQuantitySelector = <TValue extends OptionQuantityValueType>(
) => {
const updatedOption = { ...selected[optionIndex] }
updatedOption.quantity = changeType === 'absolute'
? Math.max(quantity, 1)
: (updatedOption.quantity ?? 1) + quantity

? Math.max(quantity, MINIMUM_QUANTITY)
: (updatedOption.quantity ?? MINIMUM_QUANTITY) + quantity

// NOTE: Currently unusable since we always disable at minimum,
// kept in case we wanna restore that behaviour
// if (updatedOption.quantity <= 0) {
// removeOption(optionIndex)
// } else {
const updatedSelected = [...selected]

if (updatedOption.quantity <= 0) {
updatedSelected.splice(optionIndex, 1)
} else {
updatedSelected[optionIndex] = updatedOption
}

updatedSelected[optionIndex] = updatedOption
onChange(updatedSelected)
}

Expand All @@ -85,16 +88,16 @@ const OptionQuantitySelector = <TValue extends OptionQuantityValueType>(
autoSelect
clearOnBlur
freeSolo
getOptionKey={option => {
if (typeof (option) === 'string') return option

return option.value
}}
getOptionLabel={option => {
if (typeof (option) === 'string') return option

return option.displayText
}}
getOptionKey={option => (
typeof (option) === 'string'
? option
: option.value
)}
getOptionLabel={option => (
typeof (option) === 'string'
? option
: option.displayText
)}
id={id}
onChange={(_event, option) => {
if (option === null || typeof (option) === 'string') return
Expand Down Expand Up @@ -135,7 +138,7 @@ const OptionQuantitySelector = <TValue extends OptionQuantityValueType>(
<ul className='list-group list-group-flush overflow-hidden mt-1'>
{selected.map((optionQuantity, index) => (
<li
className='list-group-item row d-flex align-items-center justify-content-between'
className='list-group-item row d-flex justify-content-between align-items-center'
key={`selected-specie-${optionQuantity.option.value}`}
>
<div className='col-6'>{optionQuantity.option.displayText}</div>
Expand All @@ -145,6 +148,7 @@ const OptionQuantitySelector = <TValue extends OptionQuantityValueType>(
<div className='d-flex justify-content-center align-items-center'>
<button
className='btn btn-outline-dark btn-sm icon-button p-1'
disabled={optionQuantity.quantity === MINIMUM_QUANTITY}
onClick={() => updateQuantity(index, -1)}
type='button'
>
Expand All @@ -162,7 +166,7 @@ const OptionQuantitySelector = <TValue extends OptionQuantityValueType>(

<button
className='btn btn-outline-dark btn-sm icon-button p-1'
onClick={() => updateQuantity(index, 1)}
onClick={() => updateQuantity(index, +1)}
type='button'
>
<span className='material-symbols-outlined fill-icon icon-xs'>add</span>
Expand Down
Loading
Loading