Skip to content

Commit 546ffa4

Browse files
authored
Fix taggroup disabled onremove (#6690)
1 parent 63a2ff6 commit 546ffa4

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

packages/@react-aria/tag/src/useTag.ts

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export function useTag<T>(props: AriaTagProps<T>, state: ListState<T>, ref: RefO
8787
removeButtonProps: {
8888
'aria-label': stringFormatter.format('removeButtonLabel'),
8989
'aria-labelledby': `${buttonId} ${rowProps.id}`,
90+
isDisabled: state.disabledKeys.has(item.key) || item.props.isDisabled,
9091
id: buttonId,
9192
onPress: () => onRemove ? onRemove(new Set([item.key])) : null,
9293
excludeFromTabOrder: true

packages/react-aria-components/src/TagGroup.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ function TagListInner<T extends object>({props, forwardedRef}: TagListInnerProps
167167
data-empty={state.collection.size === 0 || undefined}
168168
data-focused={isFocused || undefined}
169169
data-focus-visible={isFocusVisible || undefined}>
170-
{state.collection.size === 0 && props.renderEmptyState
171-
? props.renderEmptyState(renderValues)
170+
{state.collection.size === 0 && props.renderEmptyState
171+
? props.renderEmptyState(renderValues)
172172
: <CollectionRoot collection={state.collection} persistedKeys={persistedKeys} />}
173173
</div>
174174
);

packages/react-aria-components/stories/TagGroup.stories.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,9 @@ TagGroupExample.argTypes = {
5050
};
5151

5252
function MyTag(props: TagProps) {
53-
return <Tag {...props} style={({isSelected}) => ({border: '1px solid gray', borderRadius: 4, padding: '0 4px', background: isSelected ? 'black' : '', color: isSelected ? 'white' : '', cursor: props.href ? 'pointer' : 'default'})} />;
53+
return (
54+
<Tag
55+
{...props}
56+
style={({isSelected}) => ({border: '1px solid gray', borderRadius: 4, padding: '0 4px', background: isSelected ? 'black' : '', color: isSelected ? 'white' : '', cursor: props.href ? 'pointer' : 'default'})} />
57+
);
5458
}

packages/react-aria-components/test/TagGroup.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,32 @@ describe('TagGroup', () => {
266266
expect(onRemove).toHaveBeenCalledTimes(1);
267267
});
268268

269+
it('should disable the remove button if the tag is disabled', async () => {
270+
let onRemove = jest.fn();
271+
let {getAllByRole} = render(
272+
<TagGroup data-testid="group" onRemove={onRemove} disabledKeys={new Set(['dog'])}>
273+
<Label>Test</Label>
274+
<TagList>
275+
<RemovableTag id="cat">Cat</RemovableTag>
276+
<RemovableTag id="dog">Dog</RemovableTag>
277+
<RemovableTag id="kangaroo">Kangaroo</RemovableTag>
278+
</TagList>
279+
<Text slot="description">Description</Text>
280+
<Text slot="errorMessage">Error</Text>
281+
</TagGroup>
282+
);
283+
284+
let row = getAllByRole('row')[0];
285+
286+
expect(row).toHaveAttribute('data-allows-removing', 'true');
287+
288+
let button = getAllByRole('button', {hidden: true})[1];
289+
expect(button).toHaveAttribute('aria-label', 'Remove');
290+
expect(button).toHaveAttribute('disabled');
291+
await user.click(button);
292+
expect(onRemove).toHaveBeenCalledTimes(0);
293+
});
294+
269295
it('should support empty state', () => {
270296
let {getByTestId} = render(
271297
<TagGroup data-testid="group">

0 commit comments

Comments
 (0)