From ebd6ea5715cd2e47720e177f3b39719b780d7853 Mon Sep 17 00:00:00 2001 From: jpsorensen <93107699+jpsorensen@users.noreply.github.com> Date: Mon, 6 Mar 2023 12:48:44 -0500 Subject: [PATCH 1/3] Add Typescript annotations to Select, SelectItem and SelectItemGroup (#13235) * refactor(typescript): type annotations on Select* Add Typescript annotations to Select, SelectItem and SelectItemGroup. * fix(lint): removed unused imports Left SelectItem and SelectItemGroup in the imports after using them for testing. Removed. * fix(Select-test): update label to labelText Previously removed label as it was not a property of Select. On recommendation of maintainers reintroduced using labelText. * Apply suggestions from code review Co-authored-by: Andrea N. Cardona * Follow up to change suggestions --------- Co-authored-by: Andrea N. Cardona Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../Select/{Select.js => Select.tsx} | 113 +++++++++++++++++- .../Select/__tests__/Select-test.js | 2 +- .../{SelectItem.js => SelectItem.tsx} | 40 ++++++- ...SelectItemGroup.js => SelectItemGroup.tsx} | 26 +++- 4 files changed, 172 insertions(+), 9 deletions(-) rename packages/react/src/components/Select/{Select.js => Select.tsx} (75%) rename packages/react/src/components/SelectItem/{SelectItem.js => SelectItem.tsx} (63%) rename packages/react/src/components/SelectItemGroup/{SelectItemGroup.js => SelectItemGroup.tsx} (71%) diff --git a/packages/react/src/components/Select/Select.js b/packages/react/src/components/Select/Select.tsx similarity index 75% rename from packages/react/src/components/Select/Select.js rename to packages/react/src/components/Select/Select.tsx index 9a7e261ad49c..2368506f653a 100644 --- a/packages/react/src/components/Select/Select.js +++ b/packages/react/src/components/Select/Select.tsx @@ -6,7 +6,14 @@ */ import PropTypes from 'prop-types'; -import React, { useContext, useState } from 'react'; +import React, { + ChangeEventHandler, + ComponentPropsWithRef, + ForwardedRef, + ReactNode, + useContext, + useState, +} from 'react'; import classNames from 'classnames'; import { ChevronDown, @@ -18,6 +25,106 @@ import { useFeatureFlag } from '../FeatureFlags'; import { usePrefix } from '../../internal/usePrefix'; import { FormContext } from '../FluidForm'; +type ExcludedAttributes = 'size'; + +interface SelectProps + extends Omit, ExcludedAttributes> { + /** + * Provide the contents of your Select + */ + children?: ReactNode; + + /** + * Specify an optional className to be applied to the node containing the label and the select box + */ + className?: string; + + /** + * Optionally provide the default value of the `` + */ + id: string; + + /** + * Specify whether you want the inline version of this control + */ + inline?: boolean; + + /** + * Specify if the currently value is invalid. + */ + invalid?: boolean; + + /** + * Message which is displayed if the value is invalid. + */ + invalidText?: ReactNode; + + /** + * Provide label text to be read by screen readers when interacting with the control. + */ + labelText?: ReactNode; + + /** + * `true` to use the light version. For use on $ui-01 backgrounds only. + * Don't use this to make tile background color same as container background color. + * + * @deprecated The `light` prop for `Select` is no longer needed and has been deprecated in v11 in favor of the new `Layer` component. + * It will be moved in the next major release. + */ + light?: boolean; + + /** + * Reserved for use with component. Will not render a label for the + * select since Pagination renders one for us. + */ + noLabel?: boolean; + + /** + * Provide an optional `onChange` hook that is called each time the value of the underlying `` changes. + */ + onChange?: ChangeEventHandler; + + /** + * Whether the select should be read-only + */ + readOnly?: boolean; + + /** + * Specify the size of the Select Input. + */ + size?: 'sm' | 'md' | 'lg'; + + /** + * Specify whether the control is currently in warning state + */ + warn?: boolean; + + /** + * Provide the text that is displayed when the control is in warning state + */ + warnText?: ReactNode; +} + const Select = React.forwardRef(function Select( { className, @@ -39,8 +146,8 @@ const Select = React.forwardRef(function Select( warn = false, warnText, ...other - }, - ref + }: SelectProps, + ref: ForwardedRef ) { const prefix = usePrefix(); const enabled = useFeatureFlag('enable-v11-release'); diff --git a/packages/react/src/components/Select/__tests__/Select-test.js b/packages/react/src/components/Select/__tests__/Select-test.js index acbb99a356a3..b47506fb3018 100644 --- a/packages/react/src/components/Select/__tests__/Select-test.js +++ b/packages/react/src/components/Select/__tests__/Select-test.js @@ -174,7 +174,7 @@ describe('Select', () => { render(