Skip to content

Add options to mixin file #62

@iammaxence

Description

@iammaxence

I'm currently working with tikui on a project and I regularly have components that change depending on an element.

Example:

mixin test-button(options)
  -const opt = options || {}
  -const classList = [];
  -if (opt.isRed) classList.push('-red');
  -if (opt.isBlue) { classList.push('-blue') };
  -const classes = classList.length > 0 ? classList.join(' ') : null;
  button.test-button(disabled=opt.isDisabled, class=classes)

I would like to propose that we add to CLI the possibility to add options to mixin:

program
    .command('create <component> [destination]')
    .option('-p, --prefix <name>', 'prefix')
    .option('-mo, --mixin-options', 'add options parameter to mixin')
    .description('create a component.')
    .addHelpText('after', '\nExample:\n $ tikui create -p tikui component src/atom')
    .action((component, destination, options) => {
      createComponent(destination, component, options.prefix, options.mixinOptions);
      console.log(`Creating component ${component} to ${path.resolve(destination)}`); // eslint-disable-line no-console
    });

To get this result :

mixin test-component(options)
  -const opt = options || {};
  -const classList = [];
  -const classes = classList.length > 0 ? classList.join(' ') : null;
  .test-component(class=classes) test-component

The createMixin function will be like this:

const createMixin = (componentDirectory: string, component: string, prefix?: string, mixinOptions?: boolean): void => {
  let content = `mixin ${dashPrefix(prefix)}${component}\n  .${dashPrefix(prefix)}${component} ${component}\n`;
  if(mixinOptions){
    content = `mixin ${dashPrefix(prefix)}${component}(options)
    -const opt = options || {};
    -const classList = [];
    -const classes = classList.length > 0 ? classList.join(' ') : null;
    .${dashPrefix(prefix)}${component}(class=classes) ${component}\n`;

  }
  const file = `${component}.mixin.pug`;
  createFile(componentDirectory, file);
  fs.writeFileSync(path.resolve(componentDirectory, file), content);
};

What do you think ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions