image

Input Button

Input Button (ปุ่มกด) คือ View ที่ใช้ในการรับค่าการกดจากผู้ใช้งาน

Source Code

ReactJSReact Native
Copy
import styled, { css } from "styled-components";

export default function InputButton({
  icon,
  text,
  children,
  onClick,
  style,
  direction = "row",
  isDisable,
  gap = "8px",
  design = "solid",
  fontColor = "#fff",
  fontSize = "16px",
  fontWeight,
  fontFamily,
  backgroundColor = "#465750",
  borderWidth = "1px",
  borderRadius = "8px",
  borderColor,
  paddingHorizontal = "12px",
  paddingVertical = "8px",
  margin,
  width,
  height,
  display = "inline-flex",
  shadow,
  className,
}) {
  return (
    <Styled
      className={className}
      style={style}
      onClick={(e) => {
        if (onClick && !isDisable) {
          onClick(e);
        }
      }}
      $isDisable={isDisable}
      $margin={margin}
      $width={width}
      $height={height}
      $design={design}
      $borderWidth={borderWidth}
      $borderColor={borderColor}
      $borderRadius={borderRadius}
      $fontSize={fontSize}
      $fontWeight={fontWeight}
      $fontFamily={fontFamily}
      $fontColor={fontColor}
      $display={display}
      $direction={direction}
      $shadow={shadow}
      $icon={icon}
      $text={text}
      $backgroundColor={backgroundColor}
      $paddingVertical={paddingVertical}
      $paddingHorizontal={paddingHorizontal}
      $gap={gap}
    >
      {icon && <span className={"icon"}>{icon}</span>}
      {text && <span className={"txt"}>{text}</span>}
      {children}
    </Styled>
  );
}

const Styled = styled.button`
  cursor: pointer;
  justify-content: center;
  align-items: center;
  display: ${({ $display }) => $display};
  flex-direction: ${({ $direction }) => $direction};
  width: ${({ $width }) => $width};
  height: ${({ $height }) => $height};
  margin: ${({ $margin }) => $margin};
  border-radius: ${({ $borderRadius }) => $borderRadius};
  padding: ${({ $paddingHorizontal, $paddingVertical }) =>
    $paddingHorizontal + " " + $paddingVertical};
  box-shadow: ${({ $shadow }) => $shadow};
  opacity: ${({ $isDisable }) => ($isDisable ? "0.5" : "1")};
  border-width: ${({ $borderWidth }) => $borderWidth};
  border-style: solid;
  .txt {
    font-size: ${({ $fontSize }) => $fontSize};
    font-weight: ${({ $fontWeight }) => $fontWeight};
    font-family: ${({ $fontFamily }) => $fontFamily};
  }
  ${({ $design }) => designStyles($design)};
  .icon {
    margin-right: ${({ $text, $gap, $direction }) =>
      $direction == "row" && $text ? $gap : null};
    margin-left: ${({ $text, $gap, $direction }) =>
      $direction == "row-reverse" && $text ? $gap : null};
    margin-bottom: ${({ $text, $gap, $direction }) =>
      $direction == "column" && $text ? $gap : null};
    margin-top: ${({ $text, $gap, $direction }) =>
      $direction == "column-reverse" && $text ? $gap : null};
  }
  .icon {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  &:active {
    opacity: 0.8;
  }
`;

const designStyles = (design) =>
  ({
    solid: css`
      background-color: ${({ $backgroundColor }) => $backgroundColor};
      border-color: ${({ $backgroundColor }) => $backgroundColor};
      .txt {
        color: ${({ $fontColor }) => $fontColor};
      }
    `,
    none: css`
      background-color: transparent;
      border-color: transparent;
      .txt {
        color: ${({ $fontColor }) => $fontColor};
      }
    `,
    outline: css`
      background-color: transparent;
      border-color: ${({ $borderColor, $fontColor }) =>
        $borderColor || $fontColor};
      .txt {
        color: ${({ $fontColor }) => $fontColor};
      }
    `,
  }[design]);
Copy
import { StyleSheet, Text, TouchableOpacity, View } from "react-native";
export default function InputButton({
  icon,
  text,
  children,
  onClick,
  style,
  direction = "row",
  isDisable,
  gap = 8,
  design = "solid", //solid, outline, none
  fontColor = "#fff",
  fontSize = 16,
  fontWeight,
  fontFamily,
  backgroundColor = "#465750",
  borderWidth = 1,
  borderRadius = 8,
  borderColor,
  paddingHorizontal = 12,
  paddingVertical = 8,
  margin,
  marginTop,
  marginLeft,
  marginRight,
  marginBottom,
  width,
  height,
}) {
  const styles = getStyles({
    width,
    height,
    fontSize: style?.fontSize || fontSize,
    fontColor: style?.color || fontColor,
    fontWeight: style?.fontWeight || fontWeight,
    fontFamily: style?.fontFamily || fontFamily,
    direction: direction || style.flexDirection,
    icon,
    text,
    gap,
    design,
    borderWidth,
    borderRadius: style?.borderRadius || borderRadius,
    backgroundColor,
    paddingHorizontal,
    paddingVertical,
    margin,
    marginTop,
    marginLeft,
    marginRight,
    marginBottom,
    isDisable,
    borderColor,
  });

  const handleOnPress = () => {
    if (onClick && !isDisable) {
      onClick();
    }
  };

  return (
    <TouchableOpacity
      style={[
        styles.container,
        design == "solid"
          ? styles.solid
          : design == "outline"
          ? styles.outline
          : styles.none,
        Array.isArray(style) ? style : { ...style },
      ]}
      onPress={handleOnPress}
    >
      {icon && <View style={styles.icon}>{icon}</View>}
      {text && <Text style={styles.text}>{text}</Text>}
      {children}
    </TouchableOpacity>
  );
}

