-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: allow ColorSwatchPicker to have no selection #10331
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,14 +28,14 @@ import React, { | |
| useEffect, | ||
| useMemo | ||
| } from 'react'; | ||
| import {useColorPickerState} from 'react-stately/useColorPickerState'; | ||
| import {useColorSwatchPickerState} from 'react-stately/useColorSwatchPickerState'; | ||
| import {useLocale} from 'react-aria/I18nProvider'; | ||
| import {useLocalizedStringFormatter} from 'react-aria/useLocalizedStringFormatter'; | ||
|
|
||
| export interface ColorSwatchPickerRenderProps extends Omit<ListBoxRenderProps, 'isDropTarget'> {} | ||
| export interface ColorSwatchPickerProps | ||
| extends | ||
| ValueBase<string | Color, Color>, | ||
| ValueBase<string | Color | null, Color | null>, | ||
| AriaLabelingProps, | ||
| StyleRenderProps<ColorSwatchPickerRenderProps>, | ||
| GlobalDOMAttributes<HTMLDivElement> { | ||
|
|
@@ -68,7 +68,7 @@ export const ColorSwatchPicker = forwardRef(function ColorSwatchPicker( | |
| ref: ForwardedRef<HTMLDivElement> | ||
| ) { | ||
| [props, ref] = useContextProps(props, ref, ColorSwatchPickerContext); | ||
| let state = useColorPickerState(props); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changes should be made in the stately hook, we should still be using the hook here. was there a reason you dropped it?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at the precedent for Select (#2676) β that PR only changed a type declaration (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. For now lets create a new stately hook we can use here. Make it look the same as the original hook so that it's easier to compare against (no need for all the memos) |
||
| let state = useColorSwatchPickerState(props); | ||
| let colorMap = useMemo(() => new Map(), []); | ||
| let formatter = useLocalizedStringFormatter(intlMessages, 'react-aria-components'); | ||
|
|
||
|
|
@@ -84,7 +84,7 @@ export const ColorSwatchPicker = forwardRef(function ColorSwatchPicker( | |
| } | ||
| layout={props.layout || 'grid'} | ||
| selectionMode="single" | ||
| selectedKeys={[state.color.toString('hexa')]} | ||
| selectedKeys={state.color ? [state.color.toString('hexa')] : []} | ||
| onSelectionChange={keys => { | ||
| // single select, 'all' cannot occur. appease typescript. | ||
| if (keys !== 'all') { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * Copyright 2024 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 REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| export type { | ||
| ColorSwatchPickerStateProps, | ||
| ColorSwatchPickerState | ||
| } from '../src/color/useColorSwatchPickerState'; | ||
|
|
||
| export {useColorSwatchPickerState} from '../src/color/useColorSwatchPickerState'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import {Color} from './types'; | ||
| import {useColor} from './useColor'; | ||
| import {useControlledState} from '../utils/useControlledState'; | ||
| import {ValueBase} from '@react-types/shared'; | ||
|
|
||
| export interface ColorSwatchPickerStateProps extends ValueBase< | ||
| string | Color | null, | ||
| Color | null | ||
| > {} | ||
|
|
||
| export interface ColorSwatchPickerState { | ||
| /** The current color value of the color swatch picker. */ | ||
| color: Color | null; | ||
| /** Sets the current color value of the color swatch picker. */ | ||
| setColor(color: Color | null): void; | ||
| } | ||
|
|
||
| export function useColorSwatchPickerState( | ||
| props: ColorSwatchPickerStateProps | ||
| ): ColorSwatchPickerState { | ||
| let value = useColor(props.value); | ||
| let defaultValue = useColor(props.defaultValue) ?? null; | ||
| let [color, setColor] = useControlledState<Color | null>(value, defaultValue, props.onChange); | ||
|
|
||
| return { | ||
| color, | ||
| setColor(color) { | ||
| setColor(color); | ||
| } | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just highlighting for the rest of the team. This now requires additional typescript and checks against null in code as seen in this file.
We went through some similar steps back in Select
https://github.com/adobe/react-spectrum/pull/2676/changes
https://github.com/adobe/react-spectrum/pull/7888/changes#diff-1de776587d3b665172f7a4e80f44244c69d868bd3ac9c8d8b6e3f6235beaa1ac