UNPKG

segmented-controls-react

Version:

A good-lookin' segmented control React component ๐Ÿ”ฅ

64 lines (61 loc) โ€ข 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styles_1 = require("../styles"); const styled_1 = require("./styled"); /** * * @param {{ value: string, disabled?: boolean }[]} props.options ์„ธ๊ทธ๋จผํŠธ์˜ ์˜ต์…˜์„ value์— ๋‹ด์•„์ฃผ์„ธ์š”. ๋น„ํ™œ์„ฑํ™” ํ•˜๊ธธ ์›ํ•˜๋Š” ๋ฒ„ํŠผ์€ disabled ์†์„ฑ์„ ์ ์šฉํ•ด์ฃผ์„ธ์š”. * @param { Dispatch<SetStateAction<string>> } props.setValue ์„ ํƒ๋œ ์„ธ๊ทธ๋จผํŠธ๋ฅผ string์œผ๋กœ ๋ฆฌํ„ดํ•ด์š” * @param { "large" | "small" } props.size ์„ธ๊ทธ๋จผํŠธ์˜ ํฌ๊ธฐ๋ฅผ ์ •ํ•ด์ฃผ์„ธ์š”. large ํ˜น์€ small์˜ ๊ฐ’์„ ๊ฐ€์ ธ์š”. * @param { string } props.name input์˜ name๊ฐ’์„ ๋„ฃ์–ด์ฃผ์„ธ์š”. ์›น ์ ‘๊ทผ์„ฑ์„ ์œ„ํ•จ์ด์—์š”. * @param { defaultIndex } props.number ์ฒ˜์Œ ์„ ํƒ๋˜์–ด์žˆ๊ธธ ์›ํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์„ธ์š”. * @example * ```jsx import React from "react"; import { SegmentedControls } from "segmented-controls-react"; const App = () => { return ( <> <SegmentedControls options={[ { value: "one", disabled: true }, { value: "two" }, { value: "three" }, ]} defaultIndex={1} setValue={(val) => console.log(val)} size={"large"} name={"one"} /> </> ); }; export default App; * ``` */ const SegmentedControls = ({ options, setValue, size, name, defaultIndex, }) => { const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(defaultIndex); const [tappingIndex, setTappingIndex] = (0, react_1.useState)(-1); const id = (0, react_1.useId)(); const handleInputChange = (value, index) => { setSelectedIndex(index); setValue(value); }; // TODO: label๊ณผ bg ์ฝ”๋“œ์—์„œ ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„ ์ค„์ผ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ? return ((0, jsx_runtime_1.jsxs)(styled_1.Wrapper, Object.assign({ size: size, count: options === null || options === void 0 ? void 0 : options.length }, { children: [options === null || options === void 0 ? void 0 : options.map((option, index) => ((0, jsx_runtime_1.jsx)(styled_1.Input, { name: name, type: "radio", id: name + option.value, checked: index === selectedIndex, disabled: option.disabled, onChange: () => handleInputChange(option.value, index) }, name + option.value))), options === null || options === void 0 ? void 0 : options.map((option, index) => ((0, jsx_runtime_1.jsxs)(styled_1.MotionWrapper, Object.assign({ size: size }, { children: [(0, jsx_runtime_1.jsx)(styled_1.MotionLabel, Object.assign({ htmlFor: name + option.value, "data-value": option.value, size: size, isSelected: index === selectedIndex, disabled: option.disabled, whileTap: { scale: 0.9, transition: styles_1.spring.rapid, }, onTapStart: () => { setTappingIndex(index); }, onTap: () => { setTappingIndex(-1); }, tap: tappingIndex === index }, { children: option.value })), index === selectedIndex && ((0, jsx_runtime_1.jsx)(styled_1.SelectedBg, { size: size, layoutId: id, transition: { layout: { stiffness: 1000, damping: 52, }, } }))] }), name + option.value)))] }))); }; exports.default = SegmentedControls;