const getStyles = (props) =>
  StyleSheet.create({
    container: {
      justifyContent: "center",
      flexDirection: props.direction,
      alignItems: "center",
      borderWidth: props.borderWidth,
      width: props.width,
      height: props.height,
      borderRadius: props.borderRadius,
      paddingVertical: props.paddingVertical,
      paddingHorizontal: props.paddingHorizontal,
      opacity: props?.isDisable ? 0.5 : 1,
      margin: props.margin,
      marginTop: props.marginTop,
      marginLeft: props.marginLeft,
      marginRight: props.marginRight,
      marginBottom: props.marginBottom,
    },
    solid: {
      backgroundColor: props.backgroundColor,
      borderColor: props.backgroundColor,
    },
    outline: {
      backgroundColor: "transparent",
      borderColor: props.borderColor || props.fontColor,
    },
    none: {
      backgroundColor: "transparent",
      borderColor: "transparent",
    },
    icon: {
      marginRight: props?.direction == "row" && props?.text ? props.gap : null,
      marginLeft:
        props?.direction == "row-reverse" && props?.text ? props.gap : null,
      marginBottom:
        props?.direction == "column" && props?.text ? props.gap : null,
      marginTop:
        props?.direction == "column-reverse" && props?.text ? props.gap : null,
    },
    text: {
      fontSize: props.fontSize,
      color: props.design == "solid" ? "#fff" : props.fontColor,
      fontWeight: props?.fontWeight || null,
      fontFamily: props?.fontFamily || null,
    },
  });

Usage

ReactjsReact Native
Copy
import IconBug from "../../element/icon/IconBug";
import InputButton from "../../example/InputButton";

export default function TestView() {
  return (
    <div className="test-view">
      <InputButton
        icon={<IconBug />}
        text={"This is button"}
        onClick={() => {
          console.log("clicked!");
        }}
      />
    </div>
  );
}
Copy
import InputButton from "../element/input/InputButton";

export default function TestView() {
  return (
    <InputButton
      text={"This is button"}
      marginTop={100}
      marginLeft={20}
      marginRight={20}
    />
  );
}

Properties

Property

Description

Type

Default

className

กำหนดลักษณะเฉพาะของ view

string

paddingHorizontal

พื้นที่ว่างระหว่างเส้นขอบของ view และ เนื้อหาของ view ตามแกน x

string

"12px"

style

inline style ของ view

CSSProperties

onClick

ฟังก์ชันจะทำงานเมื่อทำการกด view

function

isDisable

เมื่อกำหนดค่า เป็น true จะไม่สามารถทำงานฟังก์ชัน onClick 

boolean

margin

พื้นที่ว่างระหว่างขอบนอกสุดของ view และ view อื่นๆ

string

width

ความกว้างภายในเนื้อหาของ view

string

height

ความสูงภายในเนื้อหาของ view

string

design

รูปแบบดีไซน์ พื้นฐานของ view เช่น solid (มีสีพื้นหลัง), outline (มีเส้นขอบ) และ none (ไม่มีสีพื้นหลังและเส้นขอบ)

string

"solid"

borderRadius

ความโค้งมนของ view

string

"8px"

fontSize

ขนาดตัวอักษรของ view

string

"16px"

fontWeight

ความหนาตัวอักษรของ view

string

fontColor

สีตัวอักษรของ view

string

display

รูปแบบการแสดงผลของ view

string

"inline-flex"

justify

การจัดเรียงเนื้อหาตามแกนหลักของ view

string

"center"

align

การจัดเรียงเนื้อหาตามแกนรองของ view

string

"center"

shadow

ลักษณะเงาของ view

string

direction

ทิศทางการเรียงตัวของ children ภายใน view

string

"row"

borderWidth

ความหนาเส้นขอบของ view

string

borderColor

สีเส้นขอบของ view

string

text

ตัวอักษรภายใน view

ReactElement

icon

ไอคอนภายใน view

ReactElement

children

custom children view 

ReactElement

paddingVertical

พื้นที่ว่างระหว่างเส้นขอบของ view และ เนื้อหาของ view ตามแกน y

string

"8px"

fontFamily

ชื่อของ font ที่ใช้ใน view

string

gap

ระยะห่างระหว่าง icon และ text

string

shadow

เงาของ view

string

Requirements

styled-component react

ยักซ่า (Yakxar)
สร้างสรรค์และแบ่งปันสื่อดิจิทัลง่ายๆ ที่นี่
© 2025 ยักซ่า (Yakxar)