diff --git a/example/package-lock.json b/example/package-lock.json index 0ade50d..1421441 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -11,7 +11,7 @@ "@rose-hulman/react-native-dropdown-selector": "file:..", "react": "19.0.0", "react-native": "0.79.3", - "react-native-svg": "15.12.0" + "react-native-svg": "^15.12.0" }, "devDependencies": { "@babel/core": "^7.25.2", @@ -9239,6 +9239,12 @@ "node": ">= 0.6" } }, + "node_modules/mdn-data": { + "version": "2.0.14", + "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz", + "integrity": "sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==", + "license": "CC0-1.0" + }, "node_modules/memoize-one": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/memoize-one/-/memoize-one-5.2.1.tgz", diff --git a/package-lock.json b/package-lock.json index 1bc01f6..00017fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.2.1", "license": "MIT", "dependencies": { - "react-native-svg": "15.12.0" + "react-native-svg": "^15.12.0" }, "devDependencies": { "@types/react": "^18.2.9", diff --git a/src/components/MultiSelect.tsx b/src/components/MultiSelect.tsx index 712b573..777179c 100644 --- a/src/components/MultiSelect.tsx +++ b/src/components/MultiSelect.tsx @@ -4,6 +4,7 @@ import { useThemeStyles } from '../styles'; import type { Data, SelectorRect, MultiSelectProperties } from '../types'; import SelectionList from './SelectionList'; import Svg, { Path } from 'react-native-svg'; +import { createMeasureHandler, updatePriorities, renderDropdownArrow } from '../utils/SelectorUtils'; /* Renders a multi-selector component. Takes in props defined in the MultiSelectProperties type. */ const MultiSelect = (props: MultiSelectProperties): React.JSX.Element => { @@ -25,21 +26,7 @@ const MultiSelect = (props: MultiSelectProperties): React.JSX.Element => { props.data.filter((d: Data) => props.defaultValue?.includes(d)) ); - const updatePriorities = (data: Data[]) => [ - ...data.filter((d: Data) => d.priority), - ...data.filter((d: Data) => !d.priority), - ]; - const updatePos = (display = false) => - ref.current?.measureInWindow((x, y, width, height) => { - setRefRect({ - x: x, - y: y - refRectYOffset, - width: props.boxStyle?.width ?? width, - height: height + 2*refRectYOffset, - }); - if (display) - setListDisplay(true); - }); + const updatePos = createMeasureHandler(ref, setRefRect, setListDisplay, props.boxStyle?.width); return ( @@ -81,17 +68,10 @@ const MultiSelect = (props: MultiSelectProperties): React.JSX.Element => { {props.placeholderText ?? defaultPlaceholderText} } - - {listDisplay ? ( - // This is the up arrow "ᨈ" - - - ) : ( - // This is the down arrow "ᨆ" - - - )} - + {renderDropdownArrow( + listDisplay, + props.dropdownArrowColor ?? style.arrow?.color ?? '#000' + )} { @@ -26,20 +27,7 @@ const Select = (props: SelectProperties): React.JSX.Element => { ? props.defaultValue : {label: props.placeholderText ?? defaultPlaceholderText} ); - const updatePriorities = (data: Data[]) => [ - ...data.filter((d: Data) => d.priority), - ...data.filter((d: Data) => !d.priority), - ]; - const updatePos = () => - ref.current?.measureInWindow((x, y, width, height) => { - setRefRect({ - x: x, - y: y - refRectYOffset, - width: props.boxStyle?.width ?? width, - height: height + 2*refRectYOffset, - }); - setListDisplay(true); - }); + const updatePos = createMeasureHandler(ref, setRefRect, setListDisplay, props.boxStyle?.width); return ( @@ -51,7 +39,7 @@ const Select = (props: SelectProperties): React.JSX.Element => { {opacity: props.disabled ? disabledOpacity : enabledOpacity}, ]} disabled={props.disabled} - onPress={updatePos} + onPress={() => updatePos(true)} ref={ref} > { > {selected.label} - - {listDisplay ? ( - // This is the up arrow "ᨈ" - - - ) : ( - // This is the down arrow "ᨆ" - - - )} - + {renderDropdownArrow( + listDisplay, + props.dropdownArrowColor ?? style.arrow?.color ?? '#000' + )} , + setRefRect: React.Dispatch>, + setListDisplay: React.Dispatch>, + boxStyleWidth?: number | string, + refRectYOffset: number = 5, +) => { + return (display = false) => { + ref.current?.measureInWindow((x: number, y: number, width: number, height: number) => { + setRefRect({ + x: x, + y: y - refRectYOffset, + width: boxStyleWidth ?? width, + height: height + refRectYOffset * 2, + }); + + if (display && setListDisplay) { + setListDisplay(true); + } + }); + } +} + +export const updatePriorities = (data: Data[]) => [ + ...data.filter((d: Data) => d.priority), + ...data.filter((d: Data) => !d.priority), +]; + +export const renderDropdownArrow = (listDisplay: boolean, arrowColor: ColorValue) => ( + + {listDisplay ? ( + // This is the up arrow "ᨈ" + + + ) : ( + // This is the up arrow "ᨆ" + + + + + )} + +) \ No newline at end of file