-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: S2 SelectBox #8541
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
Merged
Merged
feat: S2 SelectBox #8541
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
e4d4c17
S2 SelectBox initial implementation
a431ce0
Added tests and fixed linting
DPandyan 7c6a91b
updated tests to remove styles
DPandyan b0a9b78
selectbox refactor and tests refactor
DPandyan 151e4f6
stories changes and various edits
DPandyan d64c17b
replaced aria components with a gridlist
DPandyan 23590e2
fixed borders and redid stories/tests
DPandyan 1a7e962
removed extraneous overrides and isEmphasized prop
DPandyan 29734bb
lint and removed XS
DPandyan cab2c79
test-utils-internal swap
DPandyan 2f1c99d
test-utils-internal swap
DPandyan 695706b
Merge branch 'main' of github.com:DPandyan/react-spectrum
DPandyan afa410e
Merge branch 'main' into main
DPandyan ea0f8d5
swapped useId library
DPandyan 19a536c
swapped to listbox
DPandyan a779329
added additional props and removed s2 checkbox
DPandyan eb96cba
revised tests, added additional props, swapped to UI checkmark to avo…
DPandyan 7ca7820
adjusted illustration size
DPandyan 60d2d3b
removed the sizing and fixed various alignment issues. replaced check…
DPandyan cd3299f
Merge branch 'main' into main
DPandyan bd3d6b7
fixed group disabled checkbox issue
DPandyan 85f7e21
pruned styles
DPandyan bdee23a
fixed label overflow, description still remaining
DPandyan 9ad81e6
moved selectbox into same file, reworked grid, addressed various gh c…
DPandyan 48c7e56
Merge branch 'main' into main
DPandyan 267cf7b
s2(SelectBoxGroup): center label in horizontal layout when only label…
DPandyan 4a6f681
s2(SelectBoxGroup): support [DEFAULT_SLOT] as label styles for text-o…
DPandyan c542032
removed extra story and count check
DPandyan 13e72b1
Merge branch 'main' into main
DPandyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/* | ||
* Copyright 2025 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import {Checkbox as AriaCheckbox, Radio as AriaRadio, CheckboxProps, ContextValue, RadioProps} from 'react-aria-components'; | ||
import {Checkbox} from './Checkbox'; | ||
import {FocusableRef, FocusableRefValue} from '@react-types/shared'; | ||
import {focusRing, style} from '../style' with {type: 'macro'}; | ||
import {getAllowedOverrides, StyleProps} from './style-utils' with {type: 'macro'}; | ||
import React, {createContext, forwardRef, ReactNode, useContext, useRef} from 'react'; | ||
import {SelectBoxContext} from './SelectBoxGroup'; | ||
import {useFocusableRef} from '@react-spectrum/utils'; | ||
import {useSpectrumContextProps} from './useSpectrumContextProps'; | ||
|
||
export interface SelectBoxProps extends | ||
Omit<CheckboxProps & RadioProps, 'className' | 'style' | 'children'>, StyleProps { | ||
/** | ||
* The value of the SelectBox. | ||
*/ | ||
value: string, | ||
/** | ||
* The label for the element. | ||
*/ | ||
children?: ReactNode, | ||
/** | ||
* Whether the SelectBox is disabled. | ||
*/ | ||
isDisabled?: boolean, | ||
/** | ||
* Whether the SelectBox is selected (controlled). | ||
*/ | ||
isSelected?: boolean, | ||
/** | ||
* Handler called when the SelectBox selection changes. | ||
*/ | ||
onChange?: (isSelected: boolean) => void | ||
} | ||
|
||
export const SelectBoxItemContext = createContext<ContextValue<Partial<SelectBoxProps>, FocusableRefValue<HTMLLabelElement>>>(null); | ||
|
||
// Simple basic styling with proper dark mode support | ||
const selectBoxStyles = style({ | ||
...focusRing(), | ||
display: 'flex', | ||
flexDirection: 'column', | ||
lineHeight: 'title', | ||
justifyContent: 'center', | ||
flexShrink: 0, | ||
alignItems: 'center', | ||
fontFamily: 'sans', | ||
yihuiliao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
font: 'ui', | ||
|
||
// Vertical orientation (default) - Fixed square dimensions | ||
width: { | ||
default: { | ||
size: { | ||
XS: 100, | ||
S: 128, | ||
M: 136, | ||
L: 160, | ||
XL: 192 | ||
} | ||
}, | ||
orientation: { | ||
horizontal: { | ||
size: { | ||
dannify marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
XS: 'auto', | ||
S: 'auto', | ||
M: 'auto', | ||
L: 'auto', | ||
XL: 'auto' | ||
} | ||
} | ||
} | ||
}, | ||
|
||
height: { | ||
default: { | ||
size: { | ||
XS: 100, | ||
S: 128, | ||
M: 136, | ||
L: 160, | ||
XL: 192 | ||
} | ||
}, | ||
orientation: { | ||
horizontal: { | ||
size: { | ||
yihuiliao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
XS: 'auto', | ||
S: 'auto', | ||
M: 'auto', | ||
L: 'auto', | ||
XL: 'auto' | ||
} | ||
} | ||
} | ||
}, | ||
|
||
minWidth: { | ||
orientation: { | ||
horizontal: 160 | ||
} | ||
}, | ||
|
||
maxWidth: { | ||
orientation: { | ||
horizontal: 272 | ||
} | ||
}, | ||
|
||
padding: { | ||
size: { | ||
XS: 12, | ||
S: 16, | ||
M: 20, | ||
L: 24, | ||
XL: 28 | ||
} | ||
}, | ||
|
||
borderRadius: 'lg', | ||
backgroundColor: 'layer-2', | ||
boxShadow: { | ||
default: 'emphasized', | ||
isHovered: 'elevated', | ||
isSelected: 'elevated', | ||
forcedColors: 'none' | ||
}, | ||
position: 'relative', | ||
borderWidth: 2, | ||
borderStyle: { | ||
yihuiliao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
default: 'solid', | ||
isSelected: 'solid' | ||
}, | ||
borderColor: { | ||
default: 'transparent', | ||
isSelected: 'gray-900', | ||
isFocusVisible: 'transparent' | ||
DPandyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}, | ||
transition: 'default' | ||
}, getAllowedOverrides()); | ||
|
||
const checkboxContainer = style({ | ||
position: 'absolute', | ||
top: 16, | ||
left: 16 | ||
}, getAllowedOverrides()); | ||
|
||
/** | ||
* SelectBox components allow users to select options from a list. | ||
* They can behave as radio buttons (single selection) or checkboxes (multiple selection). | ||
*/ | ||
export const SelectBox = /*#__PURE__*/ forwardRef(function SelectBox(props: SelectBoxProps, ref: FocusableRef<HTMLLabelElement>) { | ||
[props, ref] = useSpectrumContextProps(props, ref, SelectBoxItemContext); | ||
let {children, value, isDisabled = false, isSelected, onChange, UNSAFE_className = '', UNSAFE_style} = props; | ||
let inputRef = useRef<HTMLInputElement | null>(null); | ||
let domRef = useFocusableRef(ref, inputRef); | ||
|
||
let groupContext = useContext(SelectBoxContext); | ||
let { | ||
allowMultiSelect = false, | ||
size = 'M', | ||
orientation = 'vertical' | ||
} = groupContext; | ||
DPandyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const Selector = allowMultiSelect ? AriaCheckbox : AriaRadio; | ||
|
||
// Handle controlled selection | ||
const handleSelectionChange = (selected: boolean) => { | ||
if (onChange) { | ||
yihuiliao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
onChange(selected); | ||
} | ||
}; | ||
|
||
return ( | ||
<Selector | ||
value={value} | ||
isDisabled={isDisabled} | ||
isSelected={isSelected} | ||
onChange={handleSelectionChange} | ||
ref={domRef} | ||
inputRef={inputRef} | ||
style={UNSAFE_style} | ||
className={renderProps => UNSAFE_className + selectBoxStyles({...renderProps, size, orientation}, props.styles)}> | ||
{renderProps => ( | ||
<> | ||
{(renderProps.isSelected || renderProps.isHovered) && ( | ||
|
||
<div className={checkboxContainer({...renderProps, size}, props.styles)}> | ||
<Checkbox | ||
value={value} | ||
isSelected={renderProps.isSelected} | ||
isDisabled={isDisabled} | ||
size={size} /> | ||
</div> | ||
)} | ||
{children} | ||
</> | ||
)} | ||
</Selector> | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.