segmented-controls-react
Version:
A good-lookin' segmented control React component ๐ฅ
64 lines (61 loc) โข 3.51 kB
JavaScript
;
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;