| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- import {
- useLayoutEffect,
- useState
- } from "react";
- import {
- View
- } from "react-native";
- import stylesheet from "./stylesheet";
- import {
- NCoreUIKitLocalize,
- NCoreUIKitTheme,
- PageContainer,
- CodeViewer,
- SelectBox,
- TextInput,
- Button,
- Header,
- Switch,
- Text
- } from "ncore-ui-kit";
- import {
- useNavigation
- } from "@react-navigation/native";
- import type RootStackParamList from "../../../navigation/type";
- import type {
- NativeStackNavigationProp
- } from "@react-navigation/native-stack";
- import type {
- IButtonProps
- } from "ncore-ui-kit";
- import {
- Home as HomeIcon
- } from "lucide-react-native";
- const BUTTON_TYPES: Array<IButtonProps["type"]> = [
- "primary",
- "danger",
- "warning",
- "success",
- "info"
- ];
- const BUTTON_VARIANTS: Array<IButtonProps["variant"]> = [
- "filled",
- "outline",
- "ghost"
- ];
- const BUTTON_SIZES: Array<IButtonProps["size"]> = [
- "small",
- "medium",
- "large"
- ];
- const BUTTON_TYPES_DATA = BUTTON_TYPES.map(item => ({
- __title: item as string,
- __key: item as string
- }));
- const BUTTON_SPREAD_BEHAVIOURS: Array<IButtonProps["spreadBehaviour"]> = [
- "baseline",
- "stretch",
- "free"
- ];
- const BUTTON_VARIANTS_DATA = BUTTON_VARIANTS.map(item => ({
- __title: item as string,
- __key: item as string
- }));
- const BUTTON_SIZES_DATA = BUTTON_SIZES.map(item => ({
- __title: item as string,
- __key: item as string
- }));
- const ButtonPage = () => {
- const {
- radiuses,
- borders,
- spaces,
- colors
- } = NCoreUIKitTheme.useContext();
- const {
- localize
- } = NCoreUIKitLocalize.useContext();
- const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
- const [
- typeIndex,
- setTypeIndex
- ] = useState<number>(0);
- const [
- spreadIndex,
- setSpreadIndex
- ] = useState<number>(0);
- const [
- variantIndex,
- setVariantIndex
- ] = useState<number>(0);
- const [
- sizeIndex,
- setSizeIndex
- ] = useState<number>(1);
- const [
- title,
- setTitle
- ] = useState<string>("");
- const [
- isLoading,
- setIsLoading
- ] = useState<boolean>(false);
- const [
- isDisabled,
- setIsDisabled
- ] = useState<boolean>(false);
- const [
- isCustomPadding,
- setIsCustomPadding
- ] = useState<boolean>(false);
- const [
- showIcon,
- setShowIcon
- ] = useState<boolean>(false);
- const [
- iconDirection,
- setIconDirection
- ] = useState<"left" | "right">("left");
- useLayoutEffect(() => {
- navigation.setOptions({
- header: () => <Header
- isWrapSafeareaContext={false}
- title={localize("button")}
- navigation={navigation}
- isGoBackEnable={true}
- />,
- headerShown: true
- });
- }, []);
- return <PageContainer
- scrollViewProps={{
- contentContainerStyle: stylesheet.scrollContent
- }}
- isWorkWithHeaderSpace={false}
- scrollViewStyle={[
- stylesheet.container
- ]}
- isScrollable={true}
- >
- <View
- style={[
- stylesheet.contentContainer
- ]}
- >
- <Text
- style={stylesheet.descText}
- variant="bodyMediumSize"
- color="mid"
- >
- {localize("button-desc")}
- </Text>
- <View
- style={[
- stylesheet.previewContainer,
- {
- borderColor: colors.content.border.subtle,
- marginBottom: spaces.spacingMd,
- borderRadius: radiuses.form,
- borderWidth: borders.line,
- padding: spaces.spacingMd
- }
- ]}
- >
- <Button
- spreadBehaviour={BUTTON_SPREAD_BEHAVIOURS[spreadIndex]}
- title={title || localize("example-button")}
- variant={BUTTON_VARIANTS[variantIndex]}
- icon={showIcon ? ({
- color,
- size
- }) => <HomeIcon
- color={colors.content.icon[color]}
- size={size}
- /> : undefined}
- style={{
- marginBottom: spaces.spacingLg
- }}
- isCustomPadding={isCustomPadding}
- type={BUTTON_TYPES[typeIndex]}
- size={BUTTON_SIZES[sizeIndex]}
- iconDirection={iconDirection}
- isDisabled={isDisabled}
- isLoading={isLoading}
- onPress={() => {
- }}
- />
- </View>
- <View
- style={{
- marginBottom: spaces.spacingMd
- }}
- >
- <Text
- style={{
- marginBottom: spaces.spacingSm
- }}
- variant="headlineSmallSize"
- >
- {localize("usage")}
- </Text>
- <CodeViewer
- code={"import {\n Button\n} from \"ncore-ui-kit\";\n\n<Button\n title=\"Click Me!\"\n onPress={() => console.log(\"Pressed\")}\n spreadBehaviour=\"stretch\"\n iconDirection=\"left\"\n variant=\"filled\"\n type=\"primary\"\n size=\"medium\"\n/>"}
- language="tsx"
- />
- </View>
- <TextInput
- placeholder={localize("enter-text")}
- style={{
- marginBottom: spaces.spacingMd
- }}
- title={localize("button-title")}
- spreadBehaviour="stretch"
- onChangeText={setTitle}
- value={title}
- />
- <View
- style={[
- stylesheet.rowContainer,
- {
- gap: spaces.spacingMd
- }
- ]}
- >
- <SelectBox
- onChange={(selectedItems) => {
- if (selectedItems.length > 0) {
- const index = BUTTON_TYPES.indexOf(selectedItems[0]!.__key as IButtonProps["type"]);
- if (index !== -1) setTypeIndex(index);
- }
- }}
- initialSelectedItems={
- BUTTON_TYPES_DATA[typeIndex]
- ? [
- BUTTON_TYPES_DATA[typeIndex]!
- ]
- : []
- }
- titleExtractor={(item) => item.__title}
- keyExtractor={(item) => item.__key}
- title={localize("change-type")}
- data={BUTTON_TYPES_DATA}
- spreadBehaviour="free"
- />
- <SelectBox
- onChange={(selectedItems) => {
- if (selectedItems.length > 0) {
- const index = BUTTON_VARIANTS.indexOf(selectedItems[0]!.__key as IButtonProps["variant"]);
- if (index !== -1) setVariantIndex(index);
- }
- }}
- initialSelectedItems={
- BUTTON_VARIANTS_DATA[variantIndex]
- ? [
- BUTTON_VARIANTS_DATA[variantIndex]!
- ]
- : []
- }
- titleExtractor={(item) => item.__title}
- keyExtractor={(item) => item.__key}
- title={localize("change-variant")}
- data={BUTTON_VARIANTS_DATA}
- spreadBehaviour="free"
- />
- <Button
- onPress={() => {
- setSpreadIndex(prev => prev + 1 > BUTTON_SPREAD_BEHAVIOURS.length - 1 ? 0 : prev + 1);
- }}
- title={`${localize("change-spread")}: ${BUTTON_SPREAD_BEHAVIOURS[spreadIndex]}`}
- verticalLocationForStretchFix="center"
- isFixVerticalStretch={true}
- spreadBehaviour="free"
- />
- <SelectBox
- onChange={(selectedItems) => {
- if (selectedItems.length > 0) {
- const index = BUTTON_SIZES.indexOf(selectedItems[0]!.__key as IButtonProps["size"]);
- if (index !== -1) setSizeIndex(index);
- }
- }}
- initialSelectedItems={
- BUTTON_SIZES_DATA[sizeIndex]
- ? [
- BUTTON_SIZES_DATA[sizeIndex]!
- ]
- : []
- }
- titleExtractor={(item) => item.__title}
- keyExtractor={(item) => item.__key}
- title={localize("change-size")}
- data={BUTTON_SIZES_DATA}
- spreadBehaviour="free"
- />
- <View
- style={stylesheet.switchesContainer}
- >
- <Switch
- onPress={() => {
- setIconDirection(iconDirection === "left" ? "right" : "left");
- }}
- title={localize("toggle-icon-direction")}
- isActive={iconDirection === "right"}
- subTitle={localize(iconDirection)}
- spreadBehaviour="free"
- />
- <Switch
- title={localize("toggle-icon")}
- onPress={() => {
- setShowIcon(!showIcon);
- }}
- spreadBehaviour="free"
- isActive={showIcon}
- />
- <Switch
- title={localize("toggle-loading")}
- onPress={() => {
- setIsLoading(!isLoading);
- }}
- spreadBehaviour="free"
- isActive={isLoading}
- />
- <Switch
- title={localize("toggle-disabled")}
- onPress={() => {
- setIsDisabled(!isDisabled);
- }}
- spreadBehaviour="free"
- isActive={isDisabled}
- />
- <Switch
- onPress={() => {
- setIsCustomPadding(!isCustomPadding);
- }}
- title={localize("toggle-custom-padding")}
- isActive={isCustomPadding}
- spreadBehaviour="free"
- />
- </View>
- </View>
- </View>
- </PageContainer>;
- };
- export default ButtonPage;
|