diff --git a/package.json b/package.json index 4ee44475..eddb712d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ebs-design", - "version": "0.0.1-beta.95", + "version": "0.0.1-beta.96", "description": "EBS Design System React UI elements.", "author": "EBS Integrator (https://ebs-integrator.com/)", "maintainers": [ diff --git a/src/components/Palette.stories.mdx b/src/components/Palette.stories.mdx index fc1ce667..25dfd352 100644 --- a/src/components/Palette.stories.mdx +++ b/src/components/Palette.stories.mdx @@ -1,4 +1,4 @@ -import { Meta } from '@storybook/addon-docs/blocks'; +import { Meta } from '@storybook/addon-docs'; import { exportStory } from '../libs'; diff --git a/src/components/ReadMe.stories.mdx b/src/components/ReadMe.stories.mdx index de5213e3..d2b586dc 100644 --- a/src/components/ReadMe.stories.mdx +++ b/src/components/ReadMe.stories.mdx @@ -1,4 +1,4 @@ -import { Meta } from '@storybook/addon-docs/blocks'; +import { Meta } from '@storybook/addon-docs'; import { exportStory } from '../libs'; diff --git a/src/components/SizeSwitcher.tsx b/src/components/SizeSwitcher.tsx index cb71907e..bc05e783 100644 --- a/src/components/SizeSwitcher.tsx +++ b/src/components/SizeSwitcher.tsx @@ -15,8 +15,8 @@ const SizeSwitcher: React.FC = ({ sizes = ['small', 'medium', 'large'], d return ( <> - {sizes.map((value) => ( - ))} diff --git a/src/components/Variables.stories.mdx b/src/components/Variables.stories.mdx index e3b31d6a..383dc16d 100644 --- a/src/components/Variables.stories.mdx +++ b/src/components/Variables.stories.mdx @@ -1,4 +1,4 @@ -import { Meta } from '@storybook/addon-docs/blocks'; +import { Meta } from '@storybook/addon-docs'; import { exportStory } from '../libs'; diff --git a/src/components/atoms/Input/Input.tsx b/src/components/atoms/Input/Input.tsx index dd5918cb..bd772dd0 100644 --- a/src/components/atoms/Input/Input.tsx +++ b/src/components/atoms/Input/Input.tsx @@ -56,7 +56,7 @@ export const Input = React.forwardRef( const hasValue = React.useMemo(() => value != undefined && value.toString().length, [value]); const onClickHandler = ({ target }: React.ChangeEvent): void => { - if (onChange !== undefined) { + if (onChange) { onChange( props.min !== undefined && parseFloat(props.min as string) >= parseFloat(target.value) ? props.min @@ -68,13 +68,13 @@ export const Input = React.forwardRef( }; const onClickPrefixHandler = (): void => { - if (!loading && props.onClickPrefix !== undefined) { + if (!loading && props.onClickPrefix) { props.onClickPrefix(); } }; const onClickSuffixHandler = (): void => { - if (!loading && props.onClickSuffix !== undefined) { + if (!loading && props.onClickSuffix) { props.onClickSuffix(); } }; diff --git a/src/components/atoms/ReadMe.stories.mdx b/src/components/atoms/ReadMe.stories.mdx index b12a7c61..f201d546 100644 --- a/src/components/atoms/ReadMe.stories.mdx +++ b/src/components/atoms/ReadMe.stories.mdx @@ -1,4 +1,4 @@ -import { Meta } from '@storybook/addon-docs/blocks'; +import { Meta } from '@storybook/addon-docs'; import { exportStory } from '../../libs'; diff --git a/src/components/molecules/ReadMe.stories.mdx b/src/components/molecules/ReadMe.stories.mdx index 1c50cf9c..1361e892 100644 --- a/src/components/molecules/ReadMe.stories.mdx +++ b/src/components/molecules/ReadMe.stories.mdx @@ -1,4 +1,4 @@ -import { Meta } from '@storybook/addon-docs/blocks'; +import { Meta } from '@storybook/addon-docs'; import { exportStory } from '../../libs'; diff --git a/src/components/molecules/Select/Context.tsx b/src/components/molecules/Select/Context.tsx index 1d71c4a8..3dae8329 100644 --- a/src/components/molecules/Select/Context.tsx +++ b/src/components/molecules/Select/Context.tsx @@ -10,7 +10,7 @@ interface StateProps { newOption?: string; search: string | boolean; style?: React.CSSProperties; - maxHeight: number; + offsetBottom?: number; isOpen: boolean; isLoaded: boolean; } @@ -23,7 +23,6 @@ export interface ContextProps extends StateProps { const Context = React.createContext({ options: [], cache: [], - maxHeight: 350, search: false, isOpen: false, isLoaded: false, @@ -35,7 +34,6 @@ const Provider: React.FC = ({ children }) => { const [state, setState] = useSetState({ options: [], cache: [], - maxHeight: 350, search: false, isOpen: false, isLoaded: false, diff --git a/src/components/molecules/Select/Hook.tsx b/src/components/molecules/Select/Hook.tsx index 7142da2d..fe343717 100644 --- a/src/components/molecules/Select/Hook.tsx +++ b/src/components/molecules/Select/Hook.tsx @@ -23,7 +23,7 @@ interface Props extends ContextProps { onKeyDownNewOption: (e: React.KeyboardEvent) => void; } -export default ({ loading, refs, children, ...params }): Props => { +export default ({ loading, ref, children, ...params }): Props => { const { mode = 'single', optionsMode = 'dropdown', @@ -84,28 +84,29 @@ export default ({ loading, refs, children, ...params }): Props => { onClickAddNew(); } }, - refs.input, + ref, ); - useClickAway(refs.input, (e) => { + useClickAway(ref, (e) => { if (isOpen && !isBox && optionsRef.current && !optionsRef.current.contains(e.target as Node)) { setState({ isOpen: false }); } }); React.useEffect(() => { - if (!isBox && isOpen && refs.input.current) { - const rect = refs.input.current.getBoundingClientRect(); + if (!isBox && isOpen && ref.current) { + const rect = ref.current.getBoundingClientRect(); + const offsetBottom = rect.bottom + rect.height; const style: React.CSSProperties = { width: `${rect.width}px`, }; if (!isEqual(style, optionsStyle)) { - setState({ style }); + setState({ style, offsetBottom }); } } - }, [isOpen, refs, optionsStyle]); + }, [isOpen, ref, optionsStyle]); React.useEffect(() => { if (searchEl?.props?.value !== undefined && search !== searchEl.props.value) { diff --git a/src/components/molecules/Select/Select.stories.tsx b/src/components/molecules/Select/Select.stories.tsx index 3231c396..2e8a8b3f 100644 --- a/src/components/molecules/Select/Select.stories.tsx +++ b/src/components/molecules/Select/Select.stories.tsx @@ -14,7 +14,7 @@ export default { component: Select, }; -const limit = 10; +const limit = 20; export const Regular = (): React.ReactNode => { const [form] = useForm(); @@ -57,7 +57,7 @@ export const Regular = (): React.ReactNode => { return ( {(size) => ( - +
{ return ( {(size) => ( - + { return ( {(size) => ( - + { return ( {(size) => ( - + { return ( {(size) => ( - + { return ( {(size) => ( - + = ({ children, ...props }) => { - const inputRef = React.useRef(null); + const ref = React.useRef(null); const valueRef = React.useRef(null); const { @@ -58,10 +58,7 @@ const Component: React.FC = ({ } = useSelect({ mode, loading, - refs: { - root: props.rootRef, - input: inputRef, - }, + ref, children, ...props, }); @@ -74,7 +71,7 @@ const Component: React.FC = ({ return (
; @@ -42,7 +43,9 @@ const Options: React.FC & OptionsComposition = ({ onClickAddNew, }) => { const ref = React.useRef(null); + const { offsetBottom } = React.useContext(Context); const [activeItem, setActiveItem] = React.useState(0); + const [maxHeight, setMaxHeight] = React.useState(0); const onScroll = (e): void => { const scrollTop = Math.floor(e.target.scrollTop); @@ -53,22 +56,32 @@ const Options: React.FC & OptionsComposition = ({ }; React.useEffect(() => { - if (ref?.current && !scrollMode && options?.length) { + const rect = ref.current?.getBoundingClientRect(); + + if (rect?.height && offsetBottom) { + const height = window.innerHeight - offsetBottom; + + setMaxHeight(height <= rect.height ? height : rect.height); + } + }, [ref.current, offsetBottom]); + + React.useEffect(() => { + if (ref.current && !scrollMode && options?.length) { ref.current.scrollTop = 0; } - }, [ref, options, scrollMode]); + }, [ref.current, options, scrollMode]); React.useEffect(() => { - if (ref?.current && scrollMode) { + if (ref.current && scrollMode) { ref.current.addEventListener('scroll', onScroll); } return () => { - if (ref?.current && scrollMode) { + if (ref.current && scrollMode) { ref.current.removeEventListener('scroll', onScroll); } }; - }, [ref, onScroll, scrollMode]); + }, [ref.current, onScroll, scrollMode]); useEventListener( 'keydown', @@ -117,7 +130,7 @@ const Options: React.FC & OptionsComposition = ({ className={cn('ebs-select__options-items', { 'ebs-select__options--multiple': ['multiple', 'tags'].includes(mode), })} - style={scrollMode === 'scroll' ? { maxHeight: 300 } : undefined} + style={maxHeight ? { maxHeight } : undefined} > {newOption && onClickAddNew && ( & OptionsComposition = ({ loading={scrollMode !== 'scroll' ? loading || false : false} height={!ref.current || ref.current?.offsetHeight < 300 ? 300 : ref.current.offsetHeight} > - - {options.length - ? options.map((option, key) => ( - - )) - : !loading && ( - - {emptyLabel} - - )} - + {options.length + ? options.map((option, key) => ( + + )) + : !loading && ( + + {emptyLabel} + + )} - {scrollMode === 'scroll' && loading ? ( + {loading ? ( diff --git a/src/components/molecules/Select/components/Options/index.ts b/src/components/molecules/Select/components/Options/index.ts index 6e8a1714..a739b4f3 100644 --- a/src/components/molecules/Select/components/Options/index.ts +++ b/src/components/molecules/Select/components/Options/index.ts @@ -1 +1 @@ -export * from './Options'; +export { Options, OptionsComposition, OptionsProps } from './Options'; diff --git a/src/components/organisms/ReadMe.stories.mdx b/src/components/organisms/ReadMe.stories.mdx index d94fb5fe..55606b4f 100644 --- a/src/components/organisms/ReadMe.stories.mdx +++ b/src/components/organisms/ReadMe.stories.mdx @@ -1,4 +1,4 @@ -import { Meta } from '@storybook/addon-docs/blocks'; +import { Meta } from '@storybook/addon-docs'; import { exportStory } from '../../libs'; diff --git a/src/components/templates/ReadMe.stories.mdx b/src/components/templates/ReadMe.stories.mdx index c7a5ac9c..647969ac 100644 --- a/src/components/templates/ReadMe.stories.mdx +++ b/src/components/templates/ReadMe.stories.mdx @@ -1,4 +1,4 @@ -import { Meta } from '@storybook/addon-docs/blocks'; +import { Meta } from '@storybook/addon-docs'; import { exportStory } from '../../libs';