Skip to content

Conversation

@lixiaoyan
Copy link
Contributor

Closes

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

🧢 Your Project:

export type SelectionMode = 'single' | 'multiple';
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];
export type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can remove the null from the change value type for single selection. We already have the disallowEmptySelection option set.

disallowEmptySelection: selectionMode === 'single',

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately can't remove it, there is a way to get null out of this, which is to use the context to setValue(null), which acts like resetting the whole state back to the initial state where nothing is selected.

function SelectClearButton() {
  /*- begin highlight -*/
  let state = React.useContext(SelectStateContext);
  /*- end highlight -*/
  return (
    <Button
      // Don't inherit behavior from Select.
      slot={null}
      style={{fontSize: 'small', marginTop: 6, padding: 4}}
      onPress={() => state?.setValue(null)}>
      Clear
    </Button>
  );
}

Yes, it's a little weird. But we documented it at one point, so I think we've decided we were stuck with it for a while.

export type SelectionMode = 'single' | 'multiple';
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];
export type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately can't remove it, there is a way to get null out of this, which is to use the context to setValue(null), which acts like resetting the whole state back to the initial state where nothing is selected.

function SelectClearButton() {
  /*- begin highlight -*/
  let state = React.useContext(SelectStateContext);
  /*- end highlight -*/
  return (
    <Button
      // Don't inherit behavior from Select.
      slot={null}
      style={{fontSize: 'small', marginTop: 6, padding: 4}}
      onPress={() => state?.setValue(null)}>
      Clear
    </Button>
  );
}

Yes, it's a little weird. But we documented it at one point, so I think we've decided we were stuck with it for a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants