<!-- havent added gamepad support yet -- try I, K, J, and Enter. -->
<!-- other to-dos: (1) add Stripe modal, post-data entry, (2) maybe find a better .focus() mechanism -->

<div id="app">
  <p>I've been binging the first half of the History of the Internet podcast lately, and there are several conversations about the post-commercialization/pre-Secure Sockets Layer era. People would fill their Amazon carts but then call in their CC numbers, or they'd (foolishly) be willing to email CC info, but not plug it into a website.</p>

  <p>At the same time, I was going through a sort-of retro videogame kick.</p>

  <p>Somehow the two things swirled together, and I started to wonder: Does the quality of the visuals matter? What if SSL had existed for years? Would people have been willing to plug CC info into a 1995 browser? What about the Lynx text-only browser?</p>

  <p>Let's get weird. What about even earlier, lesser-quality ("quality") graphics?</p>

  <p>My initial thought was "If the interface looked like Atari 2600 Hangman, would Dad have had the confidence to joystick in his Amex card to buy me a new game over the phone line?"</p>

  <p>That idea, plugging CC numbers into Hangman, was in the back of my head when I randomly came across Ben Bittner's Asteroids font. (I think I got there from something he posted on Metafilter, if memory serves.) So its not 2600 Hangman, like my gut insisted, but it is sorta iconic imagery.</p>

  <p>Would you put your CC info into a 1979 interface? Or does the slickness of 2018's Stripe element make you feel better?</p>
</div>
@font-face {
  font-family: 'Vectorhand-Regular';
  src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/504344/Vectorhand-Regular.ttf');
}

body {
  background: black;
}
const arrayMaker = num => Array(num).fill(null);
const {default: sc, keyframes} = styled;


const flicker = keyframes`
  16.665% {opacity: 1;}
  33.33% {opacity: .46;}
  59.995% {opacity: .9;}
  100% {opacity: 1;}
`;
const blink = keyframes`
  16.665% {opacity: 1;}
  50% { opacity: 0;}
  100% {opacity: 1;}
`;

const Wrapper = sc.div`
  min-height: 800px;
  margin: 0 auto;
  background: #121;
  opacity: .8;
  color: white;
  margin: 2rem;
  padding: .5rem 1rem;
  font-family: 'Vectorhand-Regular';
  position: relative;
`;

const HeaderText = sc.div`
  font-size: 1.25rem;
  display: flex;
  justify-content: space-between;
  width: 100%;
  animation: ${flicker} .0101s linear infinite;
`;

const BigText = sc.p`
  font-size: 2rem;
  text-align: center;
  animation: ${flicker} .01s linear infinite;
`;

const Header = ({ score }) => (
  <HeaderText>
    <span>Credit Card Entry 1979</span>
    <span>Score: {score}</span>
  </HeaderText>
);

const Row = sc.div`
  display: flex;
  justify-content: flex-start;
  align-items: flex-end;
  * {
   margin: 0;
   padding: 0;
   width: 1.25rem;
  }
  font-size: 2rem;
  animation: ${flicker} .0104s linear infinite;
`;

class Field extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      userInput: arrayMaker(this.props.amount),
      current: '_',
      active: 0,
    }
    this.flashRef = React.createRef();
  }
  focusRef = () => {
    this.flashRef.current.focus();
  }
  handleKeyPress = (keyCode, name, amount) => {
    const { current, active } = this.state;
    const { type } = this.props;
    const zeroOrOne = ['0', '1'];
    const zeroOneTwo = ['0', '1', '2'];
    const oneTwoThree = ['1', '2', '3'];
    let valid;
    switch (type) {
      case 'numeric':
        valid = ['_','0','1','2','3','4','5','6','7','8','9'];
        break;
      case 'alphanum':
        valid = ['_','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L', 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        break;
      case 'alpha':
      default:
        valid = ['_','A','B','C','D','E','F','G','H','I','J','K','L', 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    }
    
    const currentIndex = valid.indexOf(current);
      if (keyCode === 74) {
        if (active === 0) return null;
        const userInput = this.state.userInput.slice();
        userInput[active] = null;
        userInput[active - 1] = null;
        return this.setState({ current: '_', userInput, active: active - 1 }, () => this.focusRef());
      }
      if (keyCode === 73) {
        const newCurrent = currentIndex === valid.length - 1 ? '_' : valid[currentIndex + 1];
        return this.setState({ current: newCurrent });
      }
      if (keyCode === 75) {
        const newCurrent = currentIndex === 0 ? valid[valid.length - 1] : valid[currentIndex - 1];
        return this.setState({ current: newCurrent });
      }
      if (keyCode === 13) {
        if ((type === 'numeric' || name === 'zip' || name === "state") && current === '_') return null; // no blanks in numeric fields
        if (name === 'month' && active === 0 && !zeroOrOne.includes(current)) return null; // only 0 or 1 in _month
        if (name === 'month' && active === 1 && this.state.userInput[0] === 0  && !zeroOneTwo.includes(current)) return null; // only 0-2 in month_ if _0
        if (name === 'year' && active === 0 && !oneTwoThree.includes(current)) return null; // only 1-3 in _year
        const userInput = this.state.userInput.slice();
        userInput[active] = current;
        if (active !== amount - 1) {
          return this.setState({ current: '_', userInput, active: active + 1 }, () => this.focusRef());
        }
        this.setState({ userInput });
        return this.props.setField({ name, valueArray: userInput });
      }
    return null;
  }
  componentDidMount() {
    this.focusRef();
  }
  render() {
    const { name, labl, amount, value } = this.props;
    const { userInput, current, active } = this.state;
    return (
      <div>
        <p>{labl}</p>
        {
          value !== null 
            ? <Row>{value.map(f => (<p>{f}</p>))}</Row>
            : <Row>
                {userInput.map((f, i) => {
                  if (f === null && i !== active) {
                    return <p>_</p>;
                  } else if (f === null) {
                    return (
                      <div
                        style={{ color: '#a7b', outline: 'none' }} //can't extract b/c of React ref
                        ref={this.flashRef}
                        tabIndex="0"
                        onKeyDown={(e) => this.handleKeyPress(e.keyCode, name, amount)}
                      >
                        {current}
                      </div>
                    );
                  }
                  return <p>{f}</p>
                })}
              </Row>
        }
      </div>
     );  
    }
}

class Main extends React.Component {
  state = {
    // number: [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
    number: null,
    month: null,
    year: null,
    name:  null,
    add1:  null,
    add2:  null,
    city:  null,
    state:  null,
    zip:  null,
    count: 1,
  }

  setField = ({ name, valueArray }) => {
    const score = valueArray.filter(v => v !== '_').length;
    this.props.updateScore(score);
    return this.setState({ [name]: valueArray, count: this.state.count + 1 });
  }

  render() {
    const { number, month, year, name, add1, add2, city, state, zip, count } = this.state;
    const all = [
      <Field value={number} amount={16} type='numeric' name="number" labl='Card No.: ' setField={this.setField} />,
      <Field value={month} amount={2} type='numeric' name="month" labl='Month: ' setField={this.setField} />,
      <Field value={year} amount={2} type='numeric' name="year" labl='Year: ' setField={this.setField} />,
      <Field value={add1} amount={26} type='alphanum' name="add1" labl='Address: ' setField={this.setField} />,
      // <Field value={add2} amount={26} type='alphanum' name="add2" labl='Address2: ' setField={this.setField} />,
      <Field value={city} amount={16} type='alpha' name="city" labl='City: ' setField={this.setField} />,
      <Field value={state} amount={2} type='alpha' name="state" labl='State: ' setField={this.setField} />,
      <Field value={zip} amount={5} type='numeric' name="zip" labl='ZIP: ' setField={this.setField} />,
    ];
    const current = all.slice(0, count);
    return (
      <div style={{ padding: '1rem', display: 'flex', flexFlow: 'column nowrap', alignItems: 'center' }}>
        {current}
      </div>
    )
  }
}


const BottomDiv = sc.div`
  position: absolute;
  bottom: 0;
  right: 13px;
`;

const Footer = () => (
  <BottomDiv>
    <p>a dumb idea I had. Vectorhand-Regular font courtesy <a style={{ color: 'white' }} href="https://surlyben.itch.io/">Ben Bittner</a> (@surlyben)</p>
  </BottomDiv>
);

class App extends React.Component {
  state = {
    score: 0,
  }

  updateScore = num => this.setState({ score: this.state.score + num });
  
  render() {
    const { score } = this.state;
    return (
      <Wrapper>
        <Header score={score} />
        <BigText>ENTER CREDENTIALS</BigText>
        <Main updateScore={this.updateScore} />
        <Footer />
      </Wrapper>
    ); 
  }  
}

ReactDOM.render(<App />, document.getElementById("app"));
View Compiled
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js
  3. https://cdnjs.cloudflare.com/ajax/libs/styled-components/3.4.1/styled-components.min.js