[React] React.js

Hoya_03ยท2022๋…„ 6์›” 27์ผ
0

ai_School

๋ชฉ๋ก ๋ณด๊ธฐ
22/30
post-thumbnail

๐ŸŒŸ ์ฆ๊ฐ€ ๋ฐ ๊ฐ์†Œ ๋ฒ„ํŠผ ์ ์šฉํ•˜๊ธฐ

import React, { Component } from 'react';

class Box1 extends Component {
  render(props) {
    return (
        <div className="box">
          Box{this.props.index}
          <p>{this.props.name}</p>
        </div>
    );
  }
}

export default Box1;
css code
.box {
  width: 100px;
  height: 100px;
  border: 1px solid red;
}
import logo from './logo.svg';
import './App.css';
import Box1 from './component/Box1';
import {useState} from "react";

function App() {
  // let counter = 0;
  const [counter, setCounter] = useState(0);
  const [counter2, setCounter2] = useState(0);
  const increase = () => {
    setCounter(counter + 1);
    // console.log("counter๋Š” ", counter);
  }
  const decrease = () => {
    setCounter(counter - 1);
    // console.log("counter๋Š” ", counter);
  }
  return (
    <div>
      <Box1 name="ํ•œ๊ตญ" index="1"/>
      <Box1 name="๋ฏธ๊ตญ" index="2"/>
      <Box1 name="์ค‘๊ตญ" index="3"/>

      <div>{counter}</div>
      <button onClick={increase}>์ฆ๊ฐ€!</button>
      <button onClick={decrease}>๊ฐ์†Œ!</button>
    </div>
  );
}

exprot default App;

๐ŸŒŸStorybook ํ™œ์šฉํ•˜๊ธฐ

  1. stroybook์„ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ
  2. npx -p storybook sb init ์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋”์•ˆ์„ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ฃผ๊ธฐ
  3. npm run storybook ์œผ๋กœ ์‹คํ–‰ํ•˜๊ธฐ

text ๋งŒ๋“ค๊ธฐ

import React, { Component } from "react";
import PropTypes from "prop-types";

export function Text({ children, color, italic, underline }) {
  const style = {
    color: color,
    fontStyle: italic ? "italic" : "normal",
    textDecoration: underline ? "underline" : "none",
  };
  return <span style={style}>{children}</span>;
}

Text.propTypes = {
  children: PropTypes.string.isRequired,
  color: PropTypes.string,
  italic: PropTypes.bool,
  underline: PropTypes.bool,
};

Text.defaultProps = {
  color: "black",
  italic: false,
  underline: false,
};
import React, { Component } from "react";
import {Text} from "./Text";

export default{
    title : "Text",
    component : Text,
};

const TEST_TEXT = "Story Text Test";
export const Default = () =><Text>{TEST_TEXT}</Text>;
export const Red = () =><Text color="red">{TEST_TEXT}</Text>;
export const Italic = () =><Text italic>{TEST_TEXT}</Text>;
export const Underline = () =><Text underline>{TEST_TEXT}</Text>;

Input ๋งŒ๋“ค๊ธฐ

import React, { Component } from "react";
import PropTypes from "prop-types";

class Input extends Component {
  constructor(props) {
    super(props);
    this.setRef = this.setRef.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    const { name, onChange } = this.props;
    if (onChange) {
      onChange(name, e.target.value);
    }
  }

  componentDidMount() {
    if (this.props.autoFocus) {
      this.ref.focus();
    }
  }

  componentDidUpdate() {
    if (this.props.autoFocus) {
      this.ref.focus();
    }
  }

  setRef(ref) {
    this.ref = ref;
  }

  render() {
    const { errorMessage, label, name, value, type, onFocus } = this.props;
    return (
      <label>
        {label}
        <input
          id={"input_${name}"}
          ref={this.setRef}
          onChange={this.handleChange}
          onFocus={onFocus}
          value={value}
          type={type}
        />
        {errorMessage && <span className="error">{errorMessage}</span>}
      </label>
    );
  }
}

Input.propTypes = {
  type: PropTypes.oneOf(["text", "number", "price"]),
  name: PropTypes.string.isRequired,
  value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  errorMessage: PropTypes.string,
  label: PropTypes.string,
  onChange: PropTypes.func,
  autoFocus: PropTypes.bool,
};

Input.defaultProps = {
  onChange: () => {},
  onFocus: () => {},
  autoFocus: false,
  type: "text",
};

export default Input;
import React, { Component } from 'react';
import Input from "./Input";

export default{
    title : "Input",
    component : Input,
};

export const label = () => <Input name="name" label="์ด๋ฆ„ : "/>;

๋ฆฌ์—‘ํŠธ์—๋Œ€ํ•˜์—ฌ ๋” ๊ณต๋ถ€๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ์ด๋“ค์—ˆ๋‹ค.

profile
๋น„์ „๊ณต์ž์˜ ํ”„๋ก ํŠธ์•ค๋“œ ๋„์ „๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